Node.js AWS Lambda节点写入S3在本地和服务器上的行为不同
我用Serverless创建了一个NodeJS Lambda函数。它从DynamoDB表中读取数据,并将数据写入S3存储桶 这是我的Node.js AWS Lambda节点写入S3在本地和服务器上的行为不同,node.js,amazon-web-services,aws-lambda,amazon-dynamodb,Node.js,Amazon Web Services,Aws Lambda,Amazon Dynamodb,我用Serverless创建了一个NodeJS Lambda函数。它从DynamoDB表中读取数据,并将数据写入S3存储桶 这是我的handler.js:(显示受影响的函数): Myserverless.yml: service: mhub-dsr-calculator-api plugins: - serverless-plugin-include-dependencies package: exclude: - node_modules/** custom: all
handler.js
:(显示受影响的函数):
Myserverless.yml
:
service: mhub-dsr-calculator-api
plugins:
- serverless-plugin-include-dependencies
package:
exclude:
- node_modules/**
custom:
allowed-headers:
- Content-Type
- Authorization
- X-Api-Token
- X-Origin
- X-Amz-Date
- X-Amz-Security-Token
settings:
CONTACTS_TABLE: dsrcalculator-contacts-dev
CONTACTS_BUCKET: mhub-dsrcalculator-contacts
provider:
name: aws
runtime: nodejs12.x
environment: ${self:custom.settings}
region: ap-southeast-1
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:DescribeTable
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:Query
Resource:
- "arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.settings.CONTACTS_TABLE}"
- Effect: "Allow"
Action:
- dynamodb:Query
Resource:
- "arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.settings.CONTACTS_TABLE}/index/client_code-created_at_local-index"
- Effect: "Allow"
Action:
- "s3:*"
Resource:
- "arn:aws:s3:::${self:custom.settings.CONTACTS_BUCKET}/*"
functions:
createPost:
handler: handler.createContact
events:
- http:
path: /contact
method: post
cors: true
updatePost:
handler: handler.updateContact
events:
- http:
path: /contact/{id}
method: put
cors: true
getAllPosts:
handler: handler.getAllContacts
events:
- http:
path: /contacts
method: get
cors: true
getContactsByClientID:
handler: handler.getContactsByClientID
events:
- http:
path: /contacts/{id}
method: get
cors: true
exportContactsByClientID:
handler: handler.exportContactsByClientID
events:
- http:
path: /export/{id}
method: get
cors: true
resources:
Resources:
PostsTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: "id"
AttributeType: "S"
- AttributeName: "client_code"
AttributeType: "S"
- AttributeName: "created_at_local"
AttributeType: "S"
KeySchema:
- AttributeName: "id"
KeyType: "HASH"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.settings.CONTACTS_TABLE}
当我在本地调用函数时(使用serverless invoke local-f exportcontacts byclientid
),其中'mhub dsrcalculator contacts'是bucket名称,'mkh'是id,'inside writetoubgger'表示工作簿中的代码。writeToBuffer
运行
mhub-dsrcalculator-contacts
mkh
inside writetoubgger
S3存储桶上的文件已成功创建
然而,当我部署这个和测试时,尽管它连续运行,但代码并没有进入“workbook.writeToBuffer”中,我们可以在下面看到
2020-06-22T11:23:22.945Z 1ce59a7b-98e4-4c9e-8736-ec7c162c4759 INFO mhub-dsrcalculator-contacts
2020-06-22T11:23:22.945Z 1ce59a7b-98e4-4c9e-8736-ec7c162c4759 INFO mkh
END RequestId: 1ce59a7b-98e4-4c9e-8736-ec7c162c4759
为什么会这样?任何指导都将不胜感激。谢谢。您的外部承诺即将完成,无需等待
writeToBuffer
承诺
尝试更改:
workbook.writeToBuffer().then(...
致:
您应该对S3上传调用执行相同的操作:
return S3.upload(params).promise().then(data => {
callback(null, response(200, res));
});
我还建议您使用
async
/wait
语法重写代码,这样更易于阅读和理解。我可以问一下-现在它在从AWS调用时可以工作,但当我在浏览器或邮递员上尝试它时,它会失败,出现“无法读取未定义的属性'id'”。在AWS上,我的测试事件定义为:{“pathParameters”:{“id”:“ijm”}}我在代码中使用const id=event.pathParameters.id.toLowerCase();?是否没有正确地指定它?您应该检查是否有空的事件。pathParameters
。Aconsole.log(event)
应该可以帮助您调试这个问题。我用async/wait这样重写代码:(答案).如果可以或可以进一步改进,请感谢您的反馈。
return workbook.writeToBuffer().then(...
return S3.upload(params).promise().then(data => {
callback(null, response(200, res));
});