Skip to content

Instantly share code, notes, and snippets.

@JosephCastro
Last active December 10, 2018 05:46
Show Gist options
  • Save JosephCastro/7c8467d71d2134e8937a864e50b5aa09 to your computer and use it in GitHub Desktop.
Save JosephCastro/7c8467d71d2134e8937a864e50b5aa09 to your computer and use it in GitHub Desktop.
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()
@JosephCastro
Copy link
Author

# If the file is in the bucket
my_file = S3TempFile('my-bucket', 'key/destiny/file.txt')
my_file.download()
print(my_file.read())

# If you want to create the file in s3
my_file = S3TempFile('my-bucket', 'key/destiny/file.txt')
my_file.write('hello world!')
my_file.upload()

# At the end remember close the file, the GC will do for you, but it's a good practice
my_file.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment