how to get the file link after successfully uploading in minio

Viewed 2683

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.

1 Answers

First, you need to install minio/stable/mc first for Linux/Mac

brew install minio/stable/mc

Than create an Alias :

mc alias set <ALIAS> <YOUR-S3-ENDPOINT> <YOUR-ACCESS-KEY> <YOUR-SECRET-KEY> --api <API-SIGNATURE> --path <BUCKET-LOOKUP-TYPE>

For example:

mc alias set minio http://192.168.1.51 BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12

For Amazon S3 Cloud storage :

mc alias set s3 https://s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12

For Google Cloud Storage

mc alias set gcs  https://storage.googleapis.com BKIKJAA5BMMU2RHO6IBB V8f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12

For example (in my local system) :

mc alias set minio http://localhost:9000 admin password

Next,
Use alias and here Bucket Name is photos and image name is p.png

mc share download --recursive minio/photos/p.png

You will get a output like this :

URL: http://localhost:9000/photos/p.png
Expire: 7 days 0 hours 0 minutes 0 seconds
Share: http://localhost:9000/photos/p.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20220314%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220314T113907Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=34cef307940b43debf85c2d66berb4576132bf2c2a8b19c8ab5b368ef73ff517

OR IF YOU WANT TO GENERATE LINK FROM PROGRAM
Golang code :

package main
import (
    "context"
    "fmt"
    "github.com/minio/minio-go/v7"
    "github.com/minio/minio-go/v7/pkg/credentials"
    "log"
    "net/url"
    "time"
)
func main() {
    endpoint := "127.0.0.1:9000"
    accessKeyID := "admin"
    secretAccessKey := "password"

    useSSL := false

    minioClient, err := minio.New(endpoint, &minio.Options{
        Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
        Secure: useSSL,
    })
    if err != nil {
        log.Fatalln(err)
    }
    fmt.Println(minioClient)

    // Set request parameters for content-disposition.
    reqParams := make(url.Values)
    reqParams.Set("response-content-disposition", "attachment; filename=\"p.png\"")

    // Generates a presigned url which expires in a day.
    presignedURL, err := minioClient.PresignedGetObject(context.Background(), "photos", "p.png", time.Second*24*60*60, reqParams)
    if err != nil {
        log.Println(err)
        return
    }
    fmt.Println("Successfully generated presigned URL", presignedURL)
}

For more Information :

https://docs.min.io/minio/baremetal/reference/minio-mc/mc-share-download.html

https://docs.min.io/docs/minio-client-quickstart-guide.html

https://docs.min.io/docs/golang-client-api-reference.html

Related