Skip to content

Commit 48d9c83

Browse files
authored
Fix upload_from_file with "application/json" (#162)
1 parent b0a6d35 commit 48d9c83

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

gcp_storage_emulator/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ def _decode_raw_data(raw_data, request_handler):
163163
return raw_data
164164

165165

166-
def _read_data(request_handler):
166+
def _read_data(request_handler, query):
167167
raw_data = _decode_raw_data(_read_raw_data(request_handler), request_handler)
168168

169169
if not raw_data:
170170
return None
171171

172172
content_type = request_handler.headers["Content-Type"] or "application/octet-stream"
173173

174-
if content_type.startswith("application/json"):
174+
if content_type.startswith("application/json") and "upload_id" not in query:
175175
return json.loads(raw_data)
176176

177177
if content_type.startswith("multipart/"):
@@ -251,7 +251,7 @@ def params(self):
251251
@property
252252
def data(self):
253253
if not self._data:
254-
self._data = _read_data(self._request_handler)
254+
self._data = _read_data(self._request_handler, self._query)
255255
return self._data
256256

257257
def get_header(self, key, default=None):

gcp_storage_emulator/storage.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def add_to_resumable_upload(self, file_id, content, total_size):
243243
"""
244244
safe_id = self.safe_id(file_id)
245245
try:
246-
file_content = self.get_file(RESUMABLE_DIR, safe_id)
246+
file_content = self.get_file(RESUMABLE_DIR, safe_id, False)
247247
except NotFound:
248248
file_content = b""
249249
file_content += content
@@ -292,12 +292,13 @@ def get_resumable_file_obj(self, file_id):
292292
except KeyError:
293293
raise NotFound
294294

295-
def get_file(self, bucket_name, file_name):
295+
def get_file(self, bucket_name, file_name, show_error=True):
296296
"""Get the raw data of a file within a bucket
297297
298298
Arguments:
299299
bucket_name {str} -- Name of the bucket
300300
file_name {str} -- File name
301+
show_error {bool} -- Show error if the file is missing
301302
302303
Raises:
303304
NotFound: Raised when the object doesn't exist
@@ -310,8 +311,9 @@ def get_file(self, bucket_name, file_name):
310311
bucket_dir = self._fs.opendir(bucket_name)
311312
return bucket_dir.open(file_name, mode="rb").read()
312313
except (FileExpected, ResourceNotFound) as e:
313-
logger.error("Resource not found:")
314-
logger.error(e)
314+
if show_error:
315+
logger.error("Resource not found:")
316+
logger.error(e)
315317
raise NotFound
316318

317319
def delete_resumable_file_obj(self, file_id):

tests/test_server.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,22 @@ def test_media_upload_without_metadata(self):
867867
self.assertEqual(blob_content, file.read())
868868
self.assertEqual(blob.content_type, "text/plain")
869869

870+
def test_upload_from_file_content_type_json(self):
871+
file_name = "test.json"
872+
content = b'[{"a": 1}]'
873+
bucket = self._client.create_bucket("testbucket")
874+
blob = bucket.blob(file_name)
875+
876+
with NamedTemporaryFile() as temp_file:
877+
temp_file.write(content)
878+
temp_file.flush()
879+
temp_file.seek(0)
880+
blob.upload_from_file(temp_file, content_type="application/json")
881+
882+
blob = bucket.get_blob(file_name)
883+
self.assertEqual(blob.name, file_name)
884+
self.assertEqual(blob.download_as_bytes(), content)
885+
870886

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

0 commit comments

Comments
 (0)