Skip to content

Commit afe73c1

Browse files
committed
feat: allow buckets without auth
As discussed at #94
1 parent 658f606 commit afe73c1

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ with \
175175
print(row)
176176
```
177177

178+
179+
### Public Buckets
180+
181+
For public buckets where credentials must not be passed, pass `None` as the `get_credentials` parameter.
182+
183+
```python
184+
query_my_db = partial(sqlite_s3_query,
185+
url='https://my-public-bucket.s3.eu-west-2.amazonaws.com/my-db.sqlite',
186+
get_credentials=None,
187+
)
188+
189+
with \
190+
query_my_db() as query, \
191+
query('SELECT * FROM my_table_2 WHERE my_col = ?', params=('my-value',)) as (columns, rows):
192+
193+
for row in rows:
194+
print(row)
195+
```
196+
197+
178198
### HTTP Client
179199

180200
The HTTP client can be changed by overriding the the default `get_http_client` parameter, which is shown below.

sqlite_s3_query.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ def sqlite_s3_query_multi(url, get_credentials=lambda now: (
7474
local = threading.local()
7575
local.pending_exception = None
7676

77+
def get_request_headers_for_private_buckets(method, params, headers, now):
78+
region, access_key_id, secret_access_key, session_token = get_credentials(now)
79+
to_auth_headers = headers + (
80+
(('x-amz-security-token', session_token),) if session_token is not None else \
81+
()
82+
)
83+
return aws_sigv4_headers(
84+
now, access_key_id, secret_access_key, region, method, to_auth_headers, params,
85+
)
86+
87+
def get_request_headers_for_public_buckets(_, __, headers, ___):
88+
return headers
89+
90+
get_request_headers = \
91+
get_request_headers_for_private_buckets if get_credentials is not None else \
92+
get_request_headers_for_public_buckets
93+
7794
def set_pending_exception(exception):
7895
local.pending_exception = exception
7996

@@ -98,15 +115,8 @@ def run_with_db(db, func, *args):
98115
@contextmanager
99116
def make_auth_request(http_client, method, params, headers):
100117
now = datetime.utcnow()
101-
region, access_key_id, secret_access_key, session_token = get_credentials(now)
102-
to_auth_headers = headers + (
103-
(('x-amz-security-token', session_token),) if session_token is not None else \
104-
()
105-
)
106-
request_headers = aws_sigv4_headers(
107-
now, access_key_id, secret_access_key, region, method, to_auth_headers, params,
108-
)
109118
url = f'{scheme}://{netloc}{path}'
119+
request_headers = get_request_headers(method, params, headers, now)
110120
with http_client.stream(method, url, params=params, headers=request_headers) as response:
111121
response.raise_for_status()
112122
yield response

0 commit comments

Comments
 (0)