我是一个Ruby,正在转到Clojure,我很难理解如何根据Clojure库亚马逊河中使用的约定将下面的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使用path样式与本地(假) S3服务(s3.spurious.localhost:49154)对话。
比如http://s3.spurious.localhost:49154/testing
我想是因为我没有正确地翻译Java代码.
(amazonica.aws.s3/set-s3client-options {:path-style-access true})...this将一个映射传递给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))))发布于 2015-09-28 14:59:32
感谢@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 (如果没有它们,它就无法工作)
发布于 2015-09-28 12:57:46
设置S3ClientOptions的第一个调用是正确的。
我觉得你搞不懂那个设置是怎么做的。它将预先签名的URL生成从使用虚拟宿主样式https://my_bucket_name.s3.amazonaws.com/my_key更改为路径样式https://s3.amazonaws.com/my_bucket_name/my_key。
如果桶名包含点,则这是必要的,因为虚拟托管样式破坏了Amazon证书标识(也就是说,它只支持通配符*.s3.amazonaws.com)。
因此,如果您要在一个桶名上生成预先签名的URL,其中包含Clojure中的点。
(s3/generate-presigned-url bucket key (-> 6 hours from-now)))您将首先需要使用
(amazonica.aws.s3/set-s3client-options {:path-style-access true})https://stackoverflow.com/questions/28340338
复制相似问题