是的,AWS Lambda 可以根据对象的 S3 存储桶路径决定在 Step Functions 中执行。可以使用以下代码示例来实现这个步骤:
首先,创建 State Machine 并定义 S3 存储桶对象:
{
"Comment": "An example of the Amazon States Language using object key to trigger a Lambda Function",
"StartAt": "WaitForNewObject",
"States": {
"WaitForNewObject": {
"Type": "Wait",
"Seconds": 30,
"Next": "ProcessNewObject"
},
"ProcessNewObject": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-west-2:123456789012:function:MyLambdaFunction",
"Parameters": {
"Bucket.$": "$.Records[0].s3.bucket.name",
"ObjectKey.$": "$.Records[0].s3.object.key"
},
"End": true
}
}
}
接下来,定义 Lambda 函数来处理事件:
import json
import boto3
def lambda_handler(event, context):
# Get the bucket name and object key from the S3 event
bucket = event['Bucket']
key = event['ObjectKey']
# Do something with the object
# ...
return {
'statusCode': 200,
'body': json.dumps('Object processed successfully')
}
然后,配置 S3 存储桶触发器来触发 State Machine:
import boto3
client = boto3.client('s3')
response = client.put_bucket_notification_configuration(
Bucket='example-bucket',
NotificationConfiguration={
'LambdaFunctionConfigurations': [
{
'LambdaFunctionArn': 'arn:aws:states:us-west-2:123456789012:stateMachine:MyStateMachine',
'Events': [
's3:ObjectCreated:*',
],
'Filter': {
'Key': {
'FilterRules': [
{
'Name': 'prefix',