Skip to content

Instantly share code, notes, and snippets.

@d4vsanchez
Created August 21, 2017 20:47
Show Gist options
  • Save d4vsanchez/c915e690cfc1bee01d97615b454b606f to your computer and use it in GitHub Desktop.
Save d4vsanchez/c915e690cfc1bee01d97615b454b606f to your computer and use it in GitHub Desktop.
Report model example
import StringIO
from django.db import models
from boto3.session import Session
class Report(models.Model):
# ...
# Information about the model
def create_report_and_send_by_email(self):
stream = StringIO.StringIO()
self.create_report(stream)
# Go back to the first bit of the stream
stream.seek(0)
session = Session(aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
s3 = session.resource('s3')
s3.Bucket(AWS_BUCKET_DATA).put_object(
Key='file.pdf',
Body=stream.read(),
ACL='public-read',
Expires=(datetime.datetime.now() + datetime.timedelta(days=1)).strftime('%Y/%m/%d'),
ContentType='application/pdf'
)
# Do some other stuff and send the file by email
# More methods
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment