headers[headerName].trim 不是函数

问题描述 投票:0回答:1

这是我的代码:

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)
node.js amazon-s3 aws-sdk serverless-framework
1个回答
0
投票

在适用于 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);
© www.soinside.com 2019 - 2024. All rights reserved.