这是我的代码:
import { CopyObjectCommand, S3Client } from '@aws-sdk/client-s3';
const s3Client = new S3Client({});
...
const params = {
Bucket: "destination_bucket", // Destination bucket name
Key: "abc.pdf", // Destination file name
CopySource: {
Bucket: "source_bucket", // Source bucket name
Key: "xyz.pdf" // Source file name
}
};
const command = new CopyObjectCommand(params);
const response = await s3Client.send(command);
当我运行代码时,出现以下错误。虽然我对两个存储桶都需要权限。
TypeError: headers[headerName].trim is not a function
at getCanonicalHeaders (/var/task/index.js:38660:62)
at SignatureV4S3Express.signRequest (/var/task/index.js:38978:80)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
在适用于 JavaScript 的 AWS 开发工具包 v3 中,CopySource 字段必须是格式为“/{sourceBucket}/{sourceKey}”的单个字符串,而不是对象。
这是正确的代码:
import { CopyObjectCommand, S3Client } from '@aws-sdk/client-s3';
const s3Client = new S3Client({});
...
const params = {
Bucket: "destination_bucket", // Destination bucket name
Key: "abc.pdf", // Destination file name
CopySource: `source_bucket/xyz.pdf`, //Source bucket and Source file name
};
const command = new CopyObjectCommand(params);
const response = await s3Client.send(command);