Apollys supports Monica
Apollys supports Monica

Reputation: 3258

AWS S3 - What to set KEY to?

I have been given instructions to download data from s3://x.y.z/ using python, where x, y, z are placeholders of course.

I've gotten myself set up to run the following code

import boto3
import botocore

BUCKET_NAME = 'my-bucket' # replace with your bucket name
KEY = '???' # replace with your object key
LOCAL_TARGET = 'some.data'

s3 = boto3.resource('s3')

try:
    s3.Bucket(BUCKET_NAME).download_file(KEY, LOCAL_TARGET)
except botocore.exceptions.ClientError as e:
    if e.response['Error']['Code'] == "404":
        print("The object does not exist.")
    else:
        raise

but all my attempts at guessing what to set BUCKET and KEY to have resulted in a 404 error.

What would the appropriate values, given the following information?

The data has been uploaded to s3://x.y.z/

Upvotes: 0

Views: 536

Answers (1)

Robert I
Robert I

Reputation: 1527

The key will be the filename you want to download for example

http://s3.amazonaws.com/goat-bucket/farms/andys/goat.png

bucket: goat-bucket

key: farms/andys/goat.png

If you don't know the object name, you can list out the object names of goat bucket like so:

import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket('goat-bucket') # Using the bucket name above
for object in bucket.objects.all():
    print('Bucket name: ' + object.bucket_name)
    print('Object key: ' + object.key)

Would output:

Bucket name: goat-bucket
Object key: farms/andys/goat.png

Upvotes: 2

Related Questions