4

I want to use python to store and download file in minio

Below is the code

from minio import Minio
import os

def getMinioClient(access, secret):
  
  return Minio(
      endpoint="localhost:9000",
      access_key=access,
      secret_key=secret,
      secure=False,
    )

if __name__ == "__main__":
  client = getMinioClient("admin", "Secret_key123")

  try:

    file_name = "myfile.csv"
    bucket = "file_bucket"

    with open(file_name, "rb") as f:
      stat_data = os.stat(file_name)

      # fput_object to upload file
      a = client.fput_object(
            bucket,
            file_name,
            f,
            stat_data.st_size
          )
    print("uploaded")

    # using fget_object to download file
    client.fget_object(bucket, file_name, f"{file_name}_downloaded")

  except Exception as e:
      print(e)

Only option I know to download file is using fget_object

How can I get a link, which upon pasted in url bar get the required file downloaded

Just like link which we get from minio UI, when we click on share of particular file like below

enter image description here

After clicking on share link, a link is generated which can be used to download that file without even logging in. enter image description here

How will I be able to generate the download link from within python code by making connection to minio.

Thanks in advance!

young_minds1
  • 1,181
  • 3
  • 10
  • 25

2 Answers2

1

There is a method named get_presigned_url that create url for deleting, updating or downloading file. See more at https://min.io/docs/minio/linux/developers/python/API.html#get_presigned_url

For downloading purpose:

url = client.get_presigned_url(
    "GET",
    "my-bucket",
    "my-object",
    expires=timedelta(hours=2),
)
minh tri Vo
  • 524
  • 4
  • 11
0

at first you must set public policy to your bucket you can use the following example

policy = {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Principal": {"AWS": "*"},
                        "Action": [
                            "s3:GetBucketLocation",
                            "s3:ListBucket",
                            "s3:ListBucketMultipartUploads",
                        ],
                        "Resource": f"arn:aws:s3:::{self.bucket_name}",
                    },
                    {
                        "Effect": "Allow",
                        "Principal": {"AWS": "*"},
                        "Action": [
                            "s3:GetObject",
                            "s3:PutObject",
                            "s3:DeleteObject",
                            "s3:ListMultipartUploadParts",
                            "s3:AbortMultipartUpload",
                        ],
                        "Resource": f"arn:aws:s3:::{self.bucket_name}/*",
                    },
                ],
            }
            client.set_bucket_policy(self.bucket_name, json.dumps(policy))

and then you can use from bucket name and file name to generate your file path.

client.fput_object(
            bucket_name,
            file_name,
            file.fileno(),
            content_type=file.content_type,
        )

        return f"{self.endpoint}/{bucket_name}/{file_name}"