-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e99b9c7
commit 9c915cc
Showing
6 changed files
with
26 additions
and
2,109 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,26 @@ | ||
from hashlib import sha1 | ||
import hmac | ||
import time | ||
import boto3 | ||
|
||
from django.conf import settings | ||
|
||
def create_url(path, method, filename=None): | ||
def create_url(name, method, filename = None): | ||
bucket = settings.BUCKETS['default'] | ||
expiry_time = int(time.time() + int(bucket['duration'])) | ||
path = bucket['bucket_name'] + '/' + path | ||
if method == 'GET': | ||
hmac_body = '%s\n%s\n%s' % ('GET', expiry_time, path) | ||
elif method == 'PUT': | ||
hmac_body = '%s\n%s\n%s' % ('PUT', expiry_time, path) | ||
sig = hmac.new(bucket['secret_key'].encode('utf-8'), hmac_body.encode('utf-8'), sha1).hexdigest() | ||
url = '%s%s?temp_url_sig=%s&temp_url_expires=%d' % (bucket['url'], path, sig, expiry_time) | ||
if filename: | ||
url = '%s&filename=%s' % (url, filename) | ||
return url | ||
|
||
def create_legacy_url(path, method, filename=None): | ||
bucket = settings.BUCKETS['default'] | ||
expiry_time = int(time.time() + int(bucket['duration'])) | ||
path = bucket['bucket_name'] + '/' + path | ||
if method == 'GET': | ||
hmac_body = '%s\n%s\n%s' % ('GET', expiry_time, path) | ||
elif method == 'PUT': | ||
hmac_body = '%s\n%s\n%s' % ('PUT', expiry_time, path) | ||
sig = hmac.new(bucket['secret_key'].encode('utf-8'), hmac_body.encode('utf-8'), sha1).hexdigest() | ||
url = '%s%s?temp_url_sig=%s&temp_url_expires=%d' % (bucket['url'], path, sig, expiry_time) | ||
if filename: | ||
url = '%s&filename=%s' % (url, filename) | ||
return url | ||
session = boto3.session.Session() | ||
s3_client = session.client( | ||
service_name= 's3', | ||
aws_access_key_id= bucket['access_key'], | ||
aws_secret_access_key= bucket['secret_key'], | ||
endpoint_url= bucket['url'], | ||
) | ||
if method == "GET": | ||
response = s3_client.generate_presigned_url('get_object', | ||
Params={'Bucket': bucket['bucket_name'], | ||
'Key': name, | ||
'ResponseContentDisposition': f'attachment; filename = {filename}]',}, | ||
ExpiresIn=bucket['duration']) | ||
else: | ||
response = s3_client.generate_presigned_url('put_object', | ||
Params={'Bucket': bucket['bucket_name'], | ||
'Key': name}, | ||
ExpiresIn=bucket['duration']) | ||
|
||
return response |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ setuptools>54.2 | |
whitenoise==5.2.0 | ||
coverage | ||
rocrate==0.7.0 | ||
boto3>=1.24 |
Oops, something went wrong.