2

I am uploading a file to AWS S3 using AWS Lambda function (Python3.8) with the following code.

   file_obj = open(filename, 'rb')

   s3_upload = s3.put_object( Bucket="aaa", Key="aaa.png", Body=file_obj)
   
   return {
   'statusCode': 200,
   'body': json.dumps("Executed Successfully")
   }

I want to get the location/url of the S3 object in return. In Node.js we use the .location parameter for getting the object location/url.

Any idea how to do this using python 3.8?

1 Answer 1

3

The url of S3 objects has known format and follows Virtual hosted style access:

https://bucket-name.s3.Region.amazonaws.com/keyname

Thus, you can construct the url yourself:

bucket_name = 'aaa'
aws_region = boto3.session.Session().region_name
object_key = 'aaa.png'

s3_url = f"https://{bucket_name}.s3.{aws_region}.amazonaws.com/{object_key}"

return {
   'statusCode': 200,
   'body': json.dumps({'s3_url': s3_url})
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's returning null
It was some issue on my end. It's working fine now. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.