Skip to content

Commit

Permalink
Merge pull request #38 from imandra-ai/object-deletion
Browse files Browse the repository at this point in the history
Add ability to delete objects
  • Loading branch information
benbellick authored Dec 16, 2024
2 parents 2be6454 + 2a53e15 commit d3995c0
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,34 @@ let list_objects ?(delimiter : string option) ?(prefix : string option)
| `OK ->
Error.parse_body_json list_objects_response_of_yojson body |> Lwt.return
| status_code -> Error.of_response_status_code_and_body status_code body

let delete_object (bucket_name : string) (object_path : string) :
(unit, [> Error.t ]) Lwt_result.t =
let open Lwt_result.Syntax in
let* token_info =
Common.get_access_token ~scopes:[ Scopes.devstorage_read_write ] ()
in
let* resp, body =
Lwt.catch
(fun () ->
let uri =
Uri.make () ~scheme:"https" ~host:"storage.googleapis.com"
~path:
(Printf.sprintf "storage/v1/b/%s/o/%s" bucket_name
(Uri.pct_encode object_path))
in
let headers =
Cohttp.Header.of_list
[
( "Authorization",
Printf.sprintf "Bearer %s" token_info.Auth.token.access_token );
]
in
let open Lwt.Infix in
Cohttp_lwt_unix.Client.delete uri ~headers >>= Util.consume_body |> ok)
(fun e -> Lwt_result.fail (`Network_error e))
in
match Cohttp.Response.status resp with
(* Deletion returns a 204 *)
| Cohttp.Code.(#success_status) -> Lwt_result.return ()
| status_code -> Error.of_response_status_code_and_body status_code body

0 comments on commit d3995c0

Please sign in to comment.