Skip to content

Commit

Permalink
refactor: integrate into copy via new options arg
Browse files Browse the repository at this point in the history
  • Loading branch information
edporras committed Jun 28, 2024
1 parent ccd82a8 commit cbb440b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
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
33 changes: 16 additions & 17 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]))
.build))
(Storage$CopyRequest/of source-blob-id target-blob-id))]
(-> (.copy storage cr)
(.getResult)))))

(defn create-blob
[^Storage storage ^BlobInfo blob-info]
Expand Down Expand Up @@ -209,20 +222,6 @@
;; 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 cbb440b

Please sign in to comment.