问题标签 [aws-sdk-js]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1830 浏览

node.js - NodeJs 在 Promise.all() 中延迟每个承诺

我正在尝试更新一个不久前创建的使用 nodejs 的工具(我不是 JS 开发人员,所以我试图将代码拼凑在一起)并且卡在最后一个障碍。

新功能将采用 swagger .json 定义,使用'aws-sdk'适用于 JS 的开发工具包将端点与 AWS 服务上匹配的 API 网关进行比较,然后相应地更新网关。

代码在一个小的定义文件(大约 15 个端点)上运行良好,但是一旦我给它一个更大的定义文件,我就开始收到大量TooManyRequestsException错误。

我了解这是由于我对 API Gateway 服务的调用太快,需要延迟/暂停。这就是我卡住的地方

我试过添加;

  • 每个返回的承诺的延迟()
  • 在每个承诺中运行 setTimeout()
  • 为 Promise.all 和 Promise.mapSeries 添加延迟

目前我的代码遍历定义中的每个端点,然后将每个承诺的响应添加到一个承诺数组:

循环完成后,我运行以下命令:

我用 mapSeries 尝试过同样的方法(不走运)。

看起来 ( getMethodResponsepromise) 中的函数会立即运行,因此,无论我添加什么类型的延迟,它们都仍然只是执行。我的怀疑是我需要让 ( getMethodResponse) 返回一个函数,然后使用 mapSeries,但我也无法让它工作。

我试过的代码:包装getMethodResponse在这个:

然后在循环之后添加这个(并且在循环内 - 没有区别):

还尝试了许多其他建议:

这里

这里

请问有什么建议吗?

编辑

根据要求,一些额外的代码可以尝试查明问题。

当前使用一小组端点的代码(在 Swagger 文件中):

};

从那以后,我尝试添加它来代替 return Promise.all():

结果是这样的(每个端点都是一样的,有很多):

错误:TooManyRequestsException:请求过多 X 错误:TooManyRequestsException:请求过多 X 错误:TooManyRequestsException:请求过多

AWS SDK 在每个 Promise 中被调用 3 次,其功能是(从 getMethodResponse() 函数启动):

典型的 AWS 开发工具包文档指出,这是进行太多连续调用(太快)时的典型行为。我过去遇到过类似的问题,只需在被调用的代码中添加 .delay(500) 即可解决;

就像是:

编辑#2

我想以彻底的名义,包括我的整个.js文件。

0 投票
1 回答
997 浏览

javascript - DynamoDB 'createSet' 返回 'constructor' 而不是 'Set'

问题:

尝试创建 DynamoDB 集。

'set' 的正确日志值:

我在控制台输出中得到什么:

更新调用:

因为'set'的类型是'constructor'而不是'Set'。尝试更新 DynamoDB 中的集合时出现以下错误。

问题:

我真的在寻找任何关于什么会导致这种情况的提示。

我正在使用 webpack 来部署我的应用程序。在开发中运行 webpack 时,我能够获得正确的值,但是当我为生产构建时,它表现出不正确的行为。

这不一定是 AWS 问题。这可能是我缺乏对 javascript 或 webpack 的理解。

0 投票
3 回答
1660 浏览

amazon-web-services - aws-sdk 抱怨缺少实际存在的 cors 标头

使用 webpack 运行开发服务器,我正在尝试列出 S3 存储桶中的项目并使用浏览器中的 javascript aws-sdk 控制台输出结果。

这样做时,我收到此错误:

但是我在 webpack 开发服务器配置中设置了该标头,证明如下:

所以我在所有东西上都尝试了 *:

如果标题在那里并且错误说它丢失了,它可能是其他类似授权的东西吗?

如果它真的是标题设置,如果标题确实存在,下一步会是什么?

更新****

1:在 S3 上添加 CORS 设置虽然我相信它抱怨的标题不会在 S3 上:

此外,存储桶未启用公共或静态托管。

2:这有效:

来自标记答案给出的 URL

0 投票
1 回答
1822 浏览

javascript - 如何将 CognitoAuth 身份验证对象转换为 AWS-SDK 凭证

我有一个认知用户池,我可以使用以下代码成功登录我的应用程序:

我现在有一个身份验证对象,我想将其转换为我拥有的 aws-sdk 的某种凭证,以便我可以将项目列出到 S3 存储桶中,假设我附加的角色中有正确的策略。

像这样的东西,但意识到这不起作用:

这可能吗,如果可以,我该怎么做?

UDPATE

接受的答案很有效,并且帮助很大,在我遇到的麻烦中添加了一些补充内容以清晰明了。

这个链接帮助了我。

0 投票
1 回答
3859 浏览

amazon-web-services - AWS.S3.upload() 403 Error When Attempting Multipart Upload

TL;DR

When attempting to upload a file directly from the browser using the s3.upload() method provided by the AWS SDK for Javascript in the Browser combined with temporary IAM Credentials generated by a call to AWS.STS.getFederationToken() everything works fine for non-multipart uploads, and for the first part of a multipart upload.

But when s3.upload() attempts to send the second part of a multipart upload S3 responds with a 403 Access Denied error.

Why?



The Context

I'm implementing an uploader in my app that will enable multipart (chunked) uploads directly from the browser to my S3 bucket.

To achieve this, I'm utilizing the s3.upload() method of the AWS SDK for Javascript in the Browser, which I understand to be nothing more than sugar for its underlying utilization of new AWS.S3.ManagedUpload().

A simple illustration of what I'm attempting can be found here: https://aws.amazon.com/blogs/developer/announcing-the-amazon-s3-managed-uploader-in-the-aws-sdk-for-javascript/

Additionally, I'm also using AWS.STS.getFederationToken() as a means to vend temporary IAM User credentials from my API layer to authorize the uploads.

The 1,2,3:

  1. The user initiates an upload by choosing a file via a standard HTML <input type="file">.
  2. This triggers an initial request to my API layer to ensure the user has the necessary privileges on my own system to perform this action, if that's true then my server calls AWS.STS.getFederationToken() with a Policy param that scopes their privileges down to nothing more than uploading the file to the key provided. And then returns the resulting temporary creds to the browser.
  3. Now that the browser has the temp creds it needs, it can go about using them to create a new AWS.S3 client and then execute the AWS.S3.upload() method to perform a (supposedly) automagical multipart upload of the file.



The Code

api.myapp.com/vendUploadCreds.js

This is the API layer method called that generates and vends the temporary upload creds. At this point in the process the account has already been authenticated and authorized to receive the creds and upload the file.


console.myapp.com/uploader.js

This is a truncated illustration of the uploader on the browser-side that first calls the vendUploadCreds API method and then uses the resulting temporary creds to execute the multipart upload.


cdn.myapp.com S3 Bucket CORS Configuration

So far as I can tell, this is wide open, so CORS shouldn't be the issue?


The Error

Okay, so when I attempt to upload a file, it gets really confusing:

  1. Any file under 5Mb uploads just fine. Files under 5Mb (the minimum part size for an S3 Multipart Upload) do not require multipart upload so s3.upload() sends them as a standard PUT request. Makes sense, and they succeed just fine.
  2. Any file over 5Mb seems to upload fine, but only for the first part. Then when s3.upload() attempts to send the second part S3 responds with a 403 Access Denied error.

I hope you're a fan of info because here's a dump of the error that I get from Chrome when I attempt to upload Astrud Gilberto's melancholy classic "So Nice (Summer Samba)" (MP3, 6.6Mb):

General

Response Headers

Request Headers

Query String Params

Actual Response Body

And here's the body of the response from S3:


The Questions

  1. It's obviously not an issue with the creds created by the sts.generateFederationToken() request, because if it were then the smaller (non-multipart) uploads would fail as well, right?
  2. It's obviously not an issue with the CORS configuration on the cdn.myapp.com bucket, because if it were then the smaller (non-multipart) uploads would fail as well, right?
  3. Why would S3 accept partNumber=1 of a multipart upload, and then 403 on the partNumber=2 of the same upload?
0 投票
6 回答
12081 浏览

javascript - 等待 AWS SNS 发布回调将值返回给调用方法

当用户请求重置密码时,我正在尝试发送短信。我想等待发送消息以提醒用户它是否成功。我目前正在尝试按以下方式进行操作:

在我的另一个类 AWSSNSClient 中,我有以下 sendMessage 函数:

我无法弄清楚如何让 sendMessage 等待 sns.publish 在它返回之前返回。我尝试将其设为异步方法并在 sns.publish 上添加等待,但该函数仍会在 send 设置为 true 之前返回。

我知道消息发送没有错误,因为我正在接收它们并且没有打印控制台日志。

0 投票
1 回答
3078 浏览

amazon-s3 - 使用 Aws s3 JavaScript 从浏览器上传文件

AWS s3 js 是否将 sdk 文件直接从浏览器上传到 s3 存储桶?我目前正在使用 Php SDK 我先将文件上传到服务器然后将它们上传到 s3 存储桶但是这个过程消耗太多资源我在网络上找不到示例我正在等待您的帮助

0 投票
1 回答
4625 浏览

amazon-web-services - AccessDenied Invalid according to Policy: Policy Condition failed: ["starts-with", "$key", "2017/"]

I get the error I specified in the title.

I try to do: Uploading Objects with Signed URLs with php

the problem looks here: ['starts-with', '$key', 'test/']

['starts-with', '$key', 'test/'] How does this work?

in the bucket test folder exists.

Please help.

my code (server side php) :


Source Code

error

my CORS Config

0 投票
1 回答
4231 浏览

node.js - 超过预置吞吐量时应该怎么做?

我正在使用 AWS SDK for Javascript (Node.js) 从 DynamoDB 表中读取数据。Auto Scaling 功能在大部分时间都做得很好,并且消耗的读取容量单位 (RCU) 在一天的大部分时间里都非常低。但是,有一个在午夜左右执行的已编程作业消耗了大约 10 倍于预置 RCU 的消耗,并且由于自动缩放需要一些时间来调整容量,因此存在大量受限制的读取请求。此外,我怀疑我的请求没有完成(尽管我在错误日志中找不到任何异常)。

为了处理这种情况,我考虑过使用 AWS API ( updateTable ) 增加预置的 RCU,但计算我的应用程序需要的 RCU 数量可能并不简单。

所以我的第二个猜测是重试失败的请求并等待自动缩放增加预置的 RCU。正如 AWS 文档和一些 Stack Overflow 答案所指出的(特别是关于ProvisionedThroughputExceededException):

适用于 Amazon DynamoDB 的 AWS 开发工具包会自动重试收到此异常的请求。因此,您的请求最终会成功,除非请求太大或您的重试队列太大而无法完成。

我读过类似的问题(这个这个这个),但我仍然感到困惑:如果请求太大或重试队列太大而无法完成(因此在自动重试之后)是否会引发此异常或实际上在重试之前?

最重要的是:这是我在我的上下文中应该期待的例外吗?(所以我可以抓住它并重试,直到自动缩放增加 RCU?)

0 投票
2 回答
1980 浏览

node.js - 在 ECS 中运行时,适用于 node.js 的 AWS 开发工具包未获取凭证

我有这个代码:

当这段代码在 ecs 中运行时(它假设一个可以访问 S3 存储桶的角色),我会在日志中得到这个:

日志的 env 部分:

“AWS_CONTAINER_CREDENTIALS_RELATIVE_URI”存在于环境变量中,所以我认为 sdk 应该能够获取它。

日志的提供者部分只是 sdk 代码中定义的 4 个函数,如果在 env var 中看到“AWS_CONTAINER_CREDENTIALS_RELATIVE_URI”,则其中第 4 个函数将返回“ECSCredentials”。

奇怪的是,日志的 cred 部分在 ECS 中是“null”。当我在本地运行它时(我手动承担一个角色),日志的 cred 部分是“EnvironmentCredentials”,并且文件被上传到 S3。

日志的错误部分是:

我的代码或环境变量有什么问题吗?

我应该打印更多日志以帮助诊断此问题吗?

请指教,谢谢。

==========更新==========

我已经在 ECS 中运行了这段代码 const curlCommand = `curl http://169.254.170.2${process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}`; exec(curlCommand, (error, stdout, stderr) => { console.log(`${curlCommand} err:${error} out:${stdout} stderr:${stderr}`); });

我得到的结果是: { "SecretAccessKey": "long string", "Token": "very long string", "Expiration": "2018-01-14T08:55:04+0000", "AccessKeyId": "shorted string" }

因此,如果 sdk 调用了http://169.254.170.2 ${process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI},它应该得到正确的响应。