Skip to content
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

chore(setup): add support for downloading file in a bucket folder #77

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions examples/lume-sample-setup-with-auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ steps:
deps:
gcs_1:
type: bucket
url: gs://infra-slack-icons/backup.png
url: gs://lume-tests/file.txt
auth_required: true
credentials_env: GOOGLE_APPLICATION_CREDENTIALS
unzip: false
gcs_2:
type: bucket
url: gs://infra-slack-icons/backup.png
url: gs://lume-tests/sample_folder/file_in_folder.txt
auth_required: true
credentials_env: GOOGLE_APPLICATION_CREDENTIALS
unzip: false
Expand Down
7 changes: 4 additions & 3 deletions lume/src/infrastructure/services/setup/setup_item_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ def run(

@staticmethod
def __download_bucket(storage_client: storage.Client, dst: str, url: str):
filepath_parts = url.split("/")
filepath_parts = url.replace("gs://", "").split("/")
filename = filepath_parts[-1]
bucket_name = "/".join(filepath_parts[2:-1])
bucket_name = filepath_parts[0]
bucket_filepath = "/".join(filepath_parts[1:])
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(filename)
blob = bucket.blob(bucket_filepath)
blob.download_to_filename(os.path.join(dst, filename))
28 changes: 22 additions & 6 deletions tests/src/test_setup_item_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from lume.src.infrastructure.services.setup.setup_item_bucket import SetupItemBucket

TEMPORARY_FOLDER = "test-deps"
VALID_FILE_FROM_BUCKET = "gs://aliceonboarding/-AmBH6e1JnNcFPej9Bcvp5EJRCI=.jpg"
VALID_FILE_FROM_BUCKET = "gs://lume-tests/file.txt"
VALID_FILE_FROM_BUCKET_FOLDER = "gs://lume-tests/sample_folder/file_in_folder.txt"


@pytest.mark.skipif(
Expand All @@ -36,17 +37,32 @@ def should_download_a_valid_bucket_with_auth(self):
)
result = file_setuper.run("test-item", dependency_config, EmojisLogger())
assert_success(result)
assert os.path.exists(
f"{TEMPORARY_FOLDER}/test-item/-AmBH6e1JnNcFPej9Bcvp5EJRCI=.jpg"
)
assert os.path.exists(f"{TEMPORARY_FOLDER}/test-item/file.txt")

# now downloads the file overwriting the previous one
dependency_config.overwrite = True
result = file_setuper.run("test-item", dependency_config, EmojisLogger())
assert_success(result)
assert os.path.exists(
f"{TEMPORARY_FOLDER}/test-item/-AmBH6e1JnNcFPej9Bcvp5EJRCI=.jpg"
assert os.path.exists(f"{TEMPORARY_FOLDER}/test-item/file.txt")

def should_download_a_valid_file_in_bucket_folder_with_auth(self):
file_setuper = SetupItemBucket(base_path=TEMPORARY_FOLDER)
dependency_config = DependencyConfig(
type="bucket",
url=VALID_FILE_FROM_BUCKET_FOLDER,
auth_required=True,
credentials_env="GOOGLE_APPLICATION_CREDENTIALS",
unzip=False,
)
result = file_setuper.run("test-item", dependency_config, EmojisLogger())
assert_success(result)
assert os.path.exists(f"{TEMPORARY_FOLDER}/test-item/file_in_folder.txt")

# now downloads the file overwriting the previous one
dependency_config.overwrite = True
result = file_setuper.run("test-item", dependency_config, EmojisLogger())
assert_success(result)
assert os.path.exists(f"{TEMPORARY_FOLDER}/test-item/file_in_folder.txt")

def should_return_error_when_wrong_bucket_name(self):
file_setuper = SetupItemBucket(base_path=TEMPORARY_FOLDER)
Expand Down