Skip to content

Commit

Permalink
Make storage client acts on behalf of the supplied project (#129)
Browse files Browse the repository at this point in the history
* Make storage client acts on behalf of the supplied project

This should resolve issue#128

* Bump version from 2.0.6 to 2.0.7
  • Loading branch information
Andilun authored Mar 12, 2024
1 parent 13c3d1b commit 4ec50a5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dapla-toolbelt"
version = "2.0.8"
version = "2.0.9"
description = "Dapla Toolbelt"
authors = ["Dapla Developers <[email protected]>"]
license = "MIT"
Expand Down
9 changes: 5 additions & 4 deletions src/dapla/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ class EmptyListError(Exception):


def _get_list_of_blobs_with_prefix(
bucket_name: str, folder_prefix: str
bucket_name: str, folder_prefix: str, project_id: str
) -> list[storage.Blob]:
"""Helper function that gets a list of Blob objects in a Google Cloud Storage bucket that has a certain prefix.
Args:
bucket_name (str): The name of the Google Cloud Storage bucket to get blobs from.
folder_prefix (str): The prefix to filter blobs by.
project_id (str): The ID of the Google Cloud project that the Pub/Sub topic belongs to.
Returns:
An list over the `storage.Blob` objects representing the blobs in the specified bucket and with names starting
with the given prefix.
"""
google_credentials = AuthClient.fetch_google_credentials()
storage_client = storage.Client(credentials=google_credentials)
storage_client = storage.Client(project=project_id, credentials=google_credentials)
return list(storage_client.list_blobs(bucket_name, prefix=folder_prefix))


Expand Down Expand Up @@ -94,7 +95,7 @@ def _publish_gcs_objects_to_pubsub(
EmptyListError: If there are no objects in the bucket with the given prefix.
"""
blob_list = _get_list_of_blobs_with_prefix(bucket_id, folder_prefix)
blob_list = _get_list_of_blobs_with_prefix(bucket_id, folder_prefix, project_id)

if len(blob_list) == 0:
raise EmptyListError(
Expand Down Expand Up @@ -163,7 +164,7 @@ def trigger_source_data_processing(
"""Triggers a source data processing service with every file that has a given prefix.
Args:
project_id (str): The ID of Google Cloud project containing the source.
project_id (str): The ID of Google Cloud project containing the pubsub topic, this is normally the standard project.
folder_prefix (str): The folder prefix of the files to be processed.
source_name (str): The name of source that should process the files.
kuben (bool): Whether the team is on kuben or legacy.
Expand Down
4 changes: 3 additions & 1 deletion tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def test_get_list_of_blobs_with_prefix(self) -> None:
NotFound,
)
):
_get_list_of_blobs_with_prefix(self.bucket_id, self.folder_prefix)
_get_list_of_blobs_with_prefix(
self.bucket_id, self.folder_prefix, self.project_id
)

def test_generate_pubsub_data(self) -> None:
byte_data = _generate_pubsub_data(self.bucket_id, self.object_id)
Expand Down

0 comments on commit 4ec50a5

Please sign in to comment.