Thursday, 14 January 2021

boto3: How to interract with DigitalOcean S3 Spaces when CDN is enabled

I'm working with DigitalOcean Spaces (S3 storage protocol) which has enabled CDN.

Any file on s3 can be accessed via direct URL in the given form:

https://my-bucket.fra1.digitaloceanspaces.com/<file_key>

If CDN is enabled, the file can be accessed via additional CDN URL:

https://my-bucket.fra1.cdn.digitaloceanspaces.com/<file_key>

where fra1 is a region_name.

When I'm using boto3 SDK for Python, the file URL is the following (generated by boto3):

https://fra1.digitaloceanspaces.com/my-bucket/<file_key>
# just note that bucket name is no more a domain part!

This format also works fine.

But, if CDN is enabled - file url causes an error:

EndpointConnectionError: Could not connect to the endpoint URL:  https://fra1.cdn.digitaloceanspaces.com/my-bucket/<file_key>

assuming the endpoint_url was changed from

default_endpoint=https://fra1.digitaloceanspaces.com

to

default_endpoint=https://fra1.cdn.digitaloceanspaces.com

How to connect to CDN with proper URL without getting an error? And why boto3 uses different URL format? Is any workaround can be applied in this case?

code:

s3_client = boto3.client('s3',
                            region_name=s3_configs['default_region'],
                            endpoint_url=s3_configs['default_endpoint'],
                            aws_access_key_id=s3_configs['bucket_access_key'],
                            aws_secret_access_key=s3_configs['bucket_secret_key'])

s3_client.download_file(bucket_name,key,local_filepath)

boto3 guide for DigitalOcean Spaces.

Here is what I've also tried but It didn't work:



from boto3: How to interract with DigitalOcean S3 Spaces when CDN is enabled

No comments:

Post a Comment