在我的 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;
}
}
您的
CopySource
被创建为:
CopySource: '/' + bucket + '/' + fileSource,
但是文档说字符串形式是:
{bucket}/{key}
因此,从CopySource
中删除前导斜线:
CopySource: bucket + '/' + fileSource,