小伙伴们对golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《将图像上传到 MiNIO》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的
小伙伴们对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_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
$ 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 build put.go
$ ./put
2023/03/18 17:41:04 Successfully uploaded
希望这有帮助! :)
以上就是《将图像上传到 MinIO》的详细内容,更多关于的资料请关注编程网公众号!
--结束END--
本文标题: 将图像上传到 MinIO
本文链接: https://lsjlt.com/news/596580.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-05
2024-04-05
2024-04-05
2024-04-04
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0