我正在使用
@aws-sdk/client-s3
包来使用 AWS API。
我尝试使用以下代码将重定向对象上传到我的存储桶:
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
const s3Client = new S3Client({ region: process.env.AWS_REGION });
const putS3Command = new PutObjectCommand({
Bucket: process.env.S3_BUCKET,
Key: hashedUrl,
WebsiteRedirectLocation: validatedRequestBody.url,
});
await s3Client.send(putS3Command);
命令确实成功了,当我浏览AWS S3服务时,我在bucket中看到了该对象:
但是,当我尝试访问该对象(使用预先签名的 URL)时,我没有被重定向,但我只是得到大小为 0 的下载文件。
我的
PutObjectCommand
有什么问题吗?
我使用 Terraform 配置了此 S3 存储桶:
module "urls_s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "4.1.0"
acl = "private"
force_destroy = true
attach_policy = true
policy = data.aws_iam_policy_document.urls_s3_policy.json
control_object_ownership = true
object_ownership = "BucketOwnerPreferred"
tags = merge(
var.common_tags,
{
Name = "${var.project}-Client-URLs-S3-Bucket"
Stack = "Backend"
}
)
}
您需要检查对象属性以确保标头已设置。也可能是您没有在存储桶上启用网站托管并且没有链接到正确的位置。
仔细检查您创建的对象上的元数据。具体是这个键
x-amz-website-redirect-location