我一直在尝试通过golang sdk上传到cloudinary。但有时它似乎在多次请求后失败(有时有效,有时无效)。
我在没有 vpn 的本地主机(windows)中运行应用程序。
错误信息:
“https://api.cloudinary.com/v1_1/dd6mdlpgj/auto/upload”:拨号tcp:查找api.cloudinary.com:没有这样的主机
type cloudinaryStorageRepository struct {
cld *cloudinary.Cloudinary
}
func NewCloudinaryStorageRepository(cloudName string, apiKey string, apiSecret string) domain.StorageRepository {
cld, err := cloudinary.NewFromParams(cloudName, apiKey, apiSecret)
if err != nil {
panic(err)
}
return &cloudinaryStorageRepository{cld}
}
func (c *cloudinaryStorageRepository) Upload(ctx context.Context, storage *domain.Storage) (string, error) {
file, err := storage.File.Open()
defer func(file multipart.File) {
err := file.Close()
if err != nil {
logrus.Error(err)
}
}(file)
if err != nil {
logrus.Error(err)
return "", err
}
uploaded, err := c.cld.Upload.Upload(ctx, file, uploader.UploadParams{
Folder: storage.Folder,
PublicID: storage.Name,
})
if err != nil {
logrus.Error(err)
return "", err
}
return uploaded.SecureURL, nil
}
如果我没理解错的话,你可以尝试在你的电脑上重置你的 DNS 或使用另一台电脑来运行代码,因为这可能是你本地主机的错误配置。