1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If any one is facing this issues please note , above posted code is working fine, I just did the changes as per @Marcin suggested

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.