小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《将图像上传到 MinIO》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!
问题内容我想将.png格式的图片上传到minio。但我遇到错误找不到目录的问题
error: open /sensors/download (1).png: no such file or directory
我在名为 ems_service 的 minio 存储桶中创建了一个名为 ems_service 的目录文件,其名称为传感器名称。
就像我编写的使用 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 参考代码文档链接
minio go 客户端 api
正确答案
您可以按照以下方式通过 go client api 将图像放入 minio:
前置步骤:
- 执行 minio:
$ minio_root_user=minio minio_root_password=minio123 minio server /volumes/data{1...4} --address :9000 --console-address :9001
minio object storage server
status: 4 online, 0 offline.
api: http://192.168.0.13:9000 http://127.0.0.1:9000
rootuser: minio
rootpass: minio123
console: http://192.168.0.13:9001 http://127.0.0.1:9001
rootuser: minio
rootpass: minio123
command-line: https://min.io/docs/minio/linux/reference/minio-mc.html#quickstart
$ mc alias set myminio http://192.168.0.13:9000 minio minio123
documentation: https://min.io/docs/minio/linux/index.html
- 创建存储桶:
- 准备好您的图像,以便 go 代码可以读取它
$ ls
my-filename.png put.go
步骤:
- 使用此代码:
package main
import (
"context"
"log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
endpoint := "127.0.0.1:9000"
accesskeyid := "minio"
secretaccesskey := "minio123"
usessl := false
s3client, err := minio.new(
endpoint, &minio.options{
creds: credentials.newstaticv4(accesskeyid, secretaccesskey, ""),
secure: usessl,
})
if err != nil {
log.fatalln(err)
}
if _, err := s3client.fputobject(context.background(), "my-bucketname", "my-objectname.png", "my-filename.png", minio.putobjectoptions{
contenttype: "application/csv",
}); err != nil {
log.fatalln(err)
}
log.println("successfully uploaded")
}
- 编译并运行 go 代码:
$ go build put.go
$ ./put
2023/03/18 17:41:04 Successfully uploaded
- 然后在控制台中观看图像:
希望这有帮助! :)
以上就是《将图像上传到 MinIO》的详细内容,更多关于的资料请关注编程网公众号!