生成一个链接,该链接从Python中的Minio下载文件

发布于 2025-02-11 02:38:29 字数 1306 浏览 1 评论 0原文

我想使用python存储和下载文件在下面的minio中

代码

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)

是我知道下载文件的唯一

选项是fget_object我如何获得一个链接,在url bar粘贴后,该链接获取所需的文件下载

就像我们从Minio UI获得的链接一样,当我们单击特定文件的共享时,如下面的

单击共享链接后,生成了一个链接,可用于下载该文件而无需登录即可下载该文件。

我将如何通过与Minio建立连接来从Python代码中生成下载链接。

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

去了角落 2025-02-18 02:38:29

有一个名为get_presigned_url的方法,该方法创建用于删除,更新或下载文件的URL。请参阅 /linux/developers/python/api.html#get_presigned_url

用于下载目的:

url = client.get_presigned_url(
    "GET",
    "my-bucket",
    "my-object",
    expires=timedelta(hours=2),
)

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),
)
百合的盛世恋 2025-02-18 02:38:29

首先,您必须将公共政策设置为您的存储桶
您可以使用以下示例

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))

,然后可以使用从存储桶名和文件名来生成文件路径。

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

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

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}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文