Last active
August 29, 2015 14:13
-
-
Save misakwa/55bced19048fd254301b to your computer and use it in GitHub Desktop.
S3 Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import threading | |
import contextlib as ctx | |
import boto.s3 as s3 | |
storage_info = threading.local() | |
DEFAULT_REGION = 'us-east-1' | |
@ctx.contextmanager | |
def storage(key_name): | |
global storage_info | |
try: | |
conn = storage_info.s3_conn | |
if conn is None: | |
raise AttributeError('S3 connection does not exist') | |
except AttributeError: | |
conn = s3.connect_to_region( | |
DEFAULT_REGION, | |
aws_access_key=settings.access_key, | |
aws_secret_key=settings.secret_key | |
) | |
storage_info.s3_conn = conn | |
try: | |
bucket = storage_info.s3_bucket | |
if bucket is None: | |
raise AttributeError('Bucket does not exist') | |
except AttributeError: | |
bucket = conn.get_bucket(settings.bucket_name, validate=False) | |
storage_info.s3_bucket = bucket | |
key = bucket.get_key(key_name, validate=False) | |
yield key | |
# Do some cleanup after use | |
filename = 'linkshare/somefile.csv' | |
# Reading | |
with storage(filename) as key: | |
key.open_read() | |
# Writing | |
with storage(filename) as key: | |
key.set_contents_from_stream(blah) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment