Skip to content

Commit

Permalink
Add rewrite endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-kaestner committed May 5, 2023
1 parent d2efa16 commit a3994a5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/gcp_storage_emulator/handlers/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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""
Expand Down
7 changes: 7 additions & 0 deletions src/gcp_storage_emulator/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def _health_check(req, res, storage):
+ r"(?P<dest_bucket_name>[-.\w]+)/o/(?P<dest_object_id>.*[^/]+)$",
{POST: objects.copy},
),
(
r"^{}/b/(?P<bucket_name>[-.\w]+)/o/(?P<object_id>.*[^/]+)/rewriteTo/b/".format(
settings.API_ENDPOINT
)
+ r"(?P<dest_bucket_name>[-.\w]+)/o/(?P<dest_object_id>.*[^/]+)$",
{POST: objects.rewrite},
),
(
r"^{}/b/(?P<bucket_name>[-.\w]+)/o/(?P<object_id>.*[^/]+)/compose$".format(
settings.API_ENDPOINT
Expand Down

0 comments on commit a3994a5

Please sign in to comment.