This repository was archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
#55: Added method to list files in bucket #60
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
63f95b1
Added listing method
umitbuyuksahin 5d9fb12
Added test for listing method
umitbuyuksahin 836e1f5
Returned list instead of string
umitbuyuksahin 49f747b
Added documentation
umitbuyuksahin 80a7a55
Updated type hints
umitbuyuksahin 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
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,28 @@ | ||
| from pathlib import Path | ||
| from exasol_bucketfs_utils_python import upload, list_files | ||
| from exasol_bucketfs_utils_python.bucket_config import BucketConfig | ||
| from exasol_bucketfs_utils_python.bucketfs_config import BucketFSConfig | ||
| from exasol_bucketfs_utils_python.bucketfs_connection_config import BucketFSConnectionConfig | ||
|
|
||
| connection_config = BucketFSConnectionConfig( | ||
| host="localhost", port=6666, | ||
| user="w", pwd="write", | ||
| is_https=False) | ||
| bucketfs_config = BucketFSConfig( | ||
| connection_config=connection_config, | ||
| bucketfs_name="bfsdefault") | ||
| bucket_config = BucketConfig( | ||
| bucket_name="default", | ||
| bucketfs_config=bucketfs_config) | ||
|
|
||
| local_input_file_path = Path("local_input_file.txt") | ||
| path_in_bucket = "path/in/bucket/file.txt" | ||
| upload.upload_file_to_bucketfs( | ||
| bucket_config=bucket_config, | ||
| bucket_file_path=path_in_bucket, | ||
| local_file_path=local_input_file_path) | ||
|
|
||
| bucket_file_path = Path("path/in/bucket") | ||
| files = list_files.list_files_in_bucketfs( | ||
| bucket_config=bucket_config, | ||
| bucket_file_path=path_in_bucket) |
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,14 @@ | ||
|
|
||
| ##################################### | ||
| Listing files in bucket | ||
| ##################################### | ||
|
|
||
| This library provides a function to list the files in the bucket under a given | ||
| path. As in the example below, the list of files in the specified bucket | ||
| directory is obtained by the provided listing method. | ||
|
|
||
|
|
||
| Example: | ||
|
|
||
| .. literalinclude:: list_files_in_bucket.py | ||
| :language: python3 |
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,34 @@ | ||
| from typing import Iterable | ||
| import requests | ||
| from pathlib import Path | ||
| from exasol_bucketfs_utils_python.bucket_config import BucketConfig | ||
| from exasol_bucketfs_utils_python import bucketfs_utils | ||
| from exasol_bucketfs_utils_python.bucketfs_utils import generate_bucket_http_url | ||
|
|
||
|
|
||
| def list_files_in_bucketfs(bucket_config: BucketConfig, | ||
| bucket_file_path: str = "") -> Iterable[str]: | ||
| """ | ||
| List files at the specified path in the bucket in BucketFs, line by line. | ||
|
|
||
| :param bucket_config: BucketConfig for the bucket to download from | ||
| :param bucket_file_path: Path in the bucket to download the file from | ||
| :return: The list of the files in the BucketFS as string. | ||
| """ | ||
| if bucket_file_path is None: | ||
| raise ValueError("bucket_file_path can't be None") | ||
| url = generate_bucket_http_url(bucket_config, "") | ||
| auth = bucketfs_utils.create_auth_object(bucket_config) | ||
| response = requests.get(url.geturl(), auth=auth) | ||
| response.raise_for_status() | ||
|
|
||
| bucket_file_path_parts = Path(bucket_file_path).parts | ||
| files = [] | ||
| for path in response.text.split(): | ||
| path_parts = Path(path).parts | ||
| if path_parts[:len(bucket_file_path_parts)] == bucket_file_path_parts: | ||
| relevant_parts = path_parts[len(bucket_file_path_parts):] | ||
| relevant_path = str(Path(*relevant_parts)) | ||
| files.append(relevant_path) | ||
|
|
||
| return files |
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,41 @@ | ||
| from exasol_bucketfs_utils_python import upload, list_files | ||
| from exasol_bucketfs_utils_python.bucket_config import BucketConfig | ||
| from exasol_bucketfs_utils_python.bucketfs_config import BucketFSConfig | ||
| from exasol_bucketfs_utils_python.bucketfs_connection_config import BucketFSConnectionConfig | ||
| from tests.test_load_fs_file_from_udf import delete_testfile_from_bucketfs | ||
|
|
||
|
|
||
| def test_list_files(): | ||
| connection_config = BucketFSConnectionConfig( | ||
| host="localhost", port=6666, user="w", pwd="write", is_https=False) | ||
| bucketfs_config = BucketFSConfig( | ||
| connection_config=connection_config, bucketfs_name="bfsdefault") | ||
| bucket_config = BucketConfig( | ||
| bucket_name="default", bucketfs_config=bucketfs_config) | ||
| test_string = "test_string" | ||
|
|
||
| path_list = ["path/in/bucket/file.txt", "path/file2.txt"] | ||
| try: | ||
umitbuyuksahin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for path_in_bucket in path_list: | ||
| upload.upload_string_to_bucketfs( | ||
| bucket_config=bucket_config, | ||
| bucket_file_path=path_in_bucket, | ||
| string=test_string) | ||
|
|
||
| bucket_file_path_map = { | ||
| "path": ["in/bucket/file.txt", "file2.txt"], | ||
| "path/": ["in/bucket/file.txt", "file2.txt"], | ||
| "path/in": ["bucket/file.txt"], | ||
| "path/in/": ["bucket/file.txt"], | ||
| "path/in/bucket": ["file.txt"], | ||
| "path/in/bucket/": ["file.txt"], | ||
| "path/in/bucket/file.txt": ["."] | ||
| } | ||
| for bucket_path, expected in bucket_file_path_map.items(): | ||
| assert expected == list_files.list_files_in_bucketfs( | ||
| bucket_config, bucket_path) | ||
| finally: | ||
| for path_in_bucket in path_list: | ||
| delete_testfile_from_bucketfs( | ||
| file_path=path_in_bucket, | ||
| bucket_config=bucket_config) | ||
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.