Fork me on GitHub
#aws
<
2023-10-27
>
cddr09:10:06

Hey Folks, I’m trying to do something that I think is possible but I can’t figure out the correct syntax or find any examples in the docs. Here’s my code. I’m trying to mock out bits of the S3 API in order to write a test. This is just an example to demonstrate my problem. I thought that I can use the client-opts to make the amazon client hit localhost instead of aws. But when I try to put-object as below, it is trying to coerce the keyword :input-stream into an object of instance InputStream. This happens whether or not I wrap the key/input-stream parameters in a map.

(defn s3-app [store]
  {"/" {:put (fn [req]
              (swap! store assoc (req/path-info req)
                     (req/body-string req)))

       :get (fn [req]
              (get @store (req/path-info req)))}})

(let [store (atom {})]
  (with-http [(:s3 ports) (s3-app store)]
    (let [client-opts {:cred (DefaultAWSCredentialsProviderChain/getInstance)
                       :endpoint "localhost"
                       :region "eu-west-2"
                       :client-config
                       {:path-style-access-enabled true
                        :chunked-encoding-disabled false
                        :accelerate-mode-enabled false
                        :payload-signing-enabled true
                        :dualstack-enabled true
                        :force-global-bucket-access-enabled true}}]
      (s3/put-object
       client-opts
       {:key "foo"
        :input-stream (ByteArrayInputStream. (.getBytes "foo"))}))))
All the examples I could find in the amazonica docs/tests that configure client-opts and use put-object do so with a file instead of an input stream. Can anyone see what I’m doing wrong? Cheers.

lukasz14:10:04

I'm not 100% sure what are you trying to do actually - what is running on localhost? :thinking_face: How about this approach: What I did (and still do), is take a different route when testing S3-related code: rather than mock the client lib or even raw HTTP requests, I use https://github.com/minio/minio as part of the local & test setup. It's light-weight enough that it can just sit there running in Docker and do its thing. Then the only part you configure is the endpoint of your S3 client. This has several advantages: • you don't mock anything • you can use the same client to verify what gets written and how it's read • in my experience it's just a simpler setup overall I don't remember if amazonica helps with this, but when using S3 SDK directly, the "endpoint" config key needs to be a full URL and not a hostname, eg.. http://localhost:8080

👍 1