查找S3存储桶并添加触发器以调用lambda [英] Lookup S3 Bucket and add a trigger to invoke a lambda

查看:222
本文介绍了查找S3存储桶并添加触发器以调用lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的AWS CDK(云开发工具包)在Java上的AWS上构建基础架构.

I'm using the new AWS CDK(Cloud Development Toolkit) to build the infrastructure on AWS in Java.

我要做的事情:查找s3存储桶并添加一个调用lambda函数的触发器.

What I have to do: lookup an s3 bucket and add a trigger that invokes a lambda function.

我所做的事情:

  • 查找了s3存储桶:

  • Looked up s3 bucket:

IBucket bucket = Bucket.fromBucketName(scope, bucketId, bucketName);

  • 向现有的lambda添加新的事件源:

  • Add a new event source to the existing lambda:

    IEventSource eventSource = getObjectCreationEvent();
    lambda.addEventSource(eventSource);
    

  • 其中 getObjectCreationEvent()是:

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

  • 问题出在哪里:

    S3EventSource 构造函数中的 bucket 参数的类型为 Bucket ,但是每个查找方法(例如, Bucket.fromBucketName())返回一个 IBucket 而不是一个 Bucket ,因此存在签名不匹配的情况.如果将 IBucket 强制转换为 Bucket ,我将具有 ClassCastException .

    The type of bucket parameter in the S3EventSource constructor is Bucket but every lookup method (e.g. the Bucket.fromBucketName()) returns an IBucket and not a Bucket, so there is a signature mismatch. If I cast IBucket to Bucket I have a ClassCastException.

    推荐答案

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

    From the git issue tracker https://github.com/aws/aws-cdk/issues/2004#issuecomment-479923251

    由于CloudFormation当前的局限性以及我们在CDK中实现存储桶通知的方式,因此无法在导入的存储桶上添加存储桶通知.这就是事件源使用s3.Bucket而不是s3.IBucket的原因.

    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. This is why the event source uses s3.Bucket instead of s3.IBucket.

    您可以使用onPutObject:

    You could use 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.

    目前看来答案是:

    无法设置.

    这篇关于查找S3存储桶并添加触发器以调用lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆