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

Respecting GS_BLOB_CHUNK_SIZE for files that already exist #1154

Merged
merged 1 commit into from
Jun 30, 2022
Merged
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
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)