-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy paths3_store_private_data.py
More file actions
executable file
·32 lines (24 loc) · 1.01 KB
/
Copy paths3_store_private_data.py
File metadata and controls
executable file
·32 lines (24 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import boto
def store_private_data(bucket_name, key_name, path_to_file):
"""
Write the contents of a local file to S3 and also store custom
metadata with the object.
bucket_name The name of the S3 Bucket.
key_name The name of the object containing the data in S3.
path_to_file Fully qualified path to local file.
"""
s3 = boto.connect_s3()
bucket = s3.lookup(bucket_name)
# Get a new, blank Key object from the bucket. This Key object only
# exists locally until we actually store data in it.
key = bucket.new_key(key_name)
# First let's demonstrate how to write string data to the Key
data = 'This is the content of my key'
key.set_contents_from_string(data)
# Now fetch the data from S3 and compare
stored_key = bucket.lookup(key_name)
stored_data = stored_key.get_contents_as_string()
assert stored_data == data
# Now, overwrite the data with the contents of the file
key.set_contents_from_filename(path_to_file)
return key