简体   繁体   English

AWS CDK Java - 文件上传到 S3

[英]AWS CDK Java - File Upload to S3

I am using the AWS CDK (Java) to upload my lambdas to S3 with the below code.我正在使用 AWS CDK (Java) 使用以下代码将我的 lambdas 上传到 S3。 I don't want the CDK to unpack my jar files.我不希望 CDK 解压缩我的 jar 文件。 This seems to be the only way to achieve this (it does work) but it uses deprecated code on the AssetOptions.builder().exclude().这似乎是实现这一目标的唯一方法(它确实有效),但它在 AssetOptions.builder().exclude() 上使用了已弃用的代码。 Is there a better way of doing this in Java without using deprecated code?在 Java 中是否有更好的方法可以在不使用已弃用代码的情况下执行此操作?

List<ISource> lambdaSources = new ArrayList<>();
for(String lambda: lambdas) {   
    AssetOptions assetOptions = AssetOptions.builder().exclude(
        Arrays.asList("**", "!" + lambda + "-" + VERSION + SUFFIX)).build();
    lambdaSources.add(Source.asset("../" + lambda + "/build/libs/", assetOptions));
}

I think I might have found a better way of doing this which combines code upload with lambda creation.我想我可能已经找到了一种更好的方法,它将代码上传与 lambda 创建相结合。 I am still interested in thoughts from experts in the CDK if there is a better approach.如果有更好的方法,我仍然对 CDK 专家的想法感兴趣。

BTW "lambda" is just an entity I created to hold relevant attributes of the various functions I have.顺便说一句,“lambda”只是我创建的一个实体,用于保存我拥有的各种功能的相关属性。

Function.Builder fnBuilder = Function.Builder.create(this, "Builder"); 
 fnBuilder.code(lambda.getCode())
    .functionName(lambda.getName())
    .handler(lambda.getHandler())
    .memorySize(lambda.getSize())
    .role(lambda.constructRole(this))
    .runtime(LambdaDetails.RUNTIME)
    .timeout(lambda.duration())
    .securityGroups(Arrays.asList(lambda.constructSecurityGroup(this)))
    .vpcSubnets(lambda.constructSubnets(this))
    .build();

I'm not on the CDK team or a CDK expert, and I don't have specific Java code to offer.我不是 CDK 团队的成员,也不是 CDK 专家,也没有具体的 Java 代码可提供。 However, I found additional context about the deprecation warning while using the TypeScript CDK that may be helpful.但是,我在使用 TypeScript CDK 时发现了有关弃用警告的其他上下文,这可能会有所帮助。

TLDR: I'm choosing to ignore the deprecation. TLDR:我选择忽略弃用。 Here's why:原因如下:

The AWS CDK source is written in TypeScript, and jsii is used to generate the AWS CDK Construct libraries for other supported languages (Java, .NET, Python, etc.). AWS CDK 源码使用 TypeScript 编写, jsii用于为其他支持的语言(Java、.NET、ZA7F5F35426B927411FC9231B56382173 等)生成 AWS CDK Construct 库。 What I describe below should still be relevant to the CDK for Java.我在下面描述的内容应该仍然与 Java 的 CDK 相关。

I agree that avoiding deprecated code is a good practice, in general.我同意,一般来说,避免弃用的代码是一种很好的做法。 But that specific deprecation warning appears to be at least partly the misleading result of some CDK refactoring (see below).但是,该特定的弃用警告似乎至少部分是某些 CDK 重构的误导性结果(见下文)。 I call it 'misleading', because it caused some devs like me to (mis)interpret it to mean that exclude will no longer be supported in the future.我称之为“误导”,因为它导致像我这样的一些开发人员(错误地)将其解释为将来不再支持exclude Since no clear alternative was offered, it was very concerning.由于没有提供明确的替代方案,因此非常令人担忧。

However, after reading through more of the github repo, the exclude functionality looks like it'll remain available and supported, although the exact syntax may change.但是,在阅读了 github 存储库的更多内容后, exclude功能看起来仍然可用并受支持,尽管确切的语法可能会改变。

After reading aws-cdk source code, pull requests 7708 and 12700 (from only 10 days ago) , along with related issues 9447 and 10125 , it looks like exclude isn't really deprecated from a practical standpoint.在阅读 aws-cdk 源代码、拉取请求770812700(仅 10 天前)以及相关问题944710125之后,从实际的角度来看,似乎exclude并没有真正被弃用。 Some comments call out the misunderstanding directly.一些评论直接指出了误解。 In the TypeScript source, exclude is moving from AssetOptions in the assets module (where it is a deprecated property) to CopyOptions (via FileOptions ) in the core module (where it is not deprecated).在 TypeScript 源代码中, exclude正在从assets模块中的AssetOptions (不推荐使用的属性)移动到core模块中的CopyOptions (通过FileOptions )(不推荐使用)。

Here's a key point: since assets.AssetOptions inherits from core.CopyOptions -- which in turn inherits from copy.FileOptions -- exclude remains effectively in place on the AssetOptions interface...at least for CDK V1!这里有一个关键点:因为assets.AssetOptions继承自core.CopyOptions —— 而 core.CopyOptions 又继承自copy.FileOptions —— exclude仍然有效地保留在AssetOptions接口上……至少对于 CDK V1!

For V2, some say they want to deprecate the entire assets module beyond just moving some shared types to core.对于 V2,有人说他们想要弃用整个资产模块,而不仅仅是将一些共享类型移动到核心。 I couldn't find a definitive reference on final V2 changes, but I'm interpreting the spirit of what I did find to mean that I can safely continue using glob patterns with exclude to customize S3 deployments.我找不到关于最终 V2 更改的明确参考,但我将我所发现的精神解释为我可以安全地继续使用带exclude的 glob 模式来自定义 S3 部署。 If the assets module does go away in V2, I expect the required code updates be minor - maybe even as simple as an import and/or type name change or two (YMMV of course).如果资产模块在 V2 中执行 go ,我希望所需的代码更新很小 - 甚至可能像导入和/或类型名称更改或两个一样简单(当然是 YMMV)。

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

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