close

[Solved] Python 3 Boto 3, AWS S3: Get object URL

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python 3 Boto 3, AWS S3: Get object URL in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python 3 Boto 3, AWS S3: Get object URL Error Occurs?

Today I get the following error Python 3 Boto 3, AWS S3: Get object URL in python.

How To Solve Python 3 Boto 3, AWS S3: Get object URL Error ?

  1. How To Solve Python 3 Boto 3, AWS S3: Get object URL Error ?

    To Solve Python 3 Boto 3, AWS S3: Get object URL Error 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:

  2. Python 3 Boto 3, AWS S3: Get object URL

    To Solve Python 3 Boto 3, AWS S3: Get object URL Error 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:

Solution 1

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)

Solution 2

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}'

With quoted key :

url = f'''https://{bucket}.s3.amazonaws.com/{urllib.parse.quote(key, safe="~()*!.'")}'''

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read