【问题标题】:How to set S3 path style in Clojure using Amazonica library?如何使用 Amazonica 库在 Clojure 中设置 S3 路径样式?
【发布时间】:2015-04-05 02:15:32
【问题描述】:

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

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

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

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

...这是将映射传递给set-s3client-options,而不是它应该传递的映射,这是传递在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 
    (. (com.amazonaws.services.s3.S3ClientOptions.) setPathStyleAccess true))
  (create-bucket credentials "testing")
  (catch Exception e
    (clojure.pprint/write (ex->map e))))

【问题讨论】:

    标签: amazon-web-services clojure amazonica


    【解决方案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})
    

    【讨论】:

    • 感谢您的反馈,我最初在 7 个多月前提出了这个问题,所以我最终设法弄清楚了(具体参见 github.com/Integralist/spurious-clojure-aws-sdk-helper/blob/…)。如果有人感兴趣,完整的工作项目在这里:github.com/Integralist/spurious-clojure-aws-sdk-helper
    • 您为什么不接受我的回答并点赞?如果我先正确回答了这个问题,那么稍后再进去,发布几乎相同的答案,接受这个答案,甚至不给我一个赞成票,这是不公平的。这应该是公认的答案;不是您稍后发布的那个。我以前做过同样的事情,在有人回应之前我独立地解决了问题。但我仍然给了他们接受的答案。
    • 我不相信您的答案是正确的,因为我已经以正确的方式设置了路径样式(您可以在我原来的问题的详细信息中看到)。实际错误出现在我的代码中的其他地方,如果您查看我去年 9 月对您的回复,我提到我最终自己解决了这个问题,但我还是要感谢您的反馈。
    【解决方案2】:

    感谢 @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 传递给set-s3client-options(没有它们就行不通)

    【讨论】:

      猜你喜欢
      • 2017-07-23
      • 2016-05-14
      • 2012-06-29
      • 2015-06-09
      • 2015-11-09
      • 2019-04-23
      • 1970-01-01
      • 2021-05-28
      • 2023-03-16
      相关资源
      最近更新 更多