Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy with generation preconditions #14

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Added
* PR #13 copy with preconditions by @rfb.

### Changed

Expand Down
19 changes: 16 additions & 3 deletions src/clj_gcloud/storage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,22 @@
(.build builder)))

(defn copy
[^Storage storage ^BlobId source-blob-id ^BlobId target-blob-id]
(-> (.copy storage (Storage$CopyRequest/of source-blob-id target-blob-id))
(.getResult)))
([storage source-blob-id target-blob-id]
(copy storage source-blob-id target-blob-id nil))
([^Storage storage ^BlobId source-blob-id ^BlobId target-blob-id options]
(let [cr (if (:with-precondition options)
;; 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
(let [precondition (if-let [target (.get storage target-blob-id)]
(Storage$BlobTargetOption/generationMatch (.getGeneration target))
(Storage$BlobTargetOption/doesNotExist))]
(-> (Storage$CopyRequest/newBuilder)
(.setSource source-blob-id)
(.setTarget target-blob-id (into-array Storage$BlobTargetOption [precondition]))
edporras marked this conversation as resolved.
Show resolved Hide resolved
.build))
(Storage$CopyRequest/of source-blob-id target-blob-id))]
(-> (.copy storage cr)
(.getResult)))))

(defn create-blob
[^Storage storage ^BlobInfo blob-info]
Expand Down