@aws-sdk/client-s3 CopyObjectCommand 不复制文件

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

在我的 Angular 15 项目中,我正在存储文件 (

PutObjectCommand
) 和复制文件 (
CopyObjectCommand
)。

使用@aws-sdk/[email protected]

PutObjectCommand
工作正常。然而,
CopyObjectCommand
实际上并没有复制文件。

我要复制的数据是:

Bucket: "simplify-pilot-media-dev"
CopySource: "/simplify-pilot-media-dev/profiles/0972000547780992.jpg"
Key: "profiles/0972000547780999.jpg"

(我也尝试过在 copySource 的存储桶名称之前没有前导 /)。

我也得到了很好的回应:

但是文件并没有复制到bucket中。 顺便说一句,我真的很想重命名文件,因此我在复制后也调用了

DeleteObjectCommand

这是我的代码:

async copyFile(fileSource: string, fileTarget: string, deleteSource: boolean = false ): Promise<any> {
    const bucket = environment.awsBucket;
    const s3Client = new S3Client({ 
      region: environment.awsRegion,
      //credentials: fromIni({profile: 'dev'}) });
      credentials: {        
        accessKeyId: environment.awsAccessKeyS3,
        secretAccessKey: environment.awsSecretS3,
      }
    });

    const params = {
      Bucket: bucket,
      CopySource: '/' + bucket + '/' + fileSource,      
      Key: fileTarget,
    };
    console.log('About to call AWS S3 copy file with: ', params);
    try {
      const results = await s3Client.send(new CopyObjectCommand(params));
      console.log('Copied S3 file: ', results);
      if (!results) {
        return results;
      }
      if (deleteSource) {
        const paramsDelete = {
          Bucket: bucket,                
          Key: fileSource,
        };
        const resultsDelete = await s3Client.send(new DeleteObjectCommand(params));
        console.log('Deleted S3 file: ', resultsDelete);
        return resultsDelete;
      } else {
        return results;
      }      
    } catch (err) {
      console.log('There was an error renaming your file: ', err);
        return false;
    }
  }
amazon-web-services amazon-s3 aws-sdk aws-s3-client
1个回答
0
投票

您的

CopySource
被创建为:

CopySource: '/' + bucket + '/' + fileSource,

但是文档说字符串形式是:

{bucket}/{key}

因此,CopySource

中删除前导斜线

CopySource: bucket + '/' + fileSource,      
© www.soinside.com 2019 - 2024. All rights reserved.