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

feat: Add encryption_key param to Blob.from_string #391

Closed
wants to merge 5 commits into from
Closed
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
23 changes: 14 additions & 9 deletions google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,36 +344,41 @@ def public_url(self):
)

@classmethod
def from_string(cls, uri, client=None):
def from_string(cls, uri, client=None, encryption_key=None):
"""Get a constructor for blob object by URI.

:type uri: str
:param uri: The blob uri pass to get blob object.
:type uri: str
:param uri: The blob uri pass to get blob object.

:type client: :class:`~google.cloud.storage.client.Client`
:param client:
(Optional) The client to use. If not passed, falls back to the
``client`` stored on the blob's bucket.

:type encryption_key: bytes
:param encryption_key:
(Optional) 32 byte encryption key for customer-supplied encryption.
See https://cloud.google.com/storage/docs/encryption#customer-supplied.

:rtype: :class:`google.cloud.storage.blob.Blob`
:returns: The blob object created.
:rtype: :class:`google.cloud.storage.blob.Blob`
:returns: The blob object created.

Example:
Get a constructor for blob object by URI..
Example:
Get a constructor for blob object by URI.

>>> from google.cloud import storage
>>> from google.cloud.storage.blob import Blob
>>> client = storage.Client()
>>> blob = Blob.from_string("gs://bucket/object")
"""
"""
from google.cloud.storage.bucket import Bucket

scheme, netloc, path, query, frag = urlsplit(uri)
if scheme != "gs":
raise ValueError("URI scheme must be gs")

bucket = Bucket(client, name=netloc)
return cls(path[1:], bucket)
return cls(path[1:], bucket, encryption_key=encryption_key)

def generate_signed_url(
self,
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -4673,6 +4673,19 @@ def test_from_string_w_domain_name_bucket(self):
self.assertEqual(blob.name, "b")
self.assertEqual(blob.bucket.name, "buckets.example.com")

def test_from_string_w_encryption_key(self):
from google.cloud.storage.blob import Blob

connection = _Connection()
client = _Client(connection)
uri = "gs://bucket-name/blob-name"
encryption_key = b"0123456789abcdef0123456789abcdef"
blob = Blob.from_string(uri, client, encryption_key=encryption_key)

self.assertIsInstance(blob, Blob)
self.assertIs(blob.client, client)
self.assertEqual(blob._encryption_key, encryption_key)

def test_open(self):
from io import TextIOWrapper
from google.cloud.storage.fileio import BlobReader
Expand Down