简体   繁体   English

在 Clojurescript 中,如何使用 AWS javascript SDK 列出 S3 存储桶?

[英]In Clojurescript, how do I use AWS javascript SDK to list S3 buckets?

I am just getting started with Clojurescript.我刚刚开始使用 Clojurescript。 I wrote some clojurescript code to use the shared aws credentials file to initialize S3 client and list buckets.我编写了一些 clojurescript 代码来使用共享的 aws 凭证文件来初始化 S3 客户端并列出存储桶。 However my code does not work.但是我的代码不起作用。

(defn -main [arg1 arg2]
  (println "hello")
  (let[ creds (new AWS/SharedIniFileCredentials #js{:profile "superman"}) 
       _ (AWS/config.update creds) 
       ; dump out the accesskey to check if it has the correct profile
       _ (AWS/config.getCredentials (fn [err] (if (nil? err) (do
                                                               (println "its good")
                                                               (println AWS/config.credentials.accessKeyId)))))
       s3 (new (.-S3 AWS ))
       ] (.listBuckets s3 (fn[err buckets] (println "err: " err) (println buckets) )) ))

The AWS/config.getCredentials in the above does pick up the correct profile as seen from (println AWS/config.credentials.accessKeyId) .上面的 AWS/config.getCredentials 确实选择了正确的配置文件,如(println AWS/config.credentials.accessKeyId)所示。 The listbuckets code throws the following error: listbuckets 代码引发以下错误:

#object[NodeError TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received undefined]

I have Google AWS SDK S3 clojuresript AND is the only link I found.我有 Google AWS SDK S3 clojuresript 并且是我找到的唯一链接 I used that to configure the S3 client but that does not seem to work我用它来配置 S3 客户端,但这似乎不起作用

I would appreciate any help.我将不胜感激任何帮助。

I checked it and the problem seems to be that SDK expects the credentials to be set before anything else, before instantiating the S3 client.我检查了它,问题似乎是 SDK 希望在实例化 S3 客户端之前先设置凭据。

The following works for me on a minimal project with shadow-cljs:以下适用于我使用 shadow-cljs 的最小项目:

(ns server.main
  (:require ["aws-sdk" :as aws]))

(defn main! []
  (println "App loaded...")
  (let [creds (aws/SharedIniFileCredentials. #js {:profile "example-profile"})
        _     (set! (.-credentials aws/config) creds)
        s3    (aws/S3.)]
    (.listBuckets s3 (fn [err data]
                       (if err
                         (println "ERROR:" err)
                         (println "OK:" data))))))

when I run it:当我运行它时:

$ node target/main.js

App loaded...
OK: #js {:Buckets #js [#js {:Name dummy-test-bucket, :CreationDate #inst "2019-05-05T17:32:17.000-00:00"} #js {:Name mydomain.com, :CreationDate #inst "2018-06-19T04:16:10.000-00:00"}], :Owner #js {:DisplayName username, :ID f63f30bc25ab3609b8d3b5be6b3a872dd2c9f7947b2d509e2338357d93e74f2}}

The key was on this answer: https://stackoverflow.com/a/33175424/483566关键在于这个答案: https://stackoverflow.com/a/33175424/483566

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用amazon-javascript-sdk列出AWS S3存储桶? - How to list AWS S3 buckets using amazon-javascript-sdk? 没有使用 aws-sdk-php 列出 amazon aws s3 的存储桶? - No buckets list amazon aws s3 with aws-sdk-php? 是否可以列出我有权访问的所有AWS S3存储桶? - Is it possible to list all the AWS S3 buckets I have access to? 如何使用 AWS S3 存储桶,在没有云端的情况下进行视频流传输 - How to use AWS S3 buckets, for video streaming without cloudfront 使用AWS Powershell Commandlet,如何为S3存储桶指定其他终结点 - With AWS Powershell commandlets, how do I specify a different endpoint for S3 buckets 如何使用适用于javascript的aws-sdk将图像从iOS应用程序(使用Kony构建)上载到AWS S3? - How do I upload an image from iOS app (built with Kony) to AWS S3 using aws-sdk for javascript? 创建 S3 存储桶 nodejs 时出现 aws-sdk 问题 - Problem with aws-sdk while creating S3 Buckets nodejs 使用PHP SDK列出AWS S3存储桶 - Listing AWS S3 buckets using the PHP SDK 如何保护我的 Amazon S3 存储桶 - How Do I Secure My Amazon S3 Buckets 如果 Amazon S3 存储桶在整个 AWS 云中应该是“全球唯一的”,那么我如何拥有一个名为“todo”的存储桶? - If Amazon S3 buckets are supposed to be "globally unique" across the whole AWS cloud, how do I have a bucket named "todo"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM