Skip to content

Instantly share code, notes, and snippets.

@ross-humphrey
Last active August 19, 2019 09:14
Show Gist options
  • Save ross-humphrey/c7e0dd79afa449b63e59f51727e8e8e3 to your computer and use it in GitHub Desktop.
Save ross-humphrey/c7e0dd79afa449b63e59f51727e8e8e3 to your computer and use it in GitHub Desktop.
🐍 Python - Retrieve Public URL From AWS S3 Object πŸ’»
import boto3
def retrieve_public_url(s3_bucket_name, s3_file_name):
"""
Given a s3 bucket name and key this method will figure out the location of the bucket
and the respective url
:param s3_bucket_name: S3 bucket name
:param s3_file_name: S3 file name
:return: formatted object URL giving publicly accessible URL for object (presuming permissions are set
accordingly)
"""
bucket_location = boto3.client('s3').get_bucket_location(Bucket=s3_bucket_name)
object_url = "https://s3-{0}.amazonaws.com/{1}/{2}".format(
bucket_location['LocationConstraint'],
s3_bucket_name,
s3_file_name)
return object_url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment