-
Notifications
You must be signed in to change notification settings - Fork 451
Replace 'aws s3 ls' shell-out in dataset.py with boto3 #239
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
Open
goanpeca
wants to merge
1
commit into
Stability-AI:main
Choose a base branch
from
goanpeca:feat/boto3-s3-loader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,4 +161,6 @@ cython_debug/ | |
|
|
||
| *.ckpt | ||
| *.wav | ||
| wandb/* | ||
| wandb/* | ||
| # macOS | ||
| .DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import os | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from stable_audio_tools.data import dataset as ds | ||
|
|
||
|
|
||
| def _fake_paginator(pages): | ||
| "Paginator-like mock; records last paginate(**kwargs) on .last_kwargs." | ||
| pag = mock.MagicMock() | ||
| pag.last_kwargs = {} | ||
|
|
||
| def paginate(**kwargs): | ||
| pag.last_kwargs = kwargs | ||
| return iter(pages) | ||
|
|
||
| pag.paginate.side_effect = paginate | ||
| return pag | ||
|
|
||
|
|
||
| def _fake_client(pages=None, presigned_url="https://example.com/signed"): | ||
| client = mock.MagicMock() | ||
| client.get_paginator.return_value = _fake_paginator(pages or []) | ||
| client.generate_presigned_url.return_value = presigned_url | ||
| return client | ||
|
|
||
|
|
||
| def test_get_s3_client_uses_aws_endpoint_url_env(): | ||
| fake_boto3 = mock.MagicMock() | ||
| fake_session = mock.MagicMock() | ||
| fake_boto3.Session.return_value = fake_session | ||
|
|
||
| with mock.patch.dict(os.environ, {"AWS_ENDPOINT_URL": "https://s3.us-west-004.backblazeb2.com"}, clear=False): | ||
| with mock.patch.dict("sys.modules", {"boto3": fake_boto3}): | ||
| ds._get_s3_client() | ||
|
|
||
| fake_session.client.assert_called_once_with( | ||
| "s3", endpoint_url="https://s3.us-west-004.backblazeb2.com" | ||
| ) | ||
|
|
||
|
|
||
| def test_get_s3_client_default_when_env_unset(): | ||
| fake_boto3 = mock.MagicMock() | ||
| fake_session = mock.MagicMock() | ||
| fake_boto3.Session.return_value = fake_session | ||
|
|
||
| env = {k: v for k, v in os.environ.items() if k != "AWS_ENDPOINT_URL"} | ||
| with mock.patch.dict(os.environ, env, clear=True): | ||
| with mock.patch.dict("sys.modules", {"boto3": fake_boto3}): | ||
| ds._get_s3_client() | ||
|
|
||
| # endpoint_url=None preserves boto3's default (AWS) behavior. | ||
| fake_session.client.assert_called_once_with("s3", endpoint_url=None) | ||
|
|
||
|
|
||
| def test_get_s3_client_uses_profile_when_given(): | ||
| fake_boto3 = mock.MagicMock() | ||
| fake_session = mock.MagicMock() | ||
| fake_boto3.Session.return_value = fake_session | ||
|
|
||
| with mock.patch.dict("sys.modules", {"boto3": fake_boto3}): | ||
| ds._get_s3_client(profile="myprofile") | ||
|
|
||
| fake_boto3.Session.assert_called_once_with(profile_name="myprofile") | ||
|
|
||
|
|
||
| def test_get_s3_contents_returns_keys_relative_to_prefix(): | ||
| pages = [ | ||
| {"Contents": [ | ||
| {"Key": "prefix/a.tar"}, | ||
| {"Key": "prefix/sub/b.tar"}, | ||
| {"Key": "prefix/"}, # directory marker -> skipped | ||
| ]}, | ||
| ] | ||
| client = _fake_client(pages=pages) | ||
|
|
||
| with mock.patch.object(ds, "_get_s3_client", return_value=client): | ||
| keys = ds.get_s3_contents("s3://bucket/prefix/", recursive=True) | ||
|
|
||
| client.get_paginator.assert_called_once_with("list_objects_v2") | ||
| pag = client.get_paginator.return_value | ||
| assert pag.last_kwargs == {"Bucket": "bucket", "Prefix": "prefix/"} | ||
| # Recursive mode strips the bucket-level prefix from each key. | ||
| assert keys == ["a.tar", "sub/b.tar"] | ||
|
|
||
|
|
||
| def test_get_s3_contents_non_recursive_adds_delimiter(): | ||
| client = _fake_client(pages=[{"Contents": []}]) | ||
|
|
||
| with mock.patch.object(ds, "_get_s3_client", return_value=client): | ||
| ds.get_s3_contents("s3://bucket/prefix/", recursive=False) | ||
|
|
||
| pag = client.get_paginator.return_value | ||
| assert pag.last_kwargs == { | ||
| "Bucket": "bucket", | ||
| "Prefix": "prefix/", | ||
| "Delimiter": "/", | ||
| } | ||
|
|
||
|
|
||
| def test_get_s3_contents_non_recursive_strips_prefix_from_keys(): | ||
| pages = [{"Contents": [ | ||
| {"Key": "prefix/a.tar"}, | ||
| {"Key": "prefix/b.tar"}, | ||
| ]}] | ||
| client = _fake_client(pages=pages) | ||
|
|
||
| with mock.patch.object(ds, "_get_s3_client", return_value=client): | ||
| keys = ds.get_s3_contents("s3://bucket/prefix/", recursive=False) | ||
|
|
||
| # Keys must be relative to dataset_path in BOTH recursive and non-recursive | ||
| # modes (matches the legacy `aws s3 ls` output shape; without this strip, | ||
| # `get_all_s3_urls(..., recursive=False)` joins the prefix twice). | ||
| assert keys == ["a.tar", "b.tar"] | ||
|
|
||
|
|
||
| def test_get_s3_contents_applies_filter(): | ||
| pages = [{"Contents": [ | ||
| {"Key": "prefix/a.tar"}, | ||
| {"Key": "prefix/b.txt"}, | ||
| {"Key": "prefix/c.tar"}, | ||
| ]}] | ||
| client = _fake_client(pages=pages) | ||
|
|
||
| with mock.patch.object(ds, "_get_s3_client", return_value=client): | ||
| keys = ds.get_s3_contents("s3://bucket/prefix/", filter="tar", recursive=True) | ||
|
|
||
| assert keys == ["a.tar", "c.tar"] | ||
|
|
||
|
|
||
| def test_get_s3_contents_rejects_non_s3_url(): | ||
| with pytest.raises(ValueError): | ||
| ds.get_s3_contents("not-an-s3-url/") | ||
|
|
||
|
|
||
| def test_get_all_s3_urls_emits_pipe_curl_with_presigned_url(): | ||
| pages = [{"Contents": [{"Key": "name/train/shard-000.tar"}]}] | ||
| fake_url = "https://signed.example.com/shard-000.tar?X-Amz-Signature=abc" | ||
| client = _fake_client(pages=pages, presigned_url=fake_url) | ||
|
|
||
| with mock.patch.object(ds, "_get_s3_client", return_value=client): | ||
| urls = ds.get_all_s3_urls( | ||
| names=["name"], | ||
| subsets=["train"], | ||
| s3_url_prefix="s3://bucket", | ||
| recursive=True, | ||
| filter_str="tar", | ||
| ) | ||
|
|
||
| assert urls == [f'pipe:curl -fsSL "{fake_url}"'] | ||
| client.generate_presigned_url.assert_called_with( | ||
| "get_object", | ||
| Params={"Bucket": "bucket", "Key": "name/train/shard-000.tar"}, | ||
| ExpiresIn=3600, | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.