Last active
December 10, 2018 05:46
-
-
Save JosephCastro/7c8467d71d2134e8937a864e50b5aa09 to your computer and use it in GitHub Desktop.
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
import tempfile | |
import boto3 | |
import sys | |
class S3TempFile(object): | |
def __init__(self, bucket, key): | |
self.bucket = bucket | |
self.key = key | |
self.tempfile = tempfile.TemporaryFile() | |
def write(self, content): | |
if sys.version_info[0] < 3: | |
self.tempfile.write(content) | |
else: | |
self.tempfile.write(bytes(content,'utf-8')) | |
def read(self): | |
self.tempfile.seek(0) | |
if sys.version_info[0] < 3: | |
content = self.tempfile.read() | |
else | |
content = self.tempfile.read().decode() | |
return content | |
def get(self): | |
return self.tempfile | |
def download(self): | |
s3_client = boto3.client('s3') | |
s3_object = s3_client.get_object(Bucket=self.bucket, Key=self.key) | |
content = s3_object['Body'].read() | |
self.write(content.decode()) | |
def upload(self): | |
s3_client = boto3.client('s3') | |
self.tempfile.seek(0) | |
s3_client.put_object( | |
Body=self.tempfile, | |
Bucket=self.bucket, | |
Key=self.key) | |
def close(self): | |
self.tempfile.close() |
Author
JosephCastro
commented
Aug 22, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment