python - Unzip my_file.zip pulled from s3 using boto -
i trying use boto open .zip file have in s3. trying work data directly, want avoid creating temporary files.
in [201]: import stringio in [202]: import boto in [203]: conn = boto.connect_s3() in [204]: my_bucket = conn.get_bucket('my_bucket') in [205]: my_list = [ele ele in my_bucket.list('my_file.zip')] in [206]: f = stringio.stringio() in [207]: my_list[0].get_file(f) in [208]: f.seek(0) if file not zipped use:
my_content = my_list[0].get_contents_as_string() but since zipped, getting garbage.
an answer question want (i borrowed bit of attempt it) using gzip, can't find using zip. tried using zipfilezipfile, read, extract , extractall methods don't seem want.
you should python module gzip :
https://docs.python.org/2/library/gzip.html
you should able stringio gzip. .
from boto.s3.connection import s3connection import gzip stringio import stringio s3conn = s3connection() # assuming .boto has been setup bucket = s3conn.get_bucket('my_bucket') my_list = [gzip.gzipfile(fileobj=(stringio(ele.get_contents_as_string()))) ele in bucket.list()] #for readability pulled out item in my_list: item.read() for readability list comprehension should broken - followed original posting compare.
good luck!
Comments
Post a Comment