首页 > 解决方案 > 上传图片到 MinIO

问题描述

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

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

我在名为 ems_service 的 minio 存储桶中创建了一个带有传感器名称的目录文件。

就像我编写的这段代码一样,使用 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

标签: gominio

解决方案


推荐阅读