minio / minio-js

MinIO Client SDK for Javascript
https://docs.min.io/docs/javascript-client-quickstart-guide.html
Apache License 2.0
955 stars 277 forks source link

Presigned URL from minio is not working #1203

Closed pforprince closed 1 year ago

pforprince commented 1 year ago

Hi there, I'm using Minio in project. Now, I want to keep the bucket private. I'm able to use the upload feature from my application but while getting preSignedUrl, I'm stuck. The URL it is returning it is not valid. Not showing the image as expected. But when I visit the console and share a file from there, it is working fine. Displaying the file as expected. I've provided both URLs in description.

Things to notice => Apart from host in both URLs, there are some things different. Security Token is missing from my own generated URLs Mine is using a different credentials value

Below are the code details.

Package.json

 "minio": "^7.1.1"

Docker compose -

  image-service:
    image: minio/minio
    container_name: image-service
    ports:
      - 9000:9000
      - 9001:9001
    networks:
      - marriage-rubru-tier      
    env_file: ./.env  
    volumes:
      - minio-vol:/data   
    restart: "unless-stopped"
    command: server --console-address ":9001" /data

Minio Client -

var minioClient = new Minio.Client({
    endPoint: 'image-service',
    port: 9000,
    useSSL: false,
    accessKey: MINIO_ACCESS_KEY,
    secretKey: MINIO_SECRET_KEY
});

Function to get URL -

const getFileUrl = async (fileName, expiresIn = 60 * 60, bucketName = 'profiles') => {
    return await minioClient.presignedGetObject(bucketName, fileName, expiresIn);
}

Minio console URL

http://127.0.0.1:9000/profiles/image-1693029523366-326398589.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=4L5XW1QG8FPQEZOTX3DC%2F20230826%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230826T070653Z&X-Amz-Expires=262800&X-Amz-Security-Token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3NLZXkiOiI0TDVYVzFRRzhGUFFFWk9UWDNEQyIsImV4cCI6MTY5MzA3MzI3NiwicGFyZW50IjoibWluaW8ifQ.yok4jcTtHLBmPT4cAzJcqKzCHpTapPp5nwy7FROKchHgkcS8PSwgzFs3SB8NU0KvSx_ZfNZ3Wa5y5-FDgbYG6Q&X-Amz-SignedHeaders=host&versionId=null&X-Amz-Signature=0a17eba6caa7ec65b2510b86747f26efae59e317878e81b95b1c3608c2502f5f

Url that I'm getting

 http://image-service:9000/profiles/image-1693029523366-326398589.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minio%2F20230826%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230826T070007Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=b48d584d98165171423d72d688f2525ab54353a6dc7105f1bd91074ca54788ab
prakashsvmx commented 1 year ago

In docker based MinIO deployments, the host url should be DNS resolveable. So please check. It is mostly networking related issue. Not a MinIO SDK issue. Could you try with the IP ?.

mc admin trace <alias> -v might help.

pforprince commented 1 year ago

HI @prakashsvmx , sure but the url should work. Even after generating the url, I'm changing the host manually still its not working,

prakashsvmx commented 1 year ago

Also please check the parameters used in S3v4 calculation

pforprince commented 1 year ago

Would it be possible for you to provide me a working example of such scenario? I'm on this for a week

prakashsvmx commented 1 year ago
wget https://dl.min.io/server/minio/release/linux-amd64/minio

chmod +x minio

CI=true MINIO_ROOT_USER=admin MINIO_ROOT_PASSWORD=password ./minio server /tmp/data --console-address ":9001"

Run this on your machine, generate a presigned url and try.

prakashsvmx commented 1 year ago

Here is an example built using SDK. (which also uses presingedUrl for preview ) Please refer to this. So it is more to do with docker/setup/environment/network. (I am not an expert in this).

https://github.com/prakashsvmx/minio-js-react-18-build/blob/7b7fab689784d90979b96c778649f80e9b9f9ee9/src/cmp/ListObjects.js#L223

prakashsvmx commented 1 year ago

Also Have you set MINIO_SERVER_URL env variable?

pforprince commented 1 year ago
MINIO_ROOT_USER = minio 
MINIO_ROOT_PASSWORD = miniosecret
MINIO_SERVER_URL = http://127.0.0.1
MINIO_BROWSER_REDIRECT_URL= http://127.0.0.1

Here's the env file

pforprince commented 1 year ago

image

If I'm using MINIO_SERVER_URL = http://127.0.0.1 then I'm unable to log in in minio console