how to get the file link after successfully uploading in minio
Asked Answered
J

3

9

I am using minio to manage the files

const getMinioClient = () => {
  const minioClient = new Minio.Client({
    endPoint: '127.0.0.1',
    port: 9000,
    useSSL: false,
    accessKey: 'minioadmin',
    secretKey: 'minioadmin'

  });
  return minioClient;
};

  uploadFile(bucketName, newFileName, localFileLocation,metadata={}) {
    return new Promise((resolve, reject) => {
      const minioClient = getMinioClient();
      //'application/octet-stream'
      minioClient.fPutObject(bucketName, newFileName, localFileLocation, metadata , (err, etag) => {
        if (err) return reject(err);

        return resolve(etag);
      });
    });
  }

with the following code I can upload the file, after successfully uploading it returns me only with etag, but I want to get the download link, how would I get it directly without searching the filename again.

Johore answered 14/2, 2021 at 2:56 Comment(1)
Did you get a solution? Please share if you did.Rear
D
3

You won't be able to get something like Public URL/Link for accessing images unless you ask for it to manually generate a time limited download URL using something like: https://min.io/docs/minio/linux/reference/minio-mc/mc-share-download.html#generate-a-url-to-download-object-s

One workaround is to let nginx directly access the location you are uploading your files to: https://gist.github.com/harshavardhana/f05b60fe6f96803743f38bea4b565bbf

Director answered 13/11, 2022 at 14:29 Comment(0)
W
1

I have a bit of an iffy from a bit of code I worked on

var UploadMediaFile = client.UploadFile(addNewMedia.MediaFile.OpenReadStream(), 
                                        "bucket", 
                                        "public/" + uploadedMediaIDName + "." + dotExtension, 
                                        addNewMedia.MediaFile.ContentType)
                            .ConfigureAwait(false);

And then basically built the link afterwards in a string

addNewMedia.NewMedia.MediaContent = "minioaddress:port" +"/" + "bucket" + "/" + "folder" + "/" + uploadedMediaIDName + "." + dotExtension;

So basically

https://ip:port/[bucket Name]/[file path if folders exist]/[FileName].[extension]

You need to use a public or custom bucket.

Copy and paste this in your custom bucket if you want to have a private bucket with read only access in a folder called public

{
    "Version": "2012-10-17",
    "Statement": [
    {
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "*"
        ]
      },
      "Resource": [
        "arn:aws:s3:::campaign-engine/public*"
      ],
      "Sid": ""
    }
  ]        
}

Hope this helps a bit

Wesla answered 20/3 at 12:17 Comment(1)
forgot to mention that used this solution because it is the only way I found to get a permanent link to a piece of media stored in a minio server, would be a bit bothersome to use links that expire in a website or a webappWesla
N
0

After you have successfully written your file with your code above, you can use presignedUrl method to generate the link to your image.

An example for Javascript is here: https://min.io/docs/minio/linux/developers/javascript/API.html#presignedUrl:~:text=//%20presigned%20url%20for%20%27getObject%27%20method.%0A//%20expires%20in%20a%20day.%0AminioClient.presignedUrl(%27GET%27%2C%20%27mybucket%27%2C%20%27hello.txt%27%2C%2024*60*60%2C%20function(err%2C%20presignedUrl)%20%7B%0A%20%20if%20(err)%20return%20console.log(err)%0A%20%20console.log(presignedUrl)%0A%7D)

In any case you have to set an expiration time. Here or you set a very long time, which is suitable to your app or if you have a backend, require the images from Frontend through the backend with the getObject method: getObject(bucketName, objectName, getOpts[, callback]). https://min.io/docs/minio/linux/developers/javascript/API.html#presignedUrl:~:text=getObject(bucketName%2C%20objectName%2C%20getOpts%5B%2C%20callback%5D)

If you have only a few number of static images to show in your app, (which are not uploaded by your app), you can also create the links manually with tme minio client or from the Minio-UI.

Nolan answered 11/12, 2022 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.