1

I would like to obtain URLs pointing to cloud-optimized geoTIFFs from Amazon's Copernicus Digital Elevation Model bucket.

After installing boto3 (with pip3 install boto3), I do, relying on this answer to the question Can I use boto3 anonymously? to download a single file:

import boto3
from botocore import UNSIGNED
from botocore.client import Config

s3 = boto3.client('s3', region_name='eu-central-1', config=Config(signature_version=UNSIGNED))

Then I query for list of objects in the bucket, using the second line of this answer to the question Use boto3 to download from public bucket:

objects = s3.list_objects(Bucket='copernicus-dem-30m')

I then access to a value in objects['Contents'], the first one, for example (ie index 0):

key = objects['Contents'][0]['Key']

key is now:

Copernicus_DSM_COG_10_N00_00_E006_00_DEM/Copernicus_DSM_COG_10_N00_00_E006_00_DEM.tif

I download this file by doing:

s3.download_file('copernicus-dem-30m', key, key.split('/')[-1])

Instead of downloading, how can I generate a URL, which later I can use to download the file, maybe using wget or just pasting it to a browswer?


This code shown above is based on the thread: How to get Copernicus DEM GeoTIFFs for a bounding box using Python.

zabop
  • 6,750
  • 3
  • 39
  • 84

2 Answers2

4

See Geoffrey’s answer for the format of the S3 URLs for public access buckets.

To generate a URL that works regardless of whether the bucket/object is public, you can use generate_presigned_url:

s3.generate_presigned_url(
    'get_object',
    Params = {'Bucket': 'copernicus-dem-30m', 'Key': key},
    ExpiresIn = SIGNED_URL_TIMEOUT
)

… with a suitably chosen SIGNED_URL_TIMEOUT (in seconds).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thank you. I get the error: AttributeError: 'S3' object has no attribute 'client' – zabop Sep 29 '21 at 12:23
  • Looking it up... – zabop Sep 29 '21 at 12:23
  • @zabop Sorry, copy&paste error. Fixed now. – Konrad Rudolph Sep 29 '21 at 12:24
  • Thanks @Konrad, works now! :) I realized that the url generated does not in fact depend on SIGNED_URL_TIMEOUT, and I get the same URL if I don't pass any SIGNED_URL_TIMEOUT value. Can accept in 2 mins... – zabop Sep 29 '21 at 12:28
  • 1
    @zabop Ah, yes, I hadn’t paid attention to your specific use-case: your bucket is public so you technically don’t need a pre-signed URL at all. My answer is more general and will also work for private buckets. – Konrad Rudolph Sep 29 '21 at 12:33
3

S3 uses this format: https://<bucket-name>.s3.amazonaws.com/<key>

url = 'https://copernicus-dem-30m.s3.amazonaws.com/' + key

So the example above will look like this: https://copernicus-dem-30m.s3.amazonaws.com/Copernicus_DSM_COG_10_N00_00_E006_00_DEM/Copernicus_DSM_COG_10_N00_00_E006_00_DEM.tif

Geoffrey Burdett
  • 1,906
  • 1
  • 14
  • 26