5

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Titanio Yudista
  • 241
  • 4
  • 8
  • 2
    Have you checked the local path of the file which is being uploaded? In your case the file might not be existed or path might be wrong – sigkilled Sep 20 '21 at 10:06
  • 2
    @TitanioYudista have you provide relative path or absolute path from the example i see it seems relative path and there would be no such file or directory as the error states – Chandan Sep 20 '21 at 10:15
  • 3
    https://github.com/minio/minio-go/blob/master/examples/s3/fputobject.go check the example it is simple. – Prakash S Sep 20 '21 at 15:11
  • 3
    https://github.com/minio/minio-go/blob/master/examples/s3/putobject.go this one as well – Prakash S Sep 20 '21 at 15:11

1 Answers1

1

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:

enter image description here

  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:

enter image description here

You can preview it as well:

enter image description here

Hope this helps!!! :)

Cesar Celis
  • 166
  • 1
  • 4
  • 8