Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
#List the objects in our bucket
response = s3.list_objects(Bucket=bucketnameS3_BUCKET_NAME)
for item in response['Contents']:
    print(item['Key'])

...

Code Block
languagepy
#List objects with paginator (not constrained to a 1000 objects)
paginator = s3.get_paginator('list_objects_v2')
pages = paginator.paginate(Bucket=bucketnameS3_BUCKET_NAME)

#Lets store the names of our objects inside a list
objects = []
for page in pages:
    for obj in page['Contents']:
        objects.append(obj["Key"])

print('Number of objects: ', len(objects))

...

Code Block
languagepy
#Read a file into Python's memory and open it as a string
filenameFILENAME = '/folder1/folder2/myfile.txt'  #Fill this in  
obj = s3.get_object(Bucket=bucketnameS3_BUCKET_NAME, Key=filenameFILENAME)
myObject = obj['Body'].read().decode('utf-8') 
print(myObject)

...

Code Block
languagepy
# Downloading a file from the bucket
with open('myfile', 'wb') as f:  #Fill this in  
    s3.download_fileobj(bucketnameS3_BUCKET_NAME, 'myfile', f) 


Upload objects

...

Code Block
languagepy
#Uploading a file to the bucket (make sure you have write access)
response = s3.upload_file('myfile', bucketnameS3_BUCKET_NAME, 'myfile')  #Fill this in  

...