From a3994a58b5bb7df53d28daaf0d5cd1820f547fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20K=C3=A4stner?= Date: Fri, 5 May 2023 16:03:38 +0200 Subject: [PATCH] Add rewrite endpoint See: https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite --- src/gcp_storage_emulator/handlers/objects.py | 39 ++++++++++++++++++++ src/gcp_storage_emulator/server.py | 7 ++++ 2 files changed, 46 insertions(+) diff --git a/src/gcp_storage_emulator/handlers/objects.py b/src/gcp_storage_emulator/handlers/objects.py index bbd523a..e7508b7 100644 --- a/src/gcp_storage_emulator/handlers/objects.py +++ b/src/gcp_storage_emulator/handlers/objects.py @@ -399,6 +399,45 @@ def copy(request, response, storage, *args, **kwargs): _handle_conflict(response, err) +def rewrite(request, response, storage, *args, **kwargs): + try: + obj = storage.get_file_obj( + request.params["bucket_name"], request.params["object_id"] + ) + except NotFound: + response.status = HTTPStatus.NOT_FOUND + return + + dest_obj = _make_object_resource( + request.base_url, + request.params["dest_bucket_name"], + request.params["dest_object_id"], + obj["contentType"], + obj["size"], + obj, + ) + + file = storage.get_file(request.params["bucket_name"], request.params["object_id"]) + try: + dest_obj = _checksums(file, dest_obj) + storage.create_file( + request.params["dest_bucket_name"], + request.params["dest_object_id"], + file, + dest_obj, + ) + response.json({ + "resource": dest_obj, + "written": dest_obj["size"], + "size": dest_obj["size"], + "done": True, + }) + except NotFound: + response.status = HTTPStatus.NOT_FOUND + except Conflict as err: + _handle_conflict(response, err) + + def compose(request, response, storage, *args, **kwargs): content_type = None dest_file = b"" diff --git a/src/gcp_storage_emulator/server.py b/src/gcp_storage_emulator/server.py index 8982dad..d52e37a 100644 --- a/src/gcp_storage_emulator/server.py +++ b/src/gcp_storage_emulator/server.py @@ -55,6 +55,13 @@ def _health_check(req, res, storage): + r"(?P[-.\w]+)/o/(?P.*[^/]+)$", {POST: objects.copy}, ), + ( + r"^{}/b/(?P[-.\w]+)/o/(?P.*[^/]+)/rewriteTo/b/".format( + settings.API_ENDPOINT + ) + + r"(?P[-.\w]+)/o/(?P.*[^/]+)$", + {POST: objects.rewrite}, + ), ( r"^{}/b/(?P[-.\w]+)/o/(?P.*[^/]+)/compose$".format( settings.API_ENDPOINT