简体   繁体   English

查找 S3 Bucket 并添加触发器来调用 lambda

[英]Lookup S3 Bucket and add a trigger to invoke a lambda

I'm using the new AWS CDK(Cloud Development Toolkit) to build the infrastructure on AWS in Java.我正在使用新的 AWS CDK(云开发工具包)在 AWS 上用 Java 构建基础设施。

What I have to do: lookup an s3 bucket and add a trigger that invokes a lambda function.我必须做的是:查找一个 s3 存储桶并添加一个调用 lambda 函数的触发器。

What I have done:我做了什么:

  • Looked up s3 bucket:查找 s3 存储桶:

     IBucket bucket = Bucket.fromBucketName(scope, bucketId, bucketName);
  • Add a new event source to the existing lambda:向现有的 lambda 添加一个新的事件源:

     IEventSource eventSource = getObjectCreationEvent(); lambda.addEventSource(eventSource);
  • Where getObjectCreationEvent() is: getObjectCreationEvent()在哪里:

     private S3EventSource getObjectCreationEvent() { return new S3EventSource(bucket, new S3EventSourceProps() { @Override public List<EventType> getEvents() { return Collections.singletonList(EventType.OBJECT_CREATED); } }); }

What is the problem:问题是什么:

The type of bucket parameter in the S3EventSource constructor is Bucket but every lookup method (eg the Bucket.fromBucketName() ) returns an IBucket and not a Bucket , so there is a signature mismatch. S3EventSource构造函数中的bucket参数类型是Bucket但每个查找方法(例如Bucket.fromBucketName() )都返回一个IBucket而不是Bucket ,因此存在签名不匹配。 If I cast IBucket to Bucket I have a ClassCastException .如果我将IBucketBucket我有一个ClassCastException

From the git issue tracker https://github.com/aws/aws-cdk/issues/2004#issuecomment-479923251来自 git 问题跟踪器https://github.com/aws/aws-cdk/issues/2004#issuecomment-479923251

Due to current limitations with CloudFormation and the way we implemented bucket notifications in the CDK, it is impossible to add bucket notifications on an imported bucket.由于 CloudFormation 当前的限制以及我们在 CDK 中实现存储桶通知的方式,无法在导入的存储桶上添加存储桶通知。 This is why the event source uses s3.Bucket instead of s3.IBucket.这就是事件源使用 s3.Bucket 而不是 s3.IBucket 的原因。

You could use onPutObject:您可以使用 onPutObject:

const bucket = s3.Bucket.import(this, 'B', {
  bucketName: 'my-bucket'
});

const fn = new lambda.Function(this, 'F', {
  code: lambda.Code.inline('boom'),
  runtime: lambda.Runtime.NodeJS810,
  handler: 'index.handler'
});

bucket.onPutObject('put-object', fn);

But reading further on, this doesn't seem to work anymore either.但是进一步阅读,这似乎也不再起作用。

It seems the answer currently is:目前的答案似乎是:

It's impossible to set up.设置是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM