Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5ca8417
add S3 functionality to strax
LuisSanchez25 Sep 26, 2024
d1cbeeb
add _get_config_values
LuisSanchez25 Sep 26, 2024
f66ff0a
S3 code pases st.make test
LuisSanchez25 Oct 1, 2024
fb4e2ca
add types (incomplete)
LuisSanchez25 Oct 2, 2024
73f6faa
Merge branch 'master' into s3_protocol
LuisSanchez25 Oct 2, 2024
c6c71dd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2024
89742f6
Merge branch 'master' into s3_protocol
yuema137 Oct 17, 2024
6c2548b
Merge branch 'master' into s3_protocol
yuema137 Nov 15, 2024
607514b
Merge branch 'master' into s3_protocol
yuema137 Nov 20, 2024
e846991
Merge branch 'master' into s3_protocol
LuisSanchez25 Mar 3, 2025
f2dbc7c
update io
LuisSanchez25 Mar 3, 2025
9409a2e
update documentation + change bucket initialization
LuisSanchez25 Mar 3, 2025
63e0bad
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 3, 2025
55fe59b
remove uneeded imports + reduce line length
LuisSanchez25 Mar 3, 2025
c5cce8e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 3, 2025
8789a53
Merge branch 'master' into s3_protocol
LuisSanchez25 Mar 4, 2025
107f0d9
fix style
LuisSanchez25 Mar 4, 2025
d653133
Update io.py
LuisSanchez25 Mar 4, 2025
7386f90
fix test
LuisSanchez25 Mar 4, 2025
d9aa5b6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 4, 2025
c306023
add self.bucket_name
LuisSanchez25 Mar 4, 2025
f8d2bee
Remove unnecessary functions
LuisSanchez25 Mar 5, 2025
e820701
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 5, 2025
3770688
remove unused variables
LuisSanchez25 Mar 5, 2025
c0a343f
fix return output + add bucket default name
LuisSanchez25 Mar 5, 2025
1c2c452
Merge branch 'master' into s3_protocol
yuema137 Mar 20, 2025
514002b
fix broken functions after update
LuisSanchez25 May 6, 2025
4320190
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
f7e56af
change boto3 to compatible version
LuisSanchez25 May 6, 2025
3864a43
Merge branch 'master' into s3_protocol
LuisSanchez25 May 22, 2025
29545d3
update files based on copilot recomendation
LuisSanchez25 Jul 29, 2025
ee135f1
Merge branch 'master' into s3_protocol
LuisSanchez25 Jul 29, 2025
4bb5ddb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 29, 2025
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
1 change: 1 addition & 0 deletions strax/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .storage.file_rechunker import *
from .storage.mongo import *
from .storage.zipfiles import *
from .storage.s3 import *

from .config import *
from .plugins import *
Expand Down
52 changes: 47 additions & 5 deletions strax/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,42 @@ def _load_file(f, compressor, dtype):


@export
def save_file(f, data, compressor="zstd"):
def save_file(f, data, compressor="zstd", is_s3_path=False):
"""Save data to file and return number of bytes written.

:param f: file name or handle to save to
:param data: data (numpy array) to save
:param compressor: compressor to use

"""

if isinstance(f, str):
final_fn = f
temp_fn = f + "_temp"
with open(temp_fn, mode="wb") as write_file:
result = _save_file(write_file, data, compressor)
os.rename(temp_fn, final_fn)
return result
if is_s3_path is False:
with open(temp_fn, mode="wb") as write_file:
result = _save_file(write_file, data, compressor)
os.rename(temp_fn, final_fn)
return result
else:
s3_interface = strax.S3Frontend(
s3_access_key_id=None,
s3_secret_access_key=None,
path="",
deep_scan=False,
)
# Copy temp file to final file
result = _save_file_to_s3(s3_interface, temp_fn, data, compressor)
s3_interface.s3.copy_object(
Bucket=s3_interface.BUCKET,
Key=final_fn,
CopySource={"Bucket": s3_interface.BUCKET, "Key": temp_fn},
)

# Delete the temporary file
s3_interface.s3.delete_object(Bucket=s3_interface.BUCKET, Key=temp_fn)

return result
else:
return _save_file(f, data, compressor)

Expand All @@ -99,6 +120,27 @@ def _save_file(f, data, compressor="zstd"):
return len(d_comp)


def _save_file_to_s3(s3_client, key, data, compressor=None):
# Use this method to save file directly to S3
# If compression is needed, handle it here
# Use `BytesIO` to handle binary data in-memory
assert isinstance(data, np.ndarray), "Please pass a numpy array"

# Create a binary buffer to simulate writing to a file
buffer = BytesIO()

# Simulate saving file content (you can compress or directly write data here)
if compressor:
data = COMPRESSORS[compressor]["compress"](data)
buffer.write(data)
buffer.seek(0) # Reset the buffer to the beginning

# Upload buffer to S3 under the specified key
s3_client.s3.put_object(Bucket=s3_client.BUCKET, Key=key, Body=buffer.getvalue())

return len(data)


def _compress_blosc(data):
if data.nbytes >= blosc.MAX_BUFFERSIZE:
raise ValueError("Blosc's input buffer cannot exceed ~2 GB")
Expand Down
Loading