Fork me on GitHub
#aws
<
2020-04-03
>
arohner17:04:34

does anyone have an example of downloading a binary file from S3? I’ve attempted to use cognitect, amazonica, and clj-aws-s3, and all download a file, but with different MD5s than aws-cli

arohner17:04:33

what I’m doing seems pretty simple:

(let [resp (aws/invoke s3-client {:op :GetObject :request {:Bucket bucket :Key filename}})]
    (io/copy (:Body resp) (io/output-stream (io/file filename)))
    resp)

arohner17:04:38

hrm, the pure java version works:

(let [java-client (com.amazonaws.services.s3.AmazonS3ClientBuilder/defaultClient)]
    (with-open [obj (.getObject java-client (com.amazonaws.services.s3.model.GetObjectRequest. bucket key))]
      (java.nio.file.Files/copy (.getObjectContent obj) (.toPath (io/file dest-path)) (into-array java.nio.file.CopyOption [java.nio.file.StandardCopyOption/REPLACE_EXISTING]))))

ghadi17:04:34

different MD5s from different clients is suspicious

arohner17:04:37

$ md5 datomic-pro-*.zip
MD5 (datomic-pro-0.9.6045-amazonica.zip) = 9b1c544ce7b68a6d4bcc7477c501cf6a
MD5 (datomic-pro-0.9.6045-aws-cli.zip) = 50d60403c5cf505ac12f31a7081995b5
MD5 (datomic-pro-0.9.6045-clj-s3.zip) = 9b1c544ce7b68a6d4bcc7477c501cf6a
MD5 (datomic-pro-0.9.6045-cognitect.zip) = 3d95d706e81ea53363b2c3f29b64ec74
MD5 (datomic-pro-0.9.6045-java.zip) = 50d60403c5cf505ac12f31a7081995b5

arohner17:04:03

aws-cli and java are correct. Unsure why the others are wrong

hiredman17:04:12

try it without the io/output-stream around the io/file

hiredman17:04:32

are the file sizes different?

hiredman17:04:59

copy doesn't close streams that are passed in to it if I recall

4
arohner17:04:13

-rw-r--r--   1 arohner  staff  333382279 Apr  3 10:57 datomic-pro-0.9.6045-amazonica.zip
-rw-r--r--   1 arohner  staff  333390244 Apr  3 10:12 datomic-pro-0.9.6045-aws-cli.zip
-rw-r--r--   1 arohner  staff  333382279 Apr  3 11:12 datomic-pro-0.9.6045-clj-s3.zip
-rw-r--r--   1 arohner  staff  333389824 Apr  3 11:29 datomic-pro-0.9.6045-cognitect.zip
-rw-r--r--   1 arohner  staff  333390244 Apr  3 12:33 datomic-pro-0.9.6045-java.zip

ghadi18:04:48


([input output & opts])
  Copies input to output.  Returns nil or throws IOException.
  Input may be an InputStream, Reader, File, byte[], char[], or String.
  Output may be an OutputStream, Writer, or File.

  Options are key/value pairs and may be one of

    :buffer-size  buffer size to use, default is 1024.
    :encoding     encoding to use if converting between
                  byte and char streams.

  Does not close any streams except those it opens itself
  (on a File).

arohner18:04:32

yeah, that was it, thanks