Created
August 21, 2017 20:47
-
-
Save d4vsanchez/c915e690cfc1bee01d97615b454b606f to your computer and use it in GitHub Desktop.
Report model example
This file contains hidden or 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 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