Permalink
Browse files

Merge pull request #13 from adityacrao/s3-dir-copy

Added suport for s3 dir to dir copying
  • Loading branch information...
2 parents d9bf535 + 5e9549b commit 33f8de01c33036be4ab0abb410ff5791159aab34 @mlimotte mlimotte committed Sep 5, 2012
Showing with 23 additions and 6 deletions.
  1. +11 −5 src/main/clj/com/climate/services/aws/s3.clj
  2. +12 −1 src/test/clj/com/climate/services/aws/s3_test.clj
@@ -279,13 +279,19 @@
; s3 to s3 copy
(defmethod cp [true true]
[src dest & _]
- (let [dest (if (slash$? dest) (str dest (ccio/basename src)) dest)
+ (let [dest (if (and (not (slash$? src)) (slash$? dest)) (str dest (ccio/basename src)) dest)
[sbkt skey] (parse-s3path src)
[dbkt dkey] (parse-s3path dest)]
- (when-not (object? sbkt skey)
- (throw (UnsupportedOperationException.
- "cp within s3 only works for a single s3 object at this time, not a directory.")))
- (copy-object sbkt skey dbkt dkey)))
+ (if-not (object? sbkt skey)
+ ; If src is a directory, then recursively copy to dest
+ (let [lst (objects sbkt skey nil)
+ f (fn [key skey dkey] (s/replace-first key (slash!$ skey) (slash!$ dkey)))]
+ (->> lst
+ second
+ (map (memfn getKey))
+ (map #(copy-object sbkt % dbkt (f % skey dkey)))
+ doall))
+ (copy-object sbkt skey dbkt dkey))))
(defn- new-s3-dest
"Used by cp (for the s3 -> local case). Determines the new path name, and
@@ -173,4 +173,15 @@
(cp (s3p "foo/bar.txt") (s3p "/"))
; same dest as prev, should overwrite, no error:
(cp (s3p "foo/bar.txt") (s3p ""))
- (is (object? test-bucket "bar.txt")))))))
+ (is (object? test-bucket "bar.txt"))
+ ; create a nested dir to test dir/dir copying
+ ; foo/baz/bar.txt
+ (cp (s3p "foo/bar.txt") (s3p "foo/baz/"))
+ (cp (s3p "foo/") (s3p "dest/"))
+ (is (object? test-bucket "dest/baz/bar.txt"))
+ (cp (s3p "foo") (s3p "dest1/"))
+ (is (object? test-bucket "dest1/foo/baz/bar.txt"))
+ (cp (s3p "foo/") (s3p "dest2"))
+ (is (object? test-bucket "dest2/baz/bar.txt"))
+ (cp (s3p "foo") (s3p "dest3"))
+ (is (object? test-bucket "dest3/baz/bar.txt")))))))

0 comments on commit 33f8de0

Please sign in to comment.