· 7 years ago · Oct 05, 2018, 06:42 AM
1# Example to read contents of a file from an S3 bucket
2# For this example there is a file named techcrunch_scrape_last_date.txt
3# in the rehinged-news S3 bucket that contains a single date value.
4# We want to read this date into a variable named last_date in python
5
6import boto3
7import botocore
8import boto.s3.connection
9import tempfile
10
11#AWS access info
12access_key = 'AWS Access Key'
13secret_key = 'AWS Secret Key'
14
15#AWS bucket name and file to retrieve
16#Include the path to the object if it is sub directories
17aws_bucket_name = 'my-bucket'
18aws_bucket_object_name = 'dir1/dir2/file_name.txt'
19
20#Open session to S3
21session = boto3.Session(aws_access_key_id=access_key,aws_secret_access_key=secret_key)
22
23#Setup to retrieve the file (object) from S3
24s3 = session.resource('s3')
25bucket = s3.Bucket(aws_bucket_name)
26object = bucket.Object(aws_bucket_object_name)
27tmp = tempfile.NamedTemporaryFile()
28
29#Download the file, extract the contents, assign to variable, print value
30with open(tmp.name, 'wb') as f:
31 object.download_fileobj(f)
32 data = object.get().get('Body').read().decode('utf-8')
33 #last_date = str(data.decode(“utf-8â€))
34 last_date = str(data.strip("'"))
35 print(last_date)