Skip to content

Commit

Permalink
Updated S3 provider to be more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed Nov 12, 2021
1 parent 924e5e8 commit 825767a
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ __pycache__/

# Distribution / packaging
.Python
env/
.venv/
build/
develop-eggs/
dist/
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.3.0
-----

- Breaking change: removed Onesignal module
- Extended S3 `s3.new()` interface with `endpoint` argument

0.2.8
-----

Expand Down
2 changes: 1 addition & 1 deletion signa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .core import new, Factory

__version__ = '0.2.8'
__version__ = '0.3.0'
17 changes: 6 additions & 11 deletions signa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
dospaces,
yaobject,
oss,

# TODO: why is it here, haha?
onesignal
)

PROVIDERS = {
Expand All @@ -15,9 +12,6 @@
'dospaces': dospaces.new,
'yaobject': yaobject.new,
'oss': oss.new,

# TODO: remove after deprecation cycle
'onesignal': onesignal.new,
}


Expand All @@ -42,15 +36,15 @@ def new(self, **kwargs):


if __name__ == '__main__':
import yaml
import os
import json
import requests
from dotenv import load_dotenv, find_dotenv

with open('config.yml', 'r') as f:
config = yaml.load(f)
load_dotenv(find_dotenv())

access_key = config['s3']['access_key']
secret_key = config['s3']['secret_key']
access_key = os.environ['AWS_ACCESS_KEY_ID']
secret_key = os.environ['AWS_SECRET_ACCESS_KEY']
signed = new(
's3',
method='PUT',
Expand All @@ -70,6 +64,7 @@ def new(self, **kwargs):
print('\n')

r = requests.put(signed['url'], headers=signed['headers'], data=b'xxxxxxxx')
r.raise_for_status()
print(r.text)
print('\n')

Expand Down
1 change: 0 additions & 1 deletion signa/providers/aws.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
import hashlib
import hmac
import json
import urllib.parse
from signa.logger import get_logger

Expand Down
6 changes: 2 additions & 4 deletions signa/providers/b2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from .aws import aws_headers

REGIONS = {
'us-west-000', 'us-west-001', 'us-west-002',
# TODO: add European region
'us-west-000', 'us-west-001', 'us-west-002', 'eu-central-003'
}

def new(method=None, region=None, bucket=None, key=None,
auth=None, headers=None, payload=None):
headers = headers.copy() if headers else {}

assert region in REGIONS

# assert region in REGIONS
# headers['host'] = '%s.s3.%s.backblazeb2.com' % (bucket, region)

# https://s3.<region>.backblazeb2.com/<bucket>
Expand Down
32 changes: 13 additions & 19 deletions signa/providers/dospaces.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
from .aws import aws_headers
from typing import Any, Dict, Optional
from . import s3


def new(method=None, region=None, bucket=None, key=None,
auth=None, headers=None, payload=None):
headers = headers.copy() if headers else {}

headers['host'] = '%s.%s.digitaloceanspaces.com' % (bucket, region)

rel_uri = ('/%s' % key) if key else '/'

headers.update(aws_headers(
def new(method: str, region: str = '', bucket: str = '',
key: str = '', auth: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str, str]] = None,
payload: Any = None,
) -> Dict[str, str]:
return s3.new(
method=method,
region=region,
service='s3',
uri=rel_uri,
bucket=bucket,
key=key,
auth=auth,
headers=headers,
payload=payload
))

return {
'url': 'https://%s%s' % (headers['host'], rel_uri),
'headers': headers,
}
payload=payload,
endpoint='{region}.digitaloceanspaces.com'
)
19 changes: 0 additions & 19 deletions signa/providers/onesignal.py

This file was deleted.

32 changes: 13 additions & 19 deletions signa/providers/oss.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
from .aws import aws_headers
from typing import Any, Dict, Optional
from . import s3


def new(method=None, region=None, bucket=None, key=None,
auth=None, headers=None, payload=None):
headers = headers.copy() if headers else {}

headers['host'] = '%s.%s.aliyuncs.com' % (bucket, region)

rel_uri = ('/%s' % key) if key else '/'

headers.update(aws_headers(
def new(method: str, region: str = '', bucket: str = '',
key: str = '', auth: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str, str]] = None,
payload: Any = None,
) -> Dict[str, str]:
return s3.new(
method=method,
region=region,
service='s3',
uri=rel_uri,
bucket=bucket,
key=key,
auth=auth,
headers=headers,
payload=payload
))

return {
'url': 'https://%s%s' % (headers['host'], rel_uri),
'headers': headers,
}
payload=payload,
endpoint='{region}.aliyuncs.com'
)
35 changes: 27 additions & 8 deletions signa/providers/s3.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
from typing import Any, Dict, Optional
from .aws import aws_headers


def new(method=None, region=None, bucket=None, key=None,
auth=None, headers=None, payload=None):
def new(method: str, region: str = '', bucket: str = '',
key: str = '', auth: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str, str]] = None,
payload: Any = None,
endpoint:str = 's3.{region}.amazonaws.com'
) -> Dict[str, str]:
"""
Create new signature for S3 request.
Returns
-------
Dictionary with `url` and `headers` keys.
"""
headers = headers.copy() if headers else {}

if endpoint and region:
endpoint = endpoint.format(region=region)

# Details on buckets URLs:
# https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html
headers['host'] = '%s.s3.%s.amazonaws.com' % (bucket, region)
# https://aws.amazon.com/blogs/aws/amazon-s3-path-deprecation-plan-the-rest-of-the-story/
headers['host'] = '{bucket}.{endpoint}'.format(
bucket=bucket, endpoint=endpoint
)

rel_uri = ('/%s' % key) if key else '/'
rel_uri = '/{key}'.format(key=(key or '')).strip()

headers.update(aws_headers(
method=method,
Expand All @@ -21,7 +39,8 @@ def new(method=None, region=None, bucket=None, key=None,
payload=payload
))

return {
'url': 'https://%s%s' % (headers['host'], rel_uri),
'headers': headers,
}
url = 'https://{host}{rel_uri}'.format(
host=headers['host'], rel_uri=rel_uri
)

return {'url': url, 'headers': headers}
8 changes: 3 additions & 5 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def tearDown(self):

def test_new_s3(self):
key = 'test.txt'
_ = signa.new(
result = signa.new(
aws_s3_provider,
method='PUT',
region=aws_region,
Expand All @@ -38,7 +38,5 @@ def test_new_s3(self):
'access_key': aws_access_key,
'secret_key': aws_secret_key,
})
self.assertTrue(True)

def test_new_onesignal(self):
self.assertTrue(True)
self.assertTrue('url' in result)
self.assertTrue('headers' in result)

0 comments on commit 825767a

Please sign in to comment.