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

AWS CodeBuild跨区域上传工件至S3遇BucketRegionError报错的解决方法咨询

解决CodeBuild跨区域上传工件到S3的BucketRegionError问题

这个问题我之前也碰到过,核心原因是你的CodeBuild项目部署在us-east-2区域,它默认会用该区域的S3服务端点去执行UPLOAD_ARTIFACTS阶段,但你的目标S3桶实际在us-west-1,区域不匹配就触发了这个错误。这里有两种靠谱的解决办法:

方法一:手动在post_build阶段上传工件(推荐,更灵活)

直接去掉CodeBuild自动上传工件的配置,改用aws s3 cp命令在构建的最后阶段手动上传,同时指定目标S3桶所在的区域。修改后的buildspec如下:

version: 0.2
phases:
  install:
    runtime-versions:
      python: 3.7
  pre_build:
    commands:
      - pip install --upgrade pip
      - pip install cfn-lint
  build:
    commands:
      - echo Build started on `date`
      - cd ./folder
      - aws cloudformation package --template-file testing.yml --output-template-file testing-packaged-cf.yml --s3-bucket codepipeline-test-bucket --region us-west-1
      - echo $PWD
  post_build:
    commands:
      - echo Build completed on `date`
      - echo $PWD
      # 手动上传工件到us-west-1的S3桶,可自定义存储路径
      - aws s3 cp testing-packaged-cf.yml s3://codepipeline-test-bucket/build-artifacts/ --region us-west-1
# 移除自动上传配置,改用手动上传

方法二:配置CodeBuild自动上传时指定目标区域

如果你更倾向于使用CodeBuild自带的工件上传功能,可以在artifacts部分明确指定目标S3桶的区域,让CodeBuild用正确的端点去访问:

version: 0.2
phases:
  install:
    runtime-versions:
      python: 3.7
  pre_build:
    commands:
      - pip install --upgrade pip
      - pip install cfn-lint
  build:
    commands:
      - echo Build started on `date`
      - cd ./folder
      - aws cloudformation package --template-file testing.yml --output-template-file testing-packaged-cf.yml --s3-bucket codepipeline-test-bucket --region us-west-1
      - echo $PWD
  post_build:
    commands:
      - echo Build completed on `date`
      - echo $PWD
artifacts:
  files:
    - testing-packaged-cf.yml
  base-directory: $PWD
  # 指定目标S3桶名称
  bucket: codepipeline-test-bucket
  # 指定桶所在的区域
  region: us-west-1
  # 可选:设置S3前缀,方便归类工件
  s3-prefix: build-output/

额外注意事项

别忘了检查CodeBuild项目使用的服务角色IAM权限,确保它拥有对us-west-1区域目标S3桶的s3:PutObject等必要权限,否则会出现权限报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.01 00:42:32