go - 上传图片到 MinIO
问题描述
我想将格式为 .png 的图像上传到 MinIo。但是我有错误找不到目录的问题
Error: open /sensors/download (1).png: no such file or directory
我在名为 ems_service 的 minio 存储桶中创建了一个带有传感器名称的目录文件。
就像我编写的这段代码一样,使用 Go 和 Minio 上传图像文件
ctx := context.Background()
endpoint := config.MINIO_ENDPOINT
accessKeyID := config.MINIO_ID
secretAccessKey := config.MINIO_KEY
useSSL := true
contentType := "image/png"
location := "us-east-1"
utilities.Info.Printf("secret access key: %+v", secretAccessKey)
utilities.Info.Printf("access ID: %+v", accessKeyID)
// Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
// minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
if err != nil {
utilities.Error.Printf("Error minio Client : %s", err)
response[config.MESSAGE_RESPONSE_INDEX] = "Error Minio Client"
c.JSON(http.StatusInternalServerError, response)
return
}
bucketName := config.MINIO_BUCKET_NAME
utilities.Info.Printf("minio client: %+v", &minioClient)
utilities.Info.Printf("Bucket name check: %+v\n", bucketName)
utilities.Info.Printf("Endpoint URL minio: %+v", endpoint)
// Make a new bucket.
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location, ObjectLocking: true})
// err = minioClient.MakeBucket(bucketName, location)
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
utilities.Info.Printf("bucket exists: %+v", exists)
if errBucketExists == nil && exists {
utilities.Info.Printf("We already own %+v\n", bucketName)
} else {
utilities.Error.Printf("make bucket error : %s", err)
response[config.MESSAGE_RESPONSE_INDEX] = "make bucket error"
c.JSON(http.StatusInternalServerError, response)
return
}
} else {
utilities.Info.Printf("Successfully created %s\n", bucketName)
}
fileNormalIcon, _ := c.FormFile("file_normal_icon")
utilities.Info.Printf("filenormalicon: %+v", fileNormalIcon)
var pathNormalIcon string
if fileNormalIcon != nil {
// Upload the .png file
pathNormalIcon = "/sensors/" + fileNormalIcon.Filename
utilities.Info.Printf("Pathnormalicon: %+v", pathNormalIcon)
utilities.Info.Printf("Endpoint URL minio: %+v", endpoint)
utilities.Info.Printf("Bucket name check: %+v\n", bucketName)
// Upload file .png with FPutObject
output, err := minioClient.FPutObject(ctx, bucketName, fileNormalIcon.Filename, pathNormalIcon, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
utilities.Error.Printf("Error upload image to bucket: %s", err)
response[config.MESSAGE_RESPONSE_INDEX] = fmt.Sprintf("err: %s", err.Error())
c.JSON(http.StatusInternalServerError, response)
return
}
utilities.Info.Panicf("result: %+v", output)
data["normal_icon"] = pathNormalIcon
dataUpdateExist = true
}
文档链接 Minio Go API 参考代码
解决方案
推荐阅读
- algorithm - 从可能有 1,024 个节点的无向图中提取所有唯一的完整子图的最佳算法是什么?
- ansible - sec_fact 给了我最后一场比赛
- python - IndexError:数组索引超出范围(但索引确实存在?)
- java - 合并映射中键包含的值(Java)
- c++ - 关于多维数组的C++问题
- node.js - 如何让一个函数同时返回一个promise和一个回调
- javascript - 无论如何要保留typescript4.0中接口的注释吗?
- node.js - Kubernetes + 领事:kv.get:连接 ETIMEDOUT
- php - 使用主键和外键将值添加到数据库中
- xamarin - Xamarin.IOS 和 Signalr 推送通知