Last active
August 19, 2019 09:14
-
-
Save ross-humphrey/c7e0dd79afa449b63e59f51727e8e8e3 to your computer and use it in GitHub Desktop.
π Python - Retrieve Public URL From AWS S3 Object π»
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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