Upload image to MinIO

Viewed 2973

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

0 Answers
Related