Last active
July 10, 2019 08:59
-
-
Save hprobotic/16290ff59f35b28fb1f383c489e38777 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 boto3 | |
# other libs import | |
s3 = boto3.client( | |
's3', | |
aws_access_key_id=AWS_ACCESS_KEY, | |
aws_secret_access_key=AWS_SECRET_KEY, | |
region_name=AWS_REGION_NAME | |
) | |
file_extension = file_attachment.filename.rsplit('.', 1)[1].lower() | |
if '.' in file_attachment.filename and not file_extension in ALLOWED_FILE_EXTENSIONS: | |
return { | |
"message": "File extension is not one of our supported types." | |
}, 400 | |
filename = "%s_%s" % (int(time.time()), file_attachment.filename) | |
s3_path = "%s/%s/%s" % (datetime.now().year, | |
datetime.now().strftime('%m'), filename) | |
try: | |
s3.upload_fileobj( | |
file_attachment, S3_BUCKET_ATTACHMENT, s3_path) | |
except Exception as e: | |
return {"message": str(e)}, 402 | |
file_attachment = FileAttachment( | |
user_id=current_user_id, | |
file_name=file_attachment.filename, | |
s3_path=s3_path | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment