首页 > 解决方案 > 在 python 中为 S3 文件夹创建预签名 URL

问题描述

我正在尝试为 S3 文件夹(它本身包含更多文件夹/文件)生成一个预签名的 url,并将其分发给我的客户,以便他们可以下载其内容。即通过单击链接,用户将文件夹下载到他们的本地磁盘。

但是,我在 XML 对话中不断收到“没有这样的键”错误。

我正在使用来自 boto3 sdk 的 client.generate_presigned_url()

def create_presigned_url(bucket, object):
    try:
        url = s3_client.generate_presigned_url(
            'get_object',
            Params={
                'Bucket': bucket,
                'Key': object
            },
            ExpiresIn=240,
            HttpMethod='GET'
        )
    except ClientError as e:
        print(e)
        return None
    return url

这是错误消息:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
   <Code>NoSuchKey</Code>
      <Message>The specified key does not exist.</Message>
         <Key>output/BARNES/070419/APR19BAR/</Key>
         <RequestId>E6BE736FE945FA22</RequestId>
         <HostId>
      hk3+d+***********************************************************+EO2CZmo=
          </HostId>
</Error>

标签: python-3.xamazon-web-servicesurlamazon-s3boto3

解决方案


S3 没有“文件夹”的概念。您在这里有效地尝试做的是为多个键创建一个预签名的 url,这也是不可能的。如果您绝对必须为多个文件共享单个 url,则需要将它们压缩到单个对象中,然后使用预签名 url 共享该对象的密钥。


推荐阅读