如何获取AWS CDK BucketDeployment生成的S3压缩文件名用于CodePipeline?
解决AWS CDK BucketDeployment获取纯压缩文件名的问题
当使用Source.asset上传资源时,CDK会自动为asset添加前缀路径(比如local_development_resources_transform/cdk.out/...),导致objectKeys返回的是完整路径而非纯文件名。可以通过CDK的内置函数组合提取最后一段文件名:
修改后的代码
const testBucketDeployments = new BucketDeployment(this, 'test-ConfigBackups', { sources: [Source.asset(resolve(__dirname, '../../splunk_config_files'))], destinationBucket: bucketConfigFiles, extract: false, }); // 分割路径并提取最后一段文件名 const pathSegments = Fn.split('/', testBucketDeployments.objectKeys[0]); const zippedConfigsDir = Fn.select(Fn.length(pathSegments) - 1, pathSegments); const sourceAction = new S3SourceAction({ actionName: 'S3ConfigSource', bucket: bucketConfigFiles, bucketKey: zippedConfigsDir, output: sourceOutput, role: codeDeployRole, });
代码说明
- 分割路径:用
Fn.split('/', ...)将完整路径按斜杠/分割成字符串数组 - 获取最后一段索引:通过
Fn.length(pathSegments) - 1得到数组最后一个元素的索引 - 提取文件名:用
Fn.select取出数组最后一个元素,即为不带前缀的压缩文件名
另外注意:你代码中出现了变量名不一致的问题(定义的是testBucketDeployments,但后面用了searchyBucketDeployments),需要统一为同一个变量名,否则会报错。
内容的提问来源于stack exchange,提问作者dredbound
相关产品推荐
相关产品推荐

