Naga
Naga

Reputation: 2021

Boto3 + Django + S3 decoded base64 Image Upload not working

I am getting the image in base64 format through rest api

def save_image_to_s3(product_name, product_id, image_base64):
s3 = boto3.resource('s3')
file_name_with_extension = product_name + str(product_id) + 'product.png'
obj = s3.Object(AWS_STORAGE_BUCKET_NAME, file_name_with_extension)
obj.put(Body=base64.b64decode(image_base64))
# get bucket location
location = boto3.client('s3').get_bucket_location(Bucket=AWS_STORAGE_BUCKET_NAME)['LocationConstraint']
# get object url
object_url = "https://%s.s3-%s.amazonaws.com/%s" % (AWS_STORAGE_BUCKET_NAME, location, file_name_with_extension)
print "Printing Stored Url = " + object_url
return object_url

But I am getting below error

EndpointConnectionError: Could not connect to the endpoint URL: "https://bucketname.s3.Asia-Pacific-Mumbai.amazonaws.com/xyz.png"

After debugging I found the error is coming on below line

obj.put(Body=base64.b64decode(image_base64))

My requirement is save the image to S3, get the stored image url and save into database.

Please suggest a way forward

Upvotes: 1

Views: 322

Answers (1)

Marcin
Marcin

Reputation: 238051

This is incorrect url:

https://bucketname.s3.Asia-Pacific-Mumbai.amazonaws.com/xyz.png

Specifically, the region Asia-Pacific-Mumbai should be ap-south-1.

You can review and double check S3 url format here.

Upvotes: 1

Related Questions