首先,我试图将文件复制到其他文件夹中,但无法将其删除。仅当文件复制到目标文件夹后,才能删除文件。
const s3Params = {
Bucket: bucket,
CopySource: bucket + '/' + objectkey,
Key: 'processed-data/' + objectkey
};
function copyFile() {
s3.copyObject(s3Params, function (err, data) {
if (err) {
console.log(err);
}
else {
deleteFile();
}
});
}
function deleteFile() {
s3.deleteObject(s3Params, function (err, data) {
if (err) {
console.log(err, err.stack);
logs.push(err, err.stack);
}
else {
console.log("File moved successfully");
log.push("File moved successfully");
}
});
}
答案 0 :(得分:1)
由于尚未共享完整代码,我们创建了以下lambda函数示例,该示例将特定文件从源文件夹复制到目标文件夹,然后从源文件夹删除该文件。据我所知,您的lambda似乎缺少一些参数和某些承诺。
代码段:
const aws = require('aws-sdk');
const s3 = new aws.S3();
const bucketName = 'Bucket Name'
const sourceFolder = 'Source Folder'
const fileName = 'File Name with extention'
const destFolder = 'Destination Folder'
const s3Params = {
Bucket: bucketName,
CopySource: `${bucketName}/${sourceFolder}/${fileName}`,
Key: `${destFolder}/${fileName}`
};
function copyFile() {
return s3.copyObject(s3Params).promise();
}
function deleteFile() {
return s3.deleteObject({ Bucket: bucketName, Key: `${sourceFolder}/${fileName}` }).promise();
}
exports.handler = async (event, context, callback) => {
try{
await copyFile().then(r => deleteFile());
console.log('All good')
}
catch(ex){ console.log(`Failed with the following exception : ${ex}`)
}
};
答案 1 :(得分:0)
尝试在参数中添加Delete
键,然后将文件的键添加到该列表中。
然后s3.deleteObjects将删除该文件。
我已经重新格式化了您的代码。试试看。
const s3Params = {
Bucket: bucket,
CopySource: bucket + '/' + objectkey,
Key: 'processed-data/' + objectkey,
Delete: { Objects: [] }
};
function copyFile() {
s3.copyObject(s3Params, function (err, data) {
if (err) {
console.log(err);
}
else {
s3Params.Delete.Objects.push({Key: s3Params.Key});
deleteFile();
}
});
}
function deleteFile() {
s3.deleteObjects(s3Params, function (err, data) {
if (err) {
console.log(err, err.stack);
logs.push(err, err.stack);
}
else {
console.log("File moved successfully");
log.push("File moved successfully");
}
});
}