Promise() 在 AWS lambda 中执行此操作。
我想在文件存储到 s3 后立即发送信号。我想知道 .promise() 在这里做什么。(--s3.putObject({}).promise()--)
当在 s3 存储上看到文件时,几乎在相同的时间点观察到 fetch(send_to_this_url) 的时间戳。基于此,promise() 并不是异步操作的。我这里有什么问题吗?
前几行中的第二行,当响应正常时返回响应。但是,如果它早点返回,文件就不会存储在 s3 中,但它已经存储在 s3 中,所以我想知道第一个 if(response.ok) 的目的是什么。
请与我分享任何想法。
fetch(url)
.then((response) => {
if (response.ok) {
return response;
}
return Promise.reject(new Error(
`Failed to fetch ${response.url}: ${response.status} ${response.statusText}`));
})
.then(response => response.buffer())
.then((buffer) => {
s3.putObject({
ACL: "public-read",
Bucket: process.env.BUCKET,
Key: key,
Body: buffer
}).promise();
try{
const send_to_this_url = "someurl?key=" + key;
fetch(send_to_this_url);
}catch(e){
callback('error' + e.message);
}
}
)
.then(v => callback(null, v), callback);
Run Code Online (Sandbox Code Playgroud) 我在从 AWS Lambda 创建 Cloudfront 失效时遇到问题。
我的情况非常基本:我设置了一个由特定 S3 对象创建和删除触发的 Lambda 处理程序,以便在我的 Cloudfront 发行版上执行缓存版本的失效操作。这是使用nodejs编写的函数代码:
const AWS = require('aws-sdk');
exports.handler = async function (event, context) {
const cloudFront = new AWS.CloudFront();
const invalidationParams = {
DistributionId: "XXXX",
InvalidationBatch: {
CallerReference: Date.now().toString(),
Paths: {
Quantity: 2,
Items: [
"/index.html",
"/service-worker.js"
]
}
}
};
cloudFront.createInvalidation(invalidationParams, (error, data) => {
if (error) {
console.log(error, error.stack);
} else {
console.log("Invalidation results", data);
}
});
};
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,没有什么太复杂的。现在,大多数时候处理程序执行时不执行任何操作,我会查看日志,除了请求 id 以及开始和结束时间戳之外,没有任何内容被打印,甚至没有 Cloudfront 错误,这让我想知道发生了什么。连续执行四到五次手动测试后,会正确创建失效,但日志不会报告它。再触发一次,然后打印上一次运行的失效结果。我觉得这非常奇怪和令人困惑。
从上下文和 Lambda 代码来看,我可能遗漏了什么吗?
谢谢。
我尝试通过调用deleteUsagePlan函数并传递usagePlanId参数来使用AWS SDK删除使用计划,如下所示:
const params: AWSNXR.APIGateway.DeleteUsagePlanRequest = {
usagePlanId: usage_plan_id,
};
const apigw = new AWS.APIGateway();
const response = await apigw.deleteUsagePlan(params).promise();
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,我无法这样做,并且收到此错误:BadRequestException:无法删除使用计划 USAGE_PLAN_ID_HERE,因为有与其关联的 API 阶段,包括 API_ID_HERE:测试
重现步骤:
1-使用以下参数创建使用计划
名称:“my_usage_plan”,apiStages:[ apiId:“API_ID_HERE”,阶段:“测试”]
2-删除使用计划
使用计划 ID:USAGE_PLAN_ID
我在步骤 #2 中不断收到错误消息: BadRequestException: 无法删除使用计划 USAGE_PLAN_ID_HERE 因为有与其关联的 API 阶段,包括 API_ID_HERE:test
预期行为:
预期的行为是能够使用使用计划 ID 删除新创建的使用计划
附加内容:
我尝试解决此问题的方法: 1- 我尝试删除 API 网关上的测试阶段 2- 我使用相同的使用计划 ID 运行删除使用计划功能
使用计划已成功删除,没有任何错误,但是,我应该能够删除它,而无需删除阶段并在之后重新创建它。
我正在开发 React/Node.js 应用程序,并尝试从 ~/.aws/credentials 文件读取我的 IAM 用户凭证。我正在尝试使用 @aws-sdk/credential-providers 节点包中的 fromIni 。根据AWS SDK v3文档,我可以执行以下操作:
\nimport { fromIni } from "@aws-sdk/credential-providers"; // ES6 import\n// const { fromIni } = require("@aws-sdk/credential-providers"); // CommonJS import\n\nconst client = new FooClient({\n credentials: fromIni({\n // Optional. The configuration profile to use. If not specified, the provider will use the value\n // in the `AWS_PROFILE` environment variable or a default of `default`.\n profile: "profile",\n // Optional. The path to the shared credentials file. If not …Run Code Online (Sandbox Code Playgroud) 任何人都可以解释在调用DocumentClient.get时如何使用GetItemInput类型,如果我传入任何类型的对象都可以工作,但是如果我尝试强烈键入params对象,则会收到错误消息
ValidationException:提供的键元素与架构不匹配
这是我的lambda函数代码,我在其中传递参数作为任何类型...
export const get: Handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
console.log(event.pathParameters)
if (!event.pathParameters) {
throw Error("no path params")
}
const params: any = {
Key: {
id: event.pathParameters.id
},
TableName: table
}
console.log(params)
try {
const result: any = await dynamoDb.get(params).promise()
return {
body: JSON.stringify(result.Item),
statusCode: result.$response.httpResponse.statusCode
}
} catch (error) {
console.log(error)
return {
body: JSON.stringify({
message: `Failed to get project with id: ${event.pathParameters!.id}`
}),
statusCode: 500
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试使其与GetItemInput类型一起使用 …
amazon-web-services amazon-dynamodb aws-sdk aws-lambda aws-sdk-js
我正在尝试使用 aws-sdk 中的 ManagedUpload 。我的代码:
var buffer = fs.readFileSync(filePath);
var paramsCreate = {
Bucket: bucketName,
Key: 'myfile.ogv',
Body: buffer
};
let perc = 0;
var upload = new AWS.S3.ManagedUpload({
partSize: 5 * 1024 * 1024, queueSize: 4,
params: paramsCreate
}).on('httpUploadProgress',function(progress) {
let newPerc = Math.round(progress.loaded / progress.total * 100);
if (newPerc != perc) {
perc = newPerc;
console.log(perc + '% sent');
}
});
let up = upload.promise();
up.then(data=>{
console.log('Finished');
console.log(data);
}).catch(err=>console.log('Err: ', err));
Run Code Online (Sandbox Code Playgroud)
使用这段代码我(显然)收到以下错误:
Error: connect EHOSTUNREACH 169.254.169.254:80
at TCPConnectWrap.afterConnect …Run Code Online (Sandbox Code Playgroud) aws-sdk-js ×6
amazon-s3 ×2
aws-lambda ×2
aws-sdk ×2
javascript ×2
node.js ×2
lambda ×1
promise ×1