-
Notifications
You must be signed in to change notification settings - Fork 15
Add Google Cloud Storage Adapter #84
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
MaxGroot
wants to merge
14
commits into
fox-it:main
Choose a base branch
from
MaxGroot:feature/gcs-adapter
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 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5b6360e
Support file-like objects in record writer
MaxGroot f078d4f
Add Google Cloud Storage Adapter
MaxGroot 36c8f34
Merge branch 'main' into feature/gcs-adapter
MaxGroot 79acd5e
Implement review suggestions
MaxGroot 3394b6a
Use `re.split` to determine prefix and pattern
MaxGroot 901c2ec
Further simplify prefix and pattern logic
MaxGroot a7d7860
Implement code review suggestions
MaxGroot 4f03249
Implement code review suggestions
MaxGroot c3747b6
Merge branch 'fox-it:main' into feature/gcs-adapter
MaxGroot 67b43ba
Merge branch 'main' into feature/gcs-adapter
MaxGroot 6624d73
Support transparent compression when writing to fileobj
MaxGroot 92390bf
Fix flushing & closing, make write test use gzip
MaxGroot 028b4e4
Implement review suggestion
MaxGroot 873ba91
Merge branch 'main' into feature/gcs-adapter
yunzheng 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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import logging | ||
| from fnmatch import fnmatch | ||
| from typing import Iterator, Union | ||
|
|
||
| from google.cloud.storage.client import Client | ||
| from google.cloud.storage.fileio import BlobReader, BlobWriter | ||
|
|
||
| from flow.record.adapter import AbstractReader, AbstractWriter | ||
| from flow.record.base import Record, RecordAdapter | ||
| from flow.record.selector import CompiledSelector, Selector | ||
|
|
||
| __usage__ = """ | ||
| Google Cloud Storage adapter | ||
| --- | ||
| Read usage: rdump gcs://[PROJECT-ID]:[BUCKET-ID]?path=[PATH] | ||
|
MaxGroot marked this conversation as resolved.
Outdated
|
||
| [PROJECT-ID]: Google Cloud Project ID | ||
| [BUCKET-ID]: Bucket ID | ||
| [path]: Path to look for files, with support for glob-pattern matching | ||
|
|
||
| Write usage: rdump gcs://[PROJECT-ID]:[BUCKET-ID]?path=[PATH] | ||
| [PROJECT-ID]: Google Cloud Project ID | ||
| [BUCKET-ID]: Bucket ID | ||
| [path]: Path to write records to | ||
| """ | ||
|
MaxGroot marked this conversation as resolved.
|
||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| GLOB_CHARACTERS = "*?[]" | ||
|
|
||
|
|
||
| class GcsReader(AbstractReader): | ||
| def __init__(self, uri: str, path: str, selector: Union[None, Selector, CompiledSelector] = None, **kwargs) -> None: | ||
| self.selector = selector | ||
| project_name = uri[: uri.find(":")] | ||
| bucket_name = uri[uri.find(":") + 1 :] | ||
|
MaxGroot marked this conversation as resolved.
Outdated
|
||
|
|
||
| self.gcs = Client(project=project_name) | ||
| self.bucket = self.gcs.bucket(bucket_name) | ||
|
|
||
| # GCS Doesn't support iterating blobs using a glob pattern, so we have to do that ourselves. | ||
| # To split the path prefix from the glob-specific stuff, we have to find the first place where | ||
| # the glob starts. We'll then go through all files that match the path prefix before the glob, | ||
| # and do fnmatch ourselves to check whether any given blob matches with our glob. | ||
| lowest_pos = min([path.find(char) if path.find(char) >= 0 else float("inf") for char in GLOB_CHARACTERS]) | ||
|
MaxGroot marked this conversation as resolved.
Outdated
|
||
| if lowest_pos == float("inf"): | ||
| # No glob character was found | ||
| self.glob = None | ||
| self.prefix = path | ||
| else: | ||
| # Split the glob and the prefix | ||
| self.prefix = path[:lowest_pos] | ||
| self.glob = path | ||
|
|
||
| def __iter__(self) -> Iterator[Record]: | ||
| blobs = self.gcs.list_blobs(bucket_or_name=self.bucket, prefix=self.prefix) | ||
| for blob in blobs: | ||
| if blob.size == 0: # Skip empty files | ||
| continue | ||
| if self.glob and not fnmatch(blob.name, self.glob): | ||
| continue | ||
| blobreader = BlobReader(blob) | ||
|
|
||
| # Give the file-like object to RecordAdapter so it will select the right adapter by peeking into the stream | ||
| reader = RecordAdapter(fileobj=blobreader, out=False, selector=self.selector) | ||
| for record in reader: | ||
| yield record | ||
|
|
||
| def close(self) -> None: | ||
| self.gcs.close() | ||
|
|
||
|
|
||
| class GcsWriter(AbstractWriter): | ||
| def __init__(self, uri: str, path: str, **kwargs): | ||
| project_name = uri[: uri.find(":")] | ||
| bucket_name = uri[uri.find(":") + 1 :] | ||
|
MaxGroot marked this conversation as resolved.
Outdated
|
||
| self.writer = None | ||
|
|
||
| self.gcs = Client(project=project_name) | ||
| self.bucket = self.gcs.bucket(bucket_name) | ||
|
|
||
| blob = self.bucket.blob(path) | ||
| self.writer = BlobWriter(blob, ignore_flush=True) | ||
| self.adapter = RecordAdapter(url=path, fileobj=self.writer, out=True, **kwargs) | ||
|
|
||
| def write(self, record: Record) -> None: | ||
| self.adapter.write(record) | ||
|
|
||
| def flush(self) -> None: | ||
| # https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.fileio.BlobWriter) | ||
| # Flushing without closing is not supported by the remote service and therefore calling it on this class | ||
| # normally results in io.UnsupportedOperation. However, that behavior is incompatible with some consumers and | ||
| # wrappers of fileobjects in Python. | ||
| pass | ||
|
|
||
| def close(self) -> None: | ||
| if self.writer: | ||
| self.writer.close() | ||
|
MaxGroot marked this conversation as resolved.
|
||
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
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
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.