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

如何通过AWS JavaScript SDK设置S3对象的x-amz-website-redirect-location元数据?

解决AWS S3对象301重定向元数据不生效问题

问题出在你把系统级元数据放到了Metadata字段里——这个字段专门用来存储自定义元数据,AWS SDK会自动给里面的键加上x-amz-meta-前缀,导致原本应该是系统元数据的x-amz-website-redirect-location变成了自定义元数据,自然无法触发重定向。

正确的做法是:把重定向地址作为顶级参数传给PutObjectCommand,SDK会自动处理成对应的系统元数据头,不需要手动加x-amz-前缀,参数名用驼峰格式WebsiteRedirectLocation。

正确代码示例

command = new PutObjectCommand({
    Bucket: bucketParams.Bucket,
    Key: cliParams.routingRules[0].old_url,
    Body: '',
    WebsiteRedirectLocation: cliParams.routingRules[0].new_url
});

为什么之前的写法不行

  • 当你把键放在Metadata对象里时,无论你写x-amz-website-redirect-location还是website-redirect-location,SDK都会自动添加x-amz-meta-前缀,最终生成的元数据键是x-amz-meta-x-amz-website-redirect-location或x-amz-meta-website-redirect-location,这些都是自定义元数据,S3不会识别为重定向规则。
  • 只有通过顶级参数WebsiteRedirectLocation设置的值,才会被SDK转换成系统级的x-amz-website-redirect-location元数据,触发S3的301重定向功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 13:18:19