Versions Compared

Key

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

...

Code Block
languagejs
{'Key': 'MyFile.txt', 'LastModified': datetime.datetime(2021, 11, 11, 0, 39, 23, 320000, tzinfo=tzlocal()), 'ETag': '"2e22f62675cea3445f7e24818a4f6ba0d6-1"', 'Size': 1013, 'StorageClass': 'STANDARD'}


Case 2: Read objects

Into memory

Now lets try to read a file from a bucket into Python's memory, so we can work with it inside Python without ever saving the file to our local computer:

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

Download objects to a file

But if you'd want to download the file instead of reading it into memory (e.g. so you can use it with other libraries or applications that expect files), here's how you'd do that:

...