-
Notifications
You must be signed in to change notification settings - Fork 1
Initial adaptation to work with S3 data #23
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
06e6740
Initial adaptation to work with S3 data
schilling40 eb38646
Updated packages for environment
schilling40 760d6ca
Moved S3 utils
schilling40 bb41c54
Expand segmentation table with distance to nearest neighbors
schilling40 dad0b31
Removed mobie dependencies from package
schilling40 4394134
Fixed issue for setting chunks
schilling40 6bf43e3
Improved S3 functions, fixed issues
schilling40 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 |
|---|---|---|
|
|
@@ -10,5 +10,7 @@ dependencies: | |
| - scikit-image | ||
| - pybdv | ||
| - pytorch | ||
| - s3fs | ||
| - torch_em | ||
| - z5py | ||
| - zarr | ||
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,111 @@ | ||
| import os | ||
|
|
||
| import s3fs | ||
| import zarr | ||
|
|
||
| """ | ||
| This script contains utility functions for processing data located on an S3 storage. | ||
| The upload of data to the storage system should be performed with 'rclone'. | ||
| """ | ||
|
|
||
| # Dedicated bucket for cochlea lightsheet project | ||
| MOBIE_FOLDER = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/mobie_project/cochlea-lightsheet" | ||
| SERVICE_ENDPOINT = "https://s3.gwdg.de/" | ||
| BUCKET_NAME = "cochlea-lightsheet" | ||
|
|
||
| DEFAULT_CREDENTIALS = os.path.expanduser("~/.aws/credentials") | ||
|
|
||
| # For MoBIE: | ||
| # https://s3.gwdg.de/incucyte-general/lightsheet | ||
|
|
||
| def check_s3_credentials(bucket_name, service_endpoint, credential_file): | ||
| """ | ||
| Check if S3 parameter and credentials were set either as a function input or were exported as environment variables. | ||
| """ | ||
| if bucket_name is None: | ||
| bucket_name = os.getenv('BUCKET_NAME') | ||
| if bucket_name is None: | ||
| if BUCKET_NAME in globals(): | ||
| bucket_name = BUCKET_NAME | ||
| else: | ||
| raise ValueError("Provide a bucket name for accessing S3 data.\nEither by using an optional argument or exporting an environment variable:\n--s3_bucket_name <bucket_name>\nexport BUCKET_NAME=<bucket_name>") | ||
|
|
||
| if service_endpoint is None: | ||
| service_endpoint = os.getenv('SERVICE_ENDPOINT') | ||
| if service_endpoint is None: | ||
| if SERVICE_ENDPOINT in globals(): | ||
| service_endpoint = SERVICE_ENDPOINT | ||
| else: | ||
| raise ValueError("Provide a service endpoint for accessing S3 data.\nEither by using an optional argument or exporting an environment variable:\n--s3_service_endpoint <endpoint>\nexport SERVICE_ENDPOINT=<endpoint>") | ||
|
|
||
| if credential_file is None: | ||
| access_key = os.getenv('AWS_ACCESS_KEY_ID') | ||
| secret_key = os.getenv('AWS_SECRET_ACCESS_KEY') | ||
|
|
||
| # check for default credentials if no credential_file is provided | ||
| if access_key is None: | ||
| if os.path.isfile(DEFAULT_CREDENTIALS): | ||
| access_key, _ = read_s3_credentials(credential_file=DEFAULT_CREDENTIALS) | ||
| else: | ||
| raise ValueError(f"Either provide a credential file as an optional argument, have credentials at '{DEFAULT_CREDENTIALS}', or export an access key as an environment variable:\nexport AWS_ACCESS_KEY_ID=<access_key>") | ||
| if secret_key is None: | ||
| # check for default credentials | ||
| if os.path.isfile(DEFAULT_CREDENTIALS): | ||
| _, secret_key = read_s3_credentials(credential_file=DEFAULT_CREDENTIALS) | ||
| else: | ||
| raise ValueError(f"Either provide a credential file as an optional argument, have credentials at '{DEFAULT_CREDENTIALS}', or export a secret access key as an environment variable:\nexport AWS_SECRET_ACCESS_KEY=<secret_key>") | ||
|
|
||
| else: | ||
| # check validity of credential file | ||
| _, _ = read_s3_credentials(credential_file=credential_file) | ||
|
|
||
| return bucket_name, service_endpoint, credential_file | ||
|
|
||
| def get_s3_path( | ||
| input_path, | ||
| bucket_name=None, service_endpoint=None, | ||
| credential_file=None, | ||
| ): | ||
| """ | ||
| Get S3 path for a file or folder and file system based on S3 parameters and credentials. | ||
| """ | ||
| bucket_name, service_endpoint, credential_file = check_s3_credentials(bucket_name, service_endpoint, credential_file) | ||
|
|
||
| fs = create_s3_target(url=service_endpoint, anon=False, credential_file=credential_file) | ||
|
|
||
| zarr_path=f"{bucket_name}/{input_path}" | ||
|
|
||
| if not fs.exists(zarr_path): | ||
| print(f"Error: S3 path {zarr_path} does not exist!") | ||
|
|
||
| s3_path = zarr.storage.FSStore(zarr_path, fs=fs) | ||
|
|
||
| return s3_path, fs | ||
|
|
||
|
|
||
| def read_s3_credentials(credential_file): | ||
| key, secret = None, None | ||
| with open(credential_file) as f: | ||
| for line in f: | ||
| if line.startswith("aws_access_key_id"): | ||
| key = line.rstrip("\n").strip().split(" ")[-1] | ||
| if line.startswith("aws_secret_access_key"): | ||
| secret = line.rstrip("\n").strip().split(" ")[-1] | ||
| if key is None or secret is None: | ||
| raise ValueError(f"Invalid credential file {credential_file}") | ||
| return key, secret | ||
|
|
||
|
|
||
| def create_s3_target(url, anon=False, credential_file=None): | ||
| """ | ||
| Create file system for S3 bucket based on a service endpoint and an optional credential file. | ||
| If the credential file is not provided, the s3fs.S3FileSystem function checks the environment variables | ||
| AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. | ||
| """ | ||
| client_kwargs = {"endpoint_url": url} | ||
| if credential_file is not None: | ||
| key, secret = read_s3_credentials(credential_file) | ||
schilling40 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fs = s3fs.S3FileSystem(key=key, secret=secret, client_kwargs=client_kwargs) | ||
| else: | ||
| fs = s3fs.S3FileSystem(anon=anon, client_kwargs=client_kwargs) | ||
| return fs | ||
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
Oops, something went wrong.
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.