Python 3 Boto 3, AWS S3: Get object URL

后端 未结 4 2115
Happy的楠姐
Happy的楠姐 2020-12-19 00:38

I need to retrieve an public object URL directly after uploading a file, this to be able to store it in a database. This is my upload code:

   s3 = boto3.res         


        
相关标签:
4条回答
  • 2020-12-19 00:57

    Concatenating the the raw key will fail for some special characters in the key(ex: '+'), you have to quote them:

    url = "https://s3-%s.amazonaws.com/%s/%s" % (
        location,
        bucket_name,
        urllib.parse.quote(key, safe="~()*!.'"),
    )
    

    Or you can call:

    my_config = Config(signature_version = botocore.UNSIGNED)
    url = boto3.client("s3", config=my_config).generate_presigned_url(
        "get_object", ExpiresIn=0, Params={"Bucket": bucket_name, "Key": key}
    )
    

    ...as described here.

    0 讨论(0)
  • 2020-12-19 00:58

    There's no simple way but you can construct the URL from the region where the bucket is located (get_bucket_location), the bucket name and the storage key:

    bucket_name = "my-aws-bucket"
    key = "upload-file"
    
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(bucket_name)
    bucket.upload_file("upload.txt", key)
    location = boto3.client('s3').get_bucket_location(Bucket=bucket_name)['LocationConstraint']
    url = "https://s3-%s.amazonaws.com/%s/%s" % (location, bucket_name, key)
    
    0 讨论(0)
  • 2020-12-19 01:02

    Just a small note. The function call

    location = 
        boto3.client('s3').get_bucket_location(Bucket=bucket_name['LocationConstraint']
    

    may return location = None if the bucket is in the region 'us-east-1'. Therefore, I'd amend the above answer and add a line below that line:

    if location == None: location = 'us-east-1'
    
    0 讨论(0)
  • 2020-12-19 01:11

    Since 2010 you can use a virtual-hosted style S3 url, i.e. no need to mess with region specific urls:

    url = f'https://{bucket}.s3.amazonaws.com/{key}'
    

    Moreover, support for the path-style model (region specific urls) continues for buckets created on or before September 30, 2020. Buckets created after that date must be referenced using the virtual-hosted model.

    See also this blog post.

    0 讨论(0)
提交回复
热议问题