![](/img/trans.png)
[英]How do you change AWS.config in Clojure using Amazonica library?
[英]How to set S3 path style in Clojure using Amazonica library?
我是一個Ruby開發人員轉移到Clojure,我很難理解如何根據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
) 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)
的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})
感謝@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
(沒有它們就行不通)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.