s3 上传后 Lambda 函数未触发
Lambda funciton not triggering after s3 upload
我正在尝试在上传文件后触发 Lambda 函数。
下面我定义了 file
Lambda 函数。在其中,一个文件被上传到 s3。然后我希望触发 process
Lambda 函数。但是,我无法触发它。此外,在 AWS s3 中,存储桶 Properties > Event notifications 为空。
我的serverless.yml:
service: backend
frameworkVersion: "2"
package:
exclude:
- "frontend/**"
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: true
dockerFile: ./dockerfile
provider:
name: aws
runtime: python2.7
stage: dev
lambdaHashingVersion: 20201221
apiGateway:
shouldStartNameWithService: true
environment:
STORAGE_BUCKET: dev-storage-bucket-01392334
iamRoleStatements:
- Effect: Allow
Action:
- s3:PutObject
- s3:PutObjectAcl
- s3:GetObject
- s3:GetObjectAcl
- s3:DeleteObject
Resource:
- "arn:aws:dynamodb:us-east-1:*:*"
- "arn:aws:s3:::*"
functions:
process:
handler: handler.process
events:
- s3:
bucket: ${self:provider.environment.STORAGE_BUCKET}
events: s3:ObjectCreated:*
existing: true
file:
handler: handler.file
events:
- http:
method: POST
path: /file
resources:
Resources:
storage:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.environment.STORAGE_BUCKET}
在我的处理程序中我有:
def process(event, context):
print("in process")
def fcsfile(event, context):
uploaded_file = event['body']
s3 = boto3.resource('s3')
object = s3.Object(STORAGE_BUCKET, 'the_file_name')
s3_response = object.put(Body=uploaded_file)
print(s3_response)
response = {
"statusCode": 200,
"body": json.dumps(response)
}
return response
当我对端点执行 CURL 时,文件会上传到 s3,但从未触发处理函数。
我试过删除 'existing: true'。同样的结果。我尝试将存储桶名称更改为完全不同的名称,例如'dev-storage-bucket-99999999968686' 但我随后看到错误:
An error occurred: S3BucketDevstoragebucket99999999968686 - dev-storage-bucket-99999999968686 already exists in stack
不,桶不存在。
我还能尝试什么?
看来您可能有一个小错字。
在 handler.process
块中用 event
替换 events
似乎对我有用:
functions:
process:
handler: handler.process
events:
- s3:
bucket: ${self:provider.environment.STORAGE_BUCKET}
event: s3:ObjectCreated:*
existing: true
当 运行 命令带有 events
时,我收到来自无服务器框架的警告:
Serverless: Configuration warning at 'functions.process.events[0]': unsupported function event
这是 documentation,它帮助我找到了正确的语法。
我正在尝试在上传文件后触发 Lambda 函数。
下面我定义了 file
Lambda 函数。在其中,一个文件被上传到 s3。然后我希望触发 process
Lambda 函数。但是,我无法触发它。此外,在 AWS s3 中,存储桶 Properties > Event notifications 为空。
我的serverless.yml:
service: backend
frameworkVersion: "2"
package:
exclude:
- "frontend/**"
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: true
dockerFile: ./dockerfile
provider:
name: aws
runtime: python2.7
stage: dev
lambdaHashingVersion: 20201221
apiGateway:
shouldStartNameWithService: true
environment:
STORAGE_BUCKET: dev-storage-bucket-01392334
iamRoleStatements:
- Effect: Allow
Action:
- s3:PutObject
- s3:PutObjectAcl
- s3:GetObject
- s3:GetObjectAcl
- s3:DeleteObject
Resource:
- "arn:aws:dynamodb:us-east-1:*:*"
- "arn:aws:s3:::*"
functions:
process:
handler: handler.process
events:
- s3:
bucket: ${self:provider.environment.STORAGE_BUCKET}
events: s3:ObjectCreated:*
existing: true
file:
handler: handler.file
events:
- http:
method: POST
path: /file
resources:
Resources:
storage:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.environment.STORAGE_BUCKET}
在我的处理程序中我有:
def process(event, context):
print("in process")
def fcsfile(event, context):
uploaded_file = event['body']
s3 = boto3.resource('s3')
object = s3.Object(STORAGE_BUCKET, 'the_file_name')
s3_response = object.put(Body=uploaded_file)
print(s3_response)
response = {
"statusCode": 200,
"body": json.dumps(response)
}
return response
当我对端点执行 CURL 时,文件会上传到 s3,但从未触发处理函数。
我试过删除 'existing: true'。同样的结果。我尝试将存储桶名称更改为完全不同的名称,例如'dev-storage-bucket-99999999968686' 但我随后看到错误:
An error occurred: S3BucketDevstoragebucket99999999968686 - dev-storage-bucket-99999999968686 already exists in stack
不,桶不存在。
我还能尝试什么?
看来您可能有一个小错字。
在 handler.process
块中用 event
替换 events
似乎对我有用:
functions:
process:
handler: handler.process
events:
- s3:
bucket: ${self:provider.environment.STORAGE_BUCKET}
event: s3:ObjectCreated:*
existing: true
当 运行 命令带有 events
时,我收到来自无服务器框架的警告:
Serverless: Configuration warning at 'functions.process.events[0]': unsupported function event
这是 documentation,它帮助我找到了正确的语法。