我正在按照此文档将文件上传到 GCS。 Setting Up Authentication for Server to Server Production Applications
它在本地工作,但在生产中我收到此错误:
Post https://www.googleapis.com/upload/storage/v1/b/[bucket-name]/o?alt=json&prettyPrint=false&projection=full&uploadType=multipart: x509: failed to load system roots and no roots provided.
func UploadIMG(ctx *context.Context, file []byte, fileName string) error {
storageClient, err := storage.NewClient(*ctx)
if err != nil {
log.Fatal(err)
}
w := storageClient.Bucket(bucketName).Object(fileName).NewWriter(*ctx)
if _, err := w.Write(file); err != nil {return err}
if err := w.Close(); err != nil {return err}
oauthClient, err := google.DefaultClient(*ctx, cloudkms.CloudPlatformScope)
if err != nil {
log.Fatal(err)
}
kmsService, err := cloudkms.New(oauthClient)
if err != nil {
log.Fatal(err)
}
_ = kmsService
return nil
}
最佳答案
您是否确实继续了链接的教程,并确保您拥有正确的凭据?
错误本身可能与证书相关。当它尝试执行请求时,它会在底层系统上查找根证书,但无法找到或打开它们。例如,在 Ubuntu 上,它们应该位于/usr/share/ca-certificates 和/或/etc/ssl/certs 下。确保您的证书具有正确的权限,以便能够执行您想要的请求。
关于go - 无法在生产中将文件上传到谷歌云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55633686/