Last active
September 15, 2018 19:29
-
-
Save hirenalken/90333b8e9922189ff1bc63f75b6bfdd7 to your computer and use it in GitHub Desktop.
Generate pre-signed s3 bucket post url
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 | |
from rest_framework import status | |
from rest_framework.response import Response | |
from rest_framework.views import APIView | |
class GenerateAwsSignature(APIView): | |
"""Generate an AWS signature for presigned POST requests. | |
This signed data allows file uploads directly from the browser to S3. | |
""" | |
def post(self, request): | |
""" | |
* API to be consumed by fine uploader to upload images directly to s3 bucket. | |
""" | |
s3 = boto3.client( | |
's3', | |
aws_access_key_id='xxxxxxxxxxxxxxx', | |
aws_secret_access_key='xxxxxxxxxxxxxx') | |
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.generate_presigned_post | |
try: | |
data = s3.generate_presigned_post( | |
'fineuploader-demo', | |
'${filename}', | |
Conditions=request.data['conditions'], | |
ExpiresIn=60 * 60, | |
) | |
except KeyError: | |
return Response(zo_diageo_response.generate_failure_response( | |
custom_error_codes.INVALID_DATA_1204), | |
status=status.HTTP_400_BAD_REQUEST) | |
return Response({'policy': data['fields']['policy'], | |
'signature': data['fields']['signature'], | |
'url': data['url']}, | |
status=status.HTTP_200_OK) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment