diff --git a/gcp_storage_emulator/server.py b/gcp_storage_emulator/server.py index cfa0ba8..8982dad 100644 --- a/gcp_storage_emulator/server.py +++ b/gcp_storage_emulator/server.py @@ -163,7 +163,7 @@ def _decode_raw_data(raw_data, request_handler): return raw_data -def _read_data(request_handler): +def _read_data(request_handler, query): raw_data = _decode_raw_data(_read_raw_data(request_handler), request_handler) if not raw_data: @@ -171,7 +171,7 @@ def _read_data(request_handler): content_type = request_handler.headers["Content-Type"] or "application/octet-stream" - if content_type.startswith("application/json"): + if content_type.startswith("application/json") and "upload_id" not in query: return json.loads(raw_data) if content_type.startswith("multipart/"): @@ -251,7 +251,7 @@ def params(self): @property def data(self): if not self._data: - self._data = _read_data(self._request_handler) + self._data = _read_data(self._request_handler, self._query) return self._data def get_header(self, key, default=None): diff --git a/gcp_storage_emulator/storage.py b/gcp_storage_emulator/storage.py index 2800f25..ae11bd9 100644 --- a/gcp_storage_emulator/storage.py +++ b/gcp_storage_emulator/storage.py @@ -243,7 +243,7 @@ def add_to_resumable_upload(self, file_id, content, total_size): """ safe_id = self.safe_id(file_id) try: - file_content = self.get_file(RESUMABLE_DIR, safe_id) + file_content = self.get_file(RESUMABLE_DIR, safe_id, False) except NotFound: file_content = b"" file_content += content @@ -292,12 +292,13 @@ def get_resumable_file_obj(self, file_id): except KeyError: raise NotFound - def get_file(self, bucket_name, file_name): + def get_file(self, bucket_name, file_name, show_error=True): """Get the raw data of a file within a bucket Arguments: bucket_name {str} -- Name of the bucket file_name {str} -- File name + show_error {bool} -- Show error if the file is missing Raises: NotFound: Raised when the object doesn't exist @@ -310,8 +311,9 @@ def get_file(self, bucket_name, file_name): bucket_dir = self._fs.opendir(bucket_name) return bucket_dir.open(file_name, mode="rb").read() except (FileExpected, ResourceNotFound) as e: - logger.error("Resource not found:") - logger.error(e) + if show_error: + logger.error("Resource not found:") + logger.error(e) raise NotFound def delete_resumable_file_obj(self, file_id): diff --git a/tests/test_server.py b/tests/test_server.py index 572ab1d..d2dc5cb 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -867,6 +867,22 @@ def test_media_upload_without_metadata(self): self.assertEqual(blob_content, file.read()) self.assertEqual(blob.content_type, "text/plain") + def test_upload_from_file_content_type_json(self): + file_name = "test.json" + content = b'[{"a": 1}]' + bucket = self._client.create_bucket("testbucket") + blob = bucket.blob(file_name) + + 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) + class HttpEndpointsTest(ServerBaseCase): """Tests for the HTTP endpoints defined by server.HANDLERS."""