Skip to content

Commit

Permalink
[gcloud] use GS_BLOB_CHUNK_SIZE with files that already exist (#1154
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wigeria committed Jun 30, 2022
1 parent 620a133 commit 0c5638b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ By order of apparition, thanks:
* Dan Hook
* François Freitag (S3)
* Uxío Fuentefría (S3)
* wigeria (Google Cloud Storage patch)


Extra thanks to Marty for adding this in Django,
Expand Down
3 changes: 2 additions & 1 deletion storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def __init__(self, name, mode, storage):
self.mime_type = mimetypes.guess_type(name)[0]
self._mode = mode
self._storage = storage
self.blob = storage.bucket.get_blob(name)
self.blob = storage.bucket.get_blob(
name, chunk_size=storage.blob_chunk_size)
if not self.blob and 'w' in mode:
self.blob = Blob(
self.name, storage.bucket,
Expand Down
28 changes: 25 additions & 3 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def test_open_read(self):

f = self.storage.open(self.filename)
self.storage._client.bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob.assert_called_with(self.filename)
self.storage._bucket.get_blob.assert_called_with(
self.filename, chunk_size=None)

f.blob.download_to_file = lambda tmpfile: tmpfile.write(data)
self.assertEqual(f.read(), data)
Expand All @@ -49,7 +50,8 @@ def test_open_read_num_bytes(self):

f = self.storage.open(self.filename)
self.storage._client.bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob.assert_called_with(self.filename)
self.storage._bucket.get_blob.assert_called_with(
self.filename, chunk_size=None)

f.blob.download_to_file = lambda tmpfile: tmpfile.write(data)
self.assertEqual(f.read(num_bytes), data[0:num_bytes])
Expand All @@ -59,7 +61,8 @@ def test_open_read_nonexistent(self):
self.storage._bucket.get_blob.return_value = None

self.assertRaises(FileNotFoundError, self.storage.open, self.filename)
self.storage._bucket.get_blob.assert_called_with(self.filename)
self.storage._bucket.get_blob.assert_called_with(
self.filename, chunk_size=None)

def test_open_read_nonexistent_unicode(self):
filename = 'ủⓝï℅ⅆℇ.txt'
Expand Down Expand Up @@ -531,3 +534,22 @@ def test_override_init_argument(self):
self.assertEqual(storage.location, 'foo1')
storage = gcloud.GoogleCloudStorage(location='foo2')
self.assertEqual(storage.location, 'foo2')

def test_dupe_file_chunk_size(self):
"""
Tests that recreating a file that already exists in the bucket
respects the `GS_BLOB_CHUNK_SIZE` setting
"""
chunk_size = 1024*256

with override_settings(GS_BLOB_CHUNK_SIZE=chunk_size):
# Creating a new storage here since chunk-size is set as an
# attribute on init
storage = gcloud.GoogleCloudStorage()
storage._bucket = mock.MagicMock()
# Confirms that `get_blob` always returns a truthy value
storage._bucket.get_blob.return_value = True

storage.open(self.filename, 'wb')
storage._bucket.get_blob.assert_called_with(
self.filename, chunk_size=chunk_size)

0 comments on commit 0c5638b

Please sign in to comment.