-
-
Save Akohrr/40bde515d68710d8a1c5127ca4b4a686 to your computer and use it in GitHub Desktop.
Django/DRF File Uploading to S3
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
const notifyDjango = (url) => { | |
// Record the URL of the file you've uploaded along with any data | |
// that is relevent to you. | |
} | |
const uploadToS3 = (file, url) => { | |
// Upload the file here | |
// See https://git.io/fhIz5 as a great example of handling all S3 upload edge cases. | |
} | |
const getSignedUrl = (file) => { | |
const params = { | |
objectName: file.name, | |
contentType: file.type | |
}; | |
client.get('/s3/signed-url/', { params }) | |
.then(data => { | |
uploadToS3(file, data).then(i => { | |
// if success, record the new URL | |
notifyDjango(data) | |
}) | |
}) | |
.catch(error => { | |
console.error(error); | |
}); | |
} |
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
from botocore.exceptions import ParamValidationError | |
from django.http import JsonResponse | |
def get_signed_url(request, *args, **kwargs): | |
object_name = request.GET.get('objectName') | |
content_type = request.GET.get('contentType') | |
try: | |
url = conn.generate_presigned_url( | |
ClientMethod='put_object', | |
Params={ | |
'Bucket': settings.S3_UPLOAD_BUCKET, | |
'Key': object_name, | |
'ContentType': content_type, | |
'ACL': 'public-read', | |
}, | |
ExpiresIn=3600 | |
) | |
return JsonResponse({'signedUrl': url}) | |
except ParamValidationError: | |
return JsonResponse({'error': 'Invalid Input'}, status=400) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment