Skip to content

Commit

Permalink
switch to s3
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanJField committed Feb 9, 2024
1 parent e99b9c7 commit 9c915cc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2,109 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/fair-data-registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,12 @@ jobs:
else
brew install graphviz
fi
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Run Tests
run: |
poetry install
source $VENV
python -m pip install -r local-requirements.txt
DJANGO_SETTINGS_MODULE=drams.test-settings coverage run --omit=drams,scripts,tools manage.py test
- name: Generate XML
run: poetry run coverage xml
run: coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
51 changes: 22 additions & 29 deletions data_management/object_storage.py
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
1 change: 1 addition & 0 deletions local-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ setuptools>54.2
whitenoise==5.2.0
coverage
rocrate==0.7.0
boto3>=1.24
Loading

0 comments on commit 9c915cc

Please sign in to comment.