Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add presign_url method, allow more aiohttp versions & py3.12 #29

Merged
merged 6 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ jobs:

matrix:
python:
- '3.7'
- '3.8'
- '3.9'
- '3.10'
- '3.11'
- '3.12'

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ async with ClientSession(raise_for_status=True) as session:
async with client.get("bucket/key") as resp:
data = await resp.read()

# Make presigned URL
url = client.presign_url("GET", "bucket/key", expires=60 * 60)

# Delete object using bucket+key
async with client.delete("bucket/key") as resp:
assert resp == HTTPStatus.NO_CONTENT
Expand Down
34 changes: 33 additions & 1 deletion aiohttp_s3_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ async def put_multipart(

log.debug(
"All parts (#%d) of %s are uploaded to %s",
part_no - 1, upload_id, object_name,
part_no - 1, upload_id, object_name, # type: ignore
)

# Parts should be in ascending order
Expand Down Expand Up @@ -794,3 +794,35 @@ async def list_objects_v2(
if not cont_token:
break
params["continuation-token"] = cont_token

def presign_url(
self,
method: str,
url: t.Union[str, URL],
headers: t.Optional[HeadersType] = None,
content_sha256: t.Optional[str] = None,
expires: int = 86400,
) -> URL:
"""
Make presigned url which will expire in specified amount of seconds

method: HTTP method
url: object key or absolute URL
headers: optional headers you would like to pass
content_sha256:
expires: amount of seconds presigned url would be usable
"""
if content_sha256 is None:
content_sha256 = UNSIGNED_PAYLOAD

_url = URL(url)
if not _url.is_absolute():
_url = self._url / str(_url)

return URL(self._credentials.signer.presign_url(
method=method.upper(),
url=str(_url),
headers=headers,
content_hash=content_sha256,
expires=expires
))
1,320 changes: 654 additions & 666 deletions poetry.lock

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ classifiers = [
"Operating System :: Microsoft",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Python :: Implementation :: CPython",
]
Expand All @@ -42,9 +42,8 @@ classifiers = [
aiohttp = "^3.8"
aiomisc = "^17.3.4"
aws-request-signer = "1.2.0"
cached-property = [{ version = '^1.5.2', python = "< 3.8" }]
typing_extensions = [{ version = '*', python = "< 3.10" }]
python = "^3.7"
python = "^3.8"


[tool.poetry.group.dev.dependencies]
Expand All @@ -53,11 +52,14 @@ coverage = "!=4.3"
coveralls = "^3.3.1"
mypy = "*"
pylama = { extras = ["toml"], version = "^8.4.1" }
pytest = "^7.2.1"
pytest-cov = "^4.0.0"
pytest-timeout = "^2.1.0"
tox = "^4.4.6"
pytest-aiohttp = "^1.0.4"
freezegun = "^1.5.0"
pytest = "^7.4.4"
setuptools = "^69.5.1"
pytest-aiohttp = "^1.0.5"
yarl = "^1.9.4"

[build-system]
requires = ["poetry-core"]
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def s3_client(event_loop, s3_url: URL):

@pytest.fixture
async def s3_url() -> URL:
return URL(os.getenv("S3_URL", "http://user:hackme@localhost:8000/"))
return URL(os.getenv("S3_URL", "http://user:hackme@localhost:9090/test"))


@pytest.fixture
Expand Down
62 changes: 62 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pathlib import Path

import pytest
from freezegun import freeze_time
from yarl import URL

from aiohttp_s3_client import S3Client

Expand Down Expand Up @@ -138,3 +140,63 @@ async def async_iterable(iterable: bytes):
# FIXME: uncomment after update fakes3 image
actual = gzip.GzipFile(fileobj=BytesIO(result)).read()
assert actual == data


@pytest.mark.parametrize('method,given_url', [
# Simple test
('GET', 'test/object'),

# Check method name is ok in lowercase
('get', 'test/object'),

# Absolute path
('GET', '/test/object'),

# Check URL object with path is given
('get', URL('./test/object')),

# Check URL object with path is given
('get', URL('/test/object')),
])
def test_presign_non_absolute_url(s3_client, s3_url, method, given_url):
presigned = s3_client.presign_url('get', 'test/object')
assert presigned.is_absolute()
assert presigned.scheme == s3_url.scheme
assert presigned.host == s3_url.host
assert presigned.port == s3_url.port
assert presigned.path == (s3_url / str(given_url).lstrip('/')).path


@pytest.mark.parametrize('method,given_url', [
# String url
('GET', 'https://absolute-url:123/path'),

# URL object
('GET', URL('https://absolute-url:123/path')),
])
def test_presign_absolute_url(s3_client, method, given_url):
presigned = s3_client.presign_url(method, given_url)
assert presigned.is_absolute()

url_object = URL(given_url)
assert presigned.scheme == url_object.scheme
assert presigned.host == url_object.host
assert presigned.port == url_object.port
assert presigned.path == url_object.path


@freeze_time("2024-01-01")
def test_presign_url(s3_client, s3_url):
url = s3_client.presign_url('get', URL('./example'))
assert url.path == (s3_url / 'example').path
assert url.query == {
'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
'X-Amz-Content-Sha256': 'UNSIGNED-PAYLOAD',
'X-Amz-Credential': 'user/20240101/us-east-1/s3/aws4_request',
'X-Amz-Date': '20240101T000000Z',
'X-Amz-Expires': '86400',
'X-Amz-SignedHeaders': 'host',
'X-Amz-Signature': (
'7359f1286edf554b0eab363e3c93ee32855b8d429d975fdb0a5d2cb7ad5c0db0'
)
}
Loading