s3func 0.8.4__tar.gz → 0.9.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {s3func-0.8.4 → s3func-0.9.0}/PKG-INFO +35 -30
- {s3func-0.8.4 → s3func-0.9.0}/README.md +34 -29
- {s3func-0.8.4 → s3func-0.9.0}/s3func/__init__.py +1 -1
- {s3func-0.8.4 → s3func-0.9.0}/s3func/b2.py +30 -5
- s3func-0.9.0/s3func/locking.py +447 -0
- {s3func-0.8.4 → s3func-0.9.0}/s3func/response.py +19 -1
- {s3func-0.8.4 → s3func-0.9.0}/s3func/s3.py +89 -34
- {s3func-0.8.4 → s3func-0.9.0}/s3func/signer.py +4 -2
- s3func-0.9.0/s3func/tests/test_lock_model.py +427 -0
- {s3func-0.8.4 → s3func-0.9.0}/s3func/tests/test_locks.py +20 -0
- {s3func-0.8.4 → s3func-0.9.0}/s3func/tests/test_mock_s3.py +35 -0
- {s3func-0.8.4 → s3func-0.9.0}/s3func/tests/test_s3func.py +22 -0
- s3func-0.8.4/s3func/locking.py +0 -324
- {s3func-0.8.4 → s3func-0.9.0}/.gitignore +0 -0
- {s3func-0.8.4 → s3func-0.9.0}/LICENSE +0 -0
- {s3func-0.8.4 → s3func-0.9.0}/pyproject.toml +0 -0
- {s3func-0.8.4 → s3func-0.9.0}/s3func/http_url.py +0 -0
- {s3func-0.8.4 → s3func-0.9.0}/s3func/tests/__init__.py +0 -0
- {s3func-0.8.4 → s3func-0.9.0}/s3func/tests/conftest.py +0 -0
- {s3func-0.8.4 → s3func-0.9.0}/s3func/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: s3func
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.9.0
|
|
4
4
|
Summary: Simple functions for working with S3
|
|
5
5
|
Project-URL: Documentation, https://mullenkamp.github.io/s3func/
|
|
6
6
|
Project-URL: Source, https://github.com/mullenkamp/s3func
|
|
@@ -15,7 +15,7 @@ Description-Content-Type: text/markdown
|
|
|
15
15
|
# s3func
|
|
16
16
|
|
|
17
17
|
<p align="center">
|
|
18
|
-
<em>Simple functions for working with S3
|
|
18
|
+
<em>Simple functions for working with S3-compatible object storage</em>
|
|
19
19
|
</p>
|
|
20
20
|
|
|
21
21
|
[](https://github.com/mullenkamp/s3func/actions)
|
|
@@ -24,13 +24,13 @@ Description-Content-Type: text/markdown
|
|
|
24
24
|
|
|
25
25
|
---
|
|
26
26
|
|
|
27
|
-
`s3func` is a lightweight Python library providing a simplified interface for interacting with S3-compatible object storage services
|
|
27
|
+
`s3func` is a lightweight Python library providing a simplified interface for interacting with S3-compatible object storage services (AWS S3, Backblaze B2, MEGA S4, and others). It removes the `boto3` dependency in favor of a fast, `urllib3`-based client with custom SigV4 signing.
|
|
28
28
|
|
|
29
29
|
## Key Features
|
|
30
30
|
|
|
31
31
|
- **Zero Boto3 Dependency**: Minimal overhead and faster imports.
|
|
32
|
-
- **
|
|
33
|
-
- **Distributed Locking**:
|
|
32
|
+
- **Provider-Agnostic**: One `S3Session` for AWS S3 and any S3-compatible provider (Backblaze B2, MEGA S4, ...), with provider quirks handled portably (strict RFC3986 signing of paths and queries).
|
|
33
|
+
- **Distributed Locking**: Verified shared/exclusive locking on plain object storage - no CAS required (see [docs/locking.md](docs/locking.md)).
|
|
34
34
|
- **Streaming Support**: Efficiently stream large objects.
|
|
35
35
|
- **Automatic Retries**: Built-in adaptive retry logic for transient network issues.
|
|
36
36
|
|
|
@@ -93,24 +93,6 @@ resp = session.head_object('data.csv')
|
|
|
93
93
|
print(resp.metadata['processed']) # 'false'
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
-
### Backblaze B2 Operations
|
|
97
|
-
|
|
98
|
-
`s3func` uses the B2 native API for better performance and reliability on Backblaze.
|
|
99
|
-
|
|
100
|
-
```python
|
|
101
|
-
from s3func import B2Session
|
|
102
|
-
|
|
103
|
-
# Initialize B2 session
|
|
104
|
-
session = B2Session(
|
|
105
|
-
access_key_id='YOUR_APPLICATION_KEY_ID',
|
|
106
|
-
access_key='YOUR_APPLICATION_KEY',
|
|
107
|
-
bucket='my-b2-bucket'
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
# Put object
|
|
111
|
-
session.put_object('data.json', b'{"status": "ok"}', content_type='application/json')
|
|
112
|
-
```
|
|
113
|
-
|
|
114
96
|
### Distributed Locking
|
|
115
97
|
|
|
116
98
|
`s3func` provides a powerful distributed lock that mimics Python's `threading.Lock` API.
|
|
@@ -134,17 +116,40 @@ if lock.acquire(blocking=True, timeout=10):
|
|
|
134
116
|
## Performance Tips
|
|
135
117
|
|
|
136
118
|
- **Streaming**: Set `stream=True` in the session (default) or individual requests to handle large files without loading them entirely into memory.
|
|
137
|
-
- **B2 Authorization Caching**: `s3func` automatically caches B2 authorization tokens globally for up to 1 hour, significantly reducing latency for concurrent operations.
|
|
138
119
|
- **Adaptive Retries**: The library uses `urllib3` retry logic configured for high-concurrency environments to handle rate limiting and network blips automatically.
|
|
139
120
|
|
|
140
121
|
## How Distributed Locking Works
|
|
141
122
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
123
|
+
Full walk-through with diagrams: [docs/locking.md](docs/locking.md).
|
|
124
|
+
|
|
125
|
+
The lock is a Lamport-bakery-style election over plain object storage (no
|
|
126
|
+
compare-and-swap needed):
|
|
127
|
+
|
|
128
|
+
1. **Acquisition**: A worker writes two small ticket objects (`seq-0` and `seq-1`).
|
|
129
|
+
2. **Self-visibility gate (0.9.0)**: it polls the listing until its OWN ticket is
|
|
130
|
+
visible - a listing that cannot show your own writes cannot be trusted to show
|
|
131
|
+
competitors (raises after `visibility_timeout`, default 30s).
|
|
132
|
+
3. **Election**: it lists all tickets and yields to older ones (`seq-1` timestamp;
|
|
133
|
+
lexicographic `lock_id` breaks ties). Shared tickets yield only to older
|
|
134
|
+
exclusive tickets.
|
|
135
|
+
4. **Confirming re-list (0.9.0)**: winning requires a second clear listing taken
|
|
136
|
+
`settle_delay` (default 1.0s) later - a violation now needs two *independent*
|
|
137
|
+
stale listings.
|
|
138
|
+
5. **Own-ticket invariant (0.9.0)**: every decisive listing must still contain the
|
|
139
|
+
worker's own ticket; if another client deleted it (e.g. `break_other_locks`),
|
|
140
|
+
acquisition raises instead of "winning" without a ticket. Recovering a ticket
|
|
141
|
+
via `lock_id=` restores the ticket only - `acquire()` re-runs the election.
|
|
142
|
+
6. **Auto-Cleanup**: `weakref.finalize` deletes ticket objects even if the process
|
|
143
|
+
exits unexpectedly (best effort).
|
|
144
|
+
|
|
145
|
+
**Guarantee and residual window**: on storage with strongly consistent listings the
|
|
146
|
+
election is safe. On eventually-consistent listings the hardening reduces the
|
|
147
|
+
failure mode to *two consecutive independently-stale listings* (measured on B2:
|
|
148
|
+
80/80 listings were first-poll consistent - see `benchmarks/results_visibility_lag.md`).
|
|
149
|
+
No provider we tested currently offers atomic conditional writes
|
|
150
|
+
(`benchmarks/conditional_write_probe.py` is the qualification gate for adding a
|
|
151
|
+
true CAS lock per provider; MEGA S4 accepts the headers but is not atomic under
|
|
152
|
+
concurrency). Tune via `session.lock(key, settle_delay=..., visibility_timeout=...)`.
|
|
148
153
|
|
|
149
154
|
## Development
|
|
150
155
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# s3func
|
|
2
2
|
|
|
3
3
|
<p align="center">
|
|
4
|
-
<em>Simple functions for working with S3
|
|
4
|
+
<em>Simple functions for working with S3-compatible object storage</em>
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
[](https://github.com/mullenkamp/s3func/actions)
|
|
@@ -10,13 +10,13 @@
|
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
13
|
-
`s3func` is a lightweight Python library providing a simplified interface for interacting with S3-compatible object storage services
|
|
13
|
+
`s3func` is a lightweight Python library providing a simplified interface for interacting with S3-compatible object storage services (AWS S3, Backblaze B2, MEGA S4, and others). It removes the `boto3` dependency in favor of a fast, `urllib3`-based client with custom SigV4 signing.
|
|
14
14
|
|
|
15
15
|
## Key Features
|
|
16
16
|
|
|
17
17
|
- **Zero Boto3 Dependency**: Minimal overhead and faster imports.
|
|
18
|
-
- **
|
|
19
|
-
- **Distributed Locking**:
|
|
18
|
+
- **Provider-Agnostic**: One `S3Session` for AWS S3 and any S3-compatible provider (Backblaze B2, MEGA S4, ...), with provider quirks handled portably (strict RFC3986 signing of paths and queries).
|
|
19
|
+
- **Distributed Locking**: Verified shared/exclusive locking on plain object storage - no CAS required (see [docs/locking.md](docs/locking.md)).
|
|
20
20
|
- **Streaming Support**: Efficiently stream large objects.
|
|
21
21
|
- **Automatic Retries**: Built-in adaptive retry logic for transient network issues.
|
|
22
22
|
|
|
@@ -79,24 +79,6 @@ resp = session.head_object('data.csv')
|
|
|
79
79
|
print(resp.metadata['processed']) # 'false'
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
### Backblaze B2 Operations
|
|
83
|
-
|
|
84
|
-
`s3func` uses the B2 native API for better performance and reliability on Backblaze.
|
|
85
|
-
|
|
86
|
-
```python
|
|
87
|
-
from s3func import B2Session
|
|
88
|
-
|
|
89
|
-
# Initialize B2 session
|
|
90
|
-
session = B2Session(
|
|
91
|
-
access_key_id='YOUR_APPLICATION_KEY_ID',
|
|
92
|
-
access_key='YOUR_APPLICATION_KEY',
|
|
93
|
-
bucket='my-b2-bucket'
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
# Put object
|
|
97
|
-
session.put_object('data.json', b'{"status": "ok"}', content_type='application/json')
|
|
98
|
-
```
|
|
99
|
-
|
|
100
82
|
### Distributed Locking
|
|
101
83
|
|
|
102
84
|
`s3func` provides a powerful distributed lock that mimics Python's `threading.Lock` API.
|
|
@@ -120,17 +102,40 @@ if lock.acquire(blocking=True, timeout=10):
|
|
|
120
102
|
## Performance Tips
|
|
121
103
|
|
|
122
104
|
- **Streaming**: Set `stream=True` in the session (default) or individual requests to handle large files without loading them entirely into memory.
|
|
123
|
-
- **B2 Authorization Caching**: `s3func` automatically caches B2 authorization tokens globally for up to 1 hour, significantly reducing latency for concurrent operations.
|
|
124
105
|
- **Adaptive Retries**: The library uses `urllib3` retry logic configured for high-concurrency environments to handle rate limiting and network blips automatically.
|
|
125
106
|
|
|
126
107
|
## How Distributed Locking Works
|
|
127
108
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
109
|
+
Full walk-through with diagrams: [docs/locking.md](docs/locking.md).
|
|
110
|
+
|
|
111
|
+
The lock is a Lamport-bakery-style election over plain object storage (no
|
|
112
|
+
compare-and-swap needed):
|
|
113
|
+
|
|
114
|
+
1. **Acquisition**: A worker writes two small ticket objects (`seq-0` and `seq-1`).
|
|
115
|
+
2. **Self-visibility gate (0.9.0)**: it polls the listing until its OWN ticket is
|
|
116
|
+
visible - a listing that cannot show your own writes cannot be trusted to show
|
|
117
|
+
competitors (raises after `visibility_timeout`, default 30s).
|
|
118
|
+
3. **Election**: it lists all tickets and yields to older ones (`seq-1` timestamp;
|
|
119
|
+
lexicographic `lock_id` breaks ties). Shared tickets yield only to older
|
|
120
|
+
exclusive tickets.
|
|
121
|
+
4. **Confirming re-list (0.9.0)**: winning requires a second clear listing taken
|
|
122
|
+
`settle_delay` (default 1.0s) later - a violation now needs two *independent*
|
|
123
|
+
stale listings.
|
|
124
|
+
5. **Own-ticket invariant (0.9.0)**: every decisive listing must still contain the
|
|
125
|
+
worker's own ticket; if another client deleted it (e.g. `break_other_locks`),
|
|
126
|
+
acquisition raises instead of "winning" without a ticket. Recovering a ticket
|
|
127
|
+
via `lock_id=` restores the ticket only - `acquire()` re-runs the election.
|
|
128
|
+
6. **Auto-Cleanup**: `weakref.finalize` deletes ticket objects even if the process
|
|
129
|
+
exits unexpectedly (best effort).
|
|
130
|
+
|
|
131
|
+
**Guarantee and residual window**: on storage with strongly consistent listings the
|
|
132
|
+
election is safe. On eventually-consistent listings the hardening reduces the
|
|
133
|
+
failure mode to *two consecutive independently-stale listings* (measured on B2:
|
|
134
|
+
80/80 listings were first-poll consistent - see `benchmarks/results_visibility_lag.md`).
|
|
135
|
+
No provider we tested currently offers atomic conditional writes
|
|
136
|
+
(`benchmarks/conditional_write_probe.py` is the qualification gate for adding a
|
|
137
|
+
true CAS lock per provider; MEGA S4 accepts the headers but is not atomic under
|
|
138
|
+
concurrency). Tune via `session.lock(key, settle_delay=..., visibility_timeout=...)`.
|
|
134
139
|
|
|
135
140
|
## Development
|
|
136
141
|
|
|
@@ -6,6 +6,7 @@ Created on May 13 08:04:38 2024
|
|
|
6
6
|
@author: mike
|
|
7
7
|
"""
|
|
8
8
|
import io
|
|
9
|
+
import warnings
|
|
9
10
|
from typing import List, Union
|
|
10
11
|
import urllib.parse
|
|
11
12
|
import hashlib
|
|
@@ -170,7 +171,16 @@ class B2Session:
|
|
|
170
171
|
An alternative download_url when downloading data. If None, the download_url will be retrieved from the initial b2 request. It should NOT include the file/ at the end of the url.
|
|
171
172
|
stream : bool
|
|
172
173
|
Should the connection stay open for streaming or should all the data/content be loaded during the initial request.
|
|
174
|
+
|
|
175
|
+
.. deprecated:: 0.9.0
|
|
176
|
+
The B2 native API offers no capability or performance advantage over
|
|
177
|
+
B2's S3-compatible endpoint. Use :class:`s3func.S3Session` instead.
|
|
173
178
|
"""
|
|
179
|
+
warnings.warn(
|
|
180
|
+
'B2Session (the B2 native API) is deprecated and will be removed in s3func 1.0 - '
|
|
181
|
+
'use S3Session with the B2 S3-compatible endpoint instead.',
|
|
182
|
+
DeprecationWarning, stacklevel=2,
|
|
183
|
+
)
|
|
174
184
|
b2_session = http_url.session(max_connections, max_attempts, read_timeout)
|
|
175
185
|
|
|
176
186
|
if isinstance(download_url, str):
|
|
@@ -595,15 +605,18 @@ class B2Session:
|
|
|
595
605
|
|
|
596
606
|
return b2resp
|
|
597
607
|
|
|
598
|
-
def delete_objects(self, keys: Union[List[str], List[dict]], purge: bool = True):
|
|
608
|
+
def delete_objects(self, keys: Union[List[str], List[dict]] = None, prefix: str = None, purge: bool = True):
|
|
599
609
|
"""
|
|
600
610
|
Delete multiple objects from a B2 bucket.
|
|
601
611
|
|
|
602
612
|
Parameters
|
|
603
613
|
----------
|
|
604
|
-
keys : list of str or list of dict
|
|
614
|
+
keys : list of str or list of dict, optional
|
|
605
615
|
Either a list of key strings (e.g. ['foo.bin', 'bar.bin']) or a list of
|
|
606
616
|
dicts with 'key' and optionally 'version_id' fields.
|
|
617
|
+
Mutually exclusive with prefix.
|
|
618
|
+
prefix : str, optional
|
|
619
|
+
Delete all objects matching this prefix. Mutually exclusive with keys.
|
|
607
620
|
purge : bool
|
|
608
621
|
If True (default), all versions of each object are deleted. If False,
|
|
609
622
|
version_id is required in each dict and only those specific versions
|
|
@@ -613,9 +626,21 @@ class B2Session:
|
|
|
613
626
|
-------
|
|
614
627
|
None
|
|
615
628
|
"""
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
629
|
+
if keys is not None and prefix is not None:
|
|
630
|
+
raise ValueError('keys and prefix are mutually exclusive.')
|
|
631
|
+
if keys is None and prefix is None:
|
|
632
|
+
raise ValueError('Either keys or prefix must be provided.')
|
|
633
|
+
|
|
634
|
+
if prefix is not None:
|
|
635
|
+
if purge:
|
|
636
|
+
resp = self.list_object_versions(prefix=prefix)
|
|
637
|
+
else:
|
|
638
|
+
resp = self.list_objects(prefix=prefix)
|
|
639
|
+
keys = [{'key': obj['key'], 'version_id': obj.get('version_id')} for obj in resp.iter_objects()]
|
|
640
|
+
else:
|
|
641
|
+
# Normalize input: strings -> dicts
|
|
642
|
+
if keys and isinstance(keys[0], str):
|
|
643
|
+
keys = [{'key': k} for k in keys]
|
|
619
644
|
|
|
620
645
|
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
|
621
646
|
futures = []
|