Move a file from one folder to another folder in s3

我的梦境 提交于 2020-01-24 12:44:05

问题


First I'm trying to copy the file into other folder and unable to delete it. How I can delete the file only if the file is copied to the destination folder.

    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");
            }
        });    
    }

回答1:


Since you haven't shared we us the complete code, I created the following lambda function example which copies a specific file from source folder to destination folder and then delete the file from the source folder. as much as I can tell it looks like your lambda were missing some parameters and some promises.

Code snippet :

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}`)

}


};



回答2:


Try adding the Delete key in your params and adding the file's key to that list. Then s3.deleteObjects will delete the file.
I've reformated your code. Give it a try.

    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");
            }
        });    
    }


来源:https://stackoverflow.com/questions/58705520/move-a-file-from-one-folder-to-another-folder-in-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!