-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Phase 1 Added new method download_blob_to_file within Storage Client #7949
Changes from all commits
42337e1
62c99d5
edcb4e2
52d3ed8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
"""Client for interacting with the Google Cloud Storage API.""" | ||
|
||
from six.moves.urllib.parse import urlsplit | ||
|
||
from google.auth.credentials import AnonymousCredentials | ||
|
||
|
@@ -24,6 +25,7 @@ | |
from google.cloud.storage._http import Connection | ||
from google.cloud.storage.batch import Batch | ||
from google.cloud.storage.bucket import Bucket | ||
from google.cloud.storage.blob import Blob | ||
|
||
|
||
_marker = object() | ||
|
@@ -309,6 +311,57 @@ def create_bucket(self, bucket_or_name, requester_pays=None, project=None): | |
bucket.create(client=self, project=project) | ||
return bucket | ||
|
||
def download_blob_to_file(self, blob_or_uri, file_obj, start=None, end=None): | ||
"""Download the contents of a blob object or blob URI into a file-like object. | ||
|
||
Args: | ||
blob_or_uri (Union[ \ | ||
:class:`~google.cloud.storage.blob.Blob`, \ | ||
str, \ | ||
]): | ||
The blob resource to pass or URI to download. | ||
file_obj (file): | ||
A file handle to which to write the blob's data. | ||
start (int): | ||
Optional. The first byte in a range to be downloaded. | ||
end (int): | ||
Optional. The last byte in a range to be downloaded. | ||
|
||
Examples: | ||
Download a blob using using a blob resource. | ||
|
||
>>> from google.cloud import storage | ||
>>> client = storage.Client() | ||
|
||
>>> bucket = client.get_bucket('my-bucket-name') | ||
>>> blob = storage.Blob('path/to/blob', bucket) | ||
|
||
>>> with open('file-to-download-to') as file_obj: | ||
>>> client.download_blob_to_file(blob, file) # API request. | ||
|
||
|
||
Download a blob using a URI. | ||
|
||
>>> from google.cloud import storage | ||
>>> client = storage.Client() | ||
|
||
>>> with open('file-to-download-to') as file_obj: | ||
>>> client.download_blob_to_file( | ||
>>> 'gs://bucket_name/path/to/blob', file) | ||
|
||
|
||
""" | ||
try: | ||
blob_or_uri.download_to_file(file_obj, client=self, start=start, end=end) | ||
except AttributeError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason for looking for |
||
scheme, netloc, path, query, frag = urlsplit(blob_or_uri) | ||
if scheme != "gs": | ||
raise ValueError("URI scheme must be gs") | ||
bucket = Bucket(self, name=netloc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's raise a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll need a test for this check. (Try a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added an |
||
blob_or_uri = Blob(path, bucket) | ||
|
||
blob_or_uri.download_to_file(file_obj, client=self, start=start, end=end) | ||
|
||
def list_buckets( | ||
self, | ||
max_results=None, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's give an example of the URI
gs://bucket_name/path/to/blob
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added examples for URI and if passing in a resource object.