Skip to content

Commit

Permalink
Added copy-with-generation-preconditions
Browse files Browse the repository at this point in the history
Added copy-with-generation-preconditions to the convenience functions
section. The purpose of this function is to set some preconditions
before the copy takes place to avoid race conditions where files are
added just before the copy.

I can imagine the "Convenience functions" section becoming unwieldy.
There is probably a more composable design here which I'm happy to work
towards if anyone has feedback.
  • Loading branch information
rfb authored and edporras committed Jun 28, 2024
1 parent 98fff85 commit ccd82a8
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/clj_gcloud/storage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@
;; Convenience functions
;;

(defn copy-with-generation-preconditions
"Copy blobs within cloud storage using preconditions recommended in Google Cloud Storage guides
See: https://cloud.google.com/storage/docs/copying-renaming-moving-objects#storage-copy-object-java"
[^Storage storage ^BlobId source-blob-id ^BlobId target-blob-id]
(let [precondition (if-let [target (.get storage target-blob-id)]
(Storage$BlobTargetOption/generationMatch (.getGeneration target))
(Storage$BlobTargetOption/doesNotExist))
builder (-> (Storage$CopyRequest/newBuilder)
(.setSource source-blob-id)
(.setTarget target-blob-id (into-array Storage$BlobTargetOption [precondition]))
.build)]
(-> (.copy storage builder)
(.getResult))))

(defn copy-file-to-storage
"Convenience function for copying a local file to a blob to storage.
By default the type is JSON encoded in UTF-8"
Expand Down

0 comments on commit ccd82a8

Please sign in to comment.