Skip to content

Commit

Permalink
Delete snapshot tmp file
Browse files Browse the repository at this point in the history
  • Loading branch information
klauswuestefeld committed Sep 15, 2024
1 parent 209236d commit 65e7dfa
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/prevayler_clj_aws/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@
(let [v2-path (snapshot-v2-path snapshot-path)
snap1 (read-object s3-sdk-cli bucket snapshot-path unmarshal)]
(try
(when (snapshot-exists? s3-cli bucket v2-path)
(if (snapshot-exists? s3-cli bucket v2-path)
(let [snap2 (read-object s3-sdk-cli bucket v2-path unmarshal-from-in)]
(println "Snapshot v1" (if (= snap1 snap2) "IS" "IS NOT") "equal to v2")))
(println "Snapshot v1" (if (= snap1 snap2) "IS" "IS NOT") "equal to v2"))
(println v2-path "object not found in bucket."))
(catch Exception e
(.printStackTrace e)))
snap1)
Expand All @@ -65,12 +66,15 @@
:Body (marshal snapshot)}})
(try
(let [v2-path (snapshot-v2-path snapshot-path)
temp-file (java.io.File/createTempFile "snapshot" "")]
(with-open [temp-out (-> (java.io.FileOutputStream. temp-file) java.io.BufferedOutputStream. java.io.DataOutputStream.)]
(nippy/freeze-to-out! temp-out snapshot))
(with-open [temp-in (java.io.BufferedInputStream. (java.io.FileInputStream. temp-file))]
(.putObject s3-sdk-cli (PutObjectRequest. bucket v2-path temp-in (doto (ObjectMetadata.)
(.setContentLength (.length temp-file)))))))
temp-file (java.io.File/createTempFile "snapshot" "")] ; We use an intermediary file to easily determine the length of the stream. Otherwise, to determine its length, Amazon's SDK would buffer the entire stream in RAM, defeating our purpose.
(try
(with-open [temp-out (-> (java.io.FileOutputStream. temp-file) java.io.BufferedOutputStream. java.io.DataOutputStream.)]
(nippy/freeze-to-out! temp-out snapshot))
(with-open [temp-in (java.io.BufferedInputStream. (java.io.FileInputStream. temp-file))]
(.putObject s3-sdk-cli (PutObjectRequest. bucket v2-path temp-in (doto (ObjectMetadata.)
(.setContentLength (.length temp-file))))))
(finally
(.delete temp-file))))
(catch Exception e
(.printStackTrace e))))

Expand Down

0 comments on commit 65e7dfa

Please sign in to comment.