You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取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,
});

代码说明

  1. 分割路径:用Fn.split('/', ...)将完整路径按斜杠/分割成字符串数组
  2. 获取最后一段索引:通过Fn.length(pathSegments) - 1得到数组最后一个元素的索引
  3. 提取文件名:用Fn.select取出数组最后一个元素,即为不带前缀的压缩文件名

另外注意:你代码中出现了变量名不一致的问题(定义的是testBucketDeployments,但后面用了searchyBucketDeployments),需要统一为同一个变量名,否则会报错。

内容的提问来源于stack exchange,提问作者dredbound

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 05:23:14