将图像上传到MinIO

huangapple go评论70阅读模式
英文:

Upload image to MinIO

问题

我想将格式为.png的图像上传到MinIo。但是我遇到了找不到目录的错误。

错误打开/sensors/download (1).png没有这样的文件或目录

我已经在名为ems_service的minio存储桶中创建了一个名为sensor的目录文件。

像这样,我使用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)

// 初始化minio客户端对象。
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)

// 创建一个新的存储桶。
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location, ObjectLocking: true})
// err = minioClient.MakeBucket(bucketName, location)
if err != nil {
    // 检查我们是否已经拥有此存储桶(如果您运行两次,则会发生这种情况)
    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 {
    // 上传.png文件
    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)

    // 使用FPutObject上传文件.png
    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 Client API

英文:

I want to upload image with format .png to MinIo. But I have problem with error not find directory

Error: open /sensors/download (1).png: no such file or directory

I have created a directory file with sensor name inside minio bucket named ems_service.

Like this code I wrote to upload an image file using Go with 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
}

Links For documentation Minio Go API refrence Code

MinIo Go Client API

答案1

得分: 1

你可以按照以下步骤使用Go Client API将图像放入MinIO:

预备步骤:

  1. 执行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
  1. 创建存储桶:

将图像上传到MinIO

  1. 准备好Go代码可以使用的图像文件
$ ls
my-filename.png	put.go

步骤:

  1. 使用以下代码:
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")
}
  1. 编译并运行Go代码:
$ go build put.go
$ ./put
2023/03/18 17:41:04 Successfully uploaded
  1. 在控制台中查看图像:

将图像上传到MinIO

你也可以预览它:

将图像上传到MinIO

希望对你有所帮助!:)

英文:

you can do as follow for putting images at MinIO via Go Client API:

Pre-steps:

  1. Execute 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
  1. Create bucket:

将图像上传到MinIO

  1. Have your image ready where Go code can take it
$ ls
my-filename.png	put.go

Steps:

  1. Use this code:
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")
}

  1. Compile and Run the go code:
$ go build put.go
$ ./put
2023/03/18 17:41:04 Successfully uploaded
  1. Then watch the image in console:

将图像上传到MinIO

> You can preview it as well:

将图像上传到MinIO

Hope this helps!!! 将图像上传到MinIO

huangapple
  • 本文由 发表于 2021年9月20日 17:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/69252083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定