我是一名 Ruby 开发人员,正在转向 Clojure,但我无法理解如何根据 Clojure 库中使用的约定将以下 Java 调用转换为 Clojure Amazonica .
AmazonS3 client = new AmazonS3Client(credentials);
client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
我目前的代码是:
(ns spurious-aws-sdk-helper.core
(:use [amazonica.aws.s3]])
(:require [amazonica.core :refer [ex->map]]))
(def credentials {:access-key "development_access"
:secret-key "development_secret"
:endpoint "s3.spurious.localhost:49154"
:client-config {:protocol "http"}})
(try
(amazonica.aws.s3/set-s3client-options {:path-style-access true})
(create-bucket credentials "testing")
(catch Exception e
(clojure.pprint/write (ex->map e))))
但我收到以下错误:
com.amazonaws.http.AmazonHttpClient executeHelper
INFO: Unable to execute HTTP request: testing.s3.spurious.localhost
java.net.UnknownHostException: testing.s3.spurious.localhost
这看起来不正确,因为它将存储桶名称 (
testing
) 添加到主机名上。因为我需要 SDK 使用路径样式与我们的本地(假)S3 服务( s3.spurious.localhost:49154
)通信。例如喜欢
http://s3.spurious.localhost:49154/testing
我认为这是因为我没有正确翻译 Java 代码......
(amazonica.aws.s3/set-s3client-options {:path-style-access true})
...这是将 map 传递给
set-s3client-options
而不是它应该是什么,它传递了调用 withPathStyleAccess(true)
的结果在 S3ClientOptions
的新实例上.但我不知道这里怎么做?任何帮助将不胜感激。
更新
这是代码的最新版本(仍然不起作用)...
(ns spurious-aws-sdk-helper.core
(:use [amazonica.aws.s3])
(:require [amazonica.core :refer [ex->map]]))
(def credentials {:access-key "development_access"
:secret-key "development_secret"
:endpoint "s3.spurious.localhost:49154"
:client-config {:protocol "http"}})
(try
(amazonica.aws.s3/set-s3client-options
(. (com.amazonaws.services.s3.S3ClientOptions.) setPathStyleAccess true))
(create-bucket credentials "testing")
(catch Exception e
(clojure.pprint/write (ex->map e))))
最佳答案
您第一次调用设置 S3ClientOptions 是正确的。
我认为您对该设置的作用感到困惑。
它更改了使用虚拟托管样式的预签名 URL 生成 https://my_bucket_name.s3.amazonaws.com/my_key
到路径样式 https://s3.amazonaws.com/my_bucket_name/my_key
如果您的存储桶名称包含点,则这是必要的,因为虚拟托管样式破坏了 Amazon 的 SSL 证书标识(即它仅支持通配符 *.s3.amazonaws.com
)
因此,如果您在 Clojure 中在包含点的存储桶名称上生成预签名 URL,例如
(s3/generate-presigned-url bucket key (-> 6 hours from-now)))
您首先需要使用设置路径样式
(amazonica.aws.s3/set-s3client-options {:path-style-access true})
关于amazon-web-services - 如何使用 Amazonica 库在 Clojure 中设置 S3 路径样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28340338/