Skip to content

Commit

Permalink
adds support for metadata during object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
CNDW committed Sep 13, 2024
1 parent 0d623b8 commit d9447d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/gcp_storage_emulator/handlers/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def _create_resumable_upload(request, response, storage):
object_id,
content_type,
content_length,
metadata={**(request.data or {}), "name": object_id},
)
id = storage.create_resumable_upload(
request.params["bucket_name"],
Expand Down
18 changes: 18 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,24 @@ def test_upload_from_file_content_type_json(self):
self.assertEqual(blob.name, file_name)
self.assertEqual(blob.download_as_bytes(), content)

def test_upload_from_file_with_metadata(self):
file_name = "test.json"
content = b'[{"a": 1}]'
bucket = self._client.create_bucket("testbucket")
blob = bucket.blob(file_name)
blob.metadata = {"foo": "bar"}

with NamedTemporaryFile() as temp_file:
temp_file.write(content)
temp_file.flush()
temp_file.seek(0)
blob.upload_from_file(temp_file, content_type="application/json")

blob = bucket.get_blob(file_name)
self.assertEqual(blob.name, file_name)
self.assertEqual(blob.download_as_bytes(), content)
self.assertEqual(blob.metadata, {"foo": "bar"})


class HttpEndpointsTest(ServerBaseCase):
"""Tests for the HTTP endpoints defined by server.HANDLERS."""
Expand Down

0 comments on commit d9447d0

Please sign in to comment.