5

我是一名迁移到 Clojure 的 Ruby 开发人员,我无法理解如何根据 Clojure 库Amazonica中使用的约定将以下 Java 调用转换为 Clojure 。

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) 添加到主机名上。我需要 SDKs3.spurious.localhost:49154使用路径样式与我们的本地(假)S3 服务()通信。

例如喜欢http://s3.spurious.localhost:49154/testing

我认为这是因为我没有正确翻译 Java 代码......

(amazonica.aws.s3/set-s3client-options {:path-style-access true})

...这是传递一个映射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))))
4

2 回答 2

1

感谢 @stukennedy 回答未决问题。我早就应该回来并用我在大约 8 个月前(2015 年 2 月 2 日)设法弄清楚并实施的实际解决方案更新了这个空间:

(ns spurious-aws-sdk-helper.s3
  (:use [amazonica.aws.s3])
  (:require [spurious-aws-sdk-helper.utils :refer [endpoint cred]]))

(defn resource [type]
  (endpoint type :spurious-s3))

(defn setup
  ([type]
   (set-s3client-options (cred (resource type)) :path-style-access true))
  ([type name]
   (try
     (let [credentials (cred (resource type))]
       (set-s3client-options credentials :path-style-access true)
       (create-bucket credentials name))
     (catch Exception e
       (prn "S3 Error: chances are you're creating a bucket that already exists")))))

如果您需要更多上下文,您可以在此处找到完整的详细信息:https ://github.com/Integralist/spurious-clojure-aws-sdk-helper

如果我没记错的话,我需要credentials通过 to set-s3client-options(没有它们就行不通)

于 2015-09-28T14:59:32.403 回答
1

您第一次调用设置 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})
于 2015-09-28T12:57:46.537 回答