Fork me on GitHub
#tools-build
<
2022-11-22
>
Ben Lieberman15:11:17

Hi, I am trying to use the Cognitect AWS API from Java. I have this .clj file:

(ns bhlieberman.s3.client
  (:require [cognitect.aws.client.api :as aws]
            [cognitect.aws.credentials :refer [basic-credentials-provider]])
  (:import [javax.imageio ImageIO]
           [ File BufferedInputStream]))

(gen-class
 :name bhlieberman.s3.client.Client
 :main false
 :prefix "-"
 :methods [^{:static true} [getImage [obj] void]])

(defn write-image [^BufferedInputStream is]
  (-> is ImageIO/read (ImageIO/write "jpg" (File. "test.jpg"))))

(defn -getImage [obj]
  (let [s3 (aws/client {:api :s3
                        :region "us-west-2"
                        :credentials-provider
                        (basic-credentials-provider {:access-key-id (System/getenv "AWS_ACCESS_KEY_ID")
                                                     :secret-access-key (System/getenv "AWS_SECRET_KEY")})})
        opts {:op :GetObject
              :request {:Bucket "images"
                        :Key obj}}]
    (-> s3 (aws/invoke opts) :Body write-image)))
I guess I'm not sure if this is properly a tools-build Q, but when I compile this into a .jar (compilation works fine) and put it in my build.gradle, I can't import it as import bhlieberman.s3.client.Client. I think I ought to be able to do that. I tried specifying the .jar itself as a Gradle dep and also the entire /target directory. Neither works.

Alex Miller (Clojure team)15:11:42

Does the jar have classes in it?

Ben Lieberman15:11:36

Meaning other classes besides the gen-class'ed Client?

Ben Lieberman15:11:47

Before I switched to tools-build for this and was just using compile at the REPL it was outputting class files for every function in the namespace, which I understand to be the expected behavior.

Ben Lieberman15:11:45

These are the contents of the outputted /target dir.

Ben Lieberman16:11:44

I learned about jar tf jar-file all of about 30sec ago, but here's the output of that:

jar tf .\s3.client-0.0.1.jar
META-INF/MANIFEST.MF
bhlieberman/
META-INF/
bhlieberman/s3/
META-INF/maven/
bhlieberman/s3/client.clj
META-INF/maven/com.bhlieberman/
META-INF/maven/com.bhlieberman/s3.client/
META-INF/maven/com.bhlieberman/s3.client/pom.properties
META-INF/maven/com.bhlieberman/s3.client/pom.xml

Alex Miller (Clojure team)16:11:20

So how are you compiling?

Ben Lieberman16:11:40

clj -T:build jar

Alex Miller (Clojure team)16:11:14

And does your build script compile classes?

Ben Lieberman16:11:06

I guess it does not. Do I need to add compile-clj to my build script?

Ben Lieberman16:11:50

Thank you Alex!