在s3中将文件从一个文件夹移动到另一个文件夹 [英] Move a file from one folder to another folder in s3

查看:541
本文介绍了在s3中将文件从一个文件夹移动到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我试图将文件复制到其他文件夹中,但无法将其删除.仅当文件复制到目标文件夹后,才能删除文件.

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

推荐答案

由于您尚未与我们共享完整的代码,因此我创建了以下lambda函数示例,该示例将特定文件从源文件夹复制到目标文件夹,然后删除源文件夹中的文件.据我所知,您的lambda似乎缺少一些参数和某些诺言.

Since you haven't shared with 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.

代码段:

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

}


};

这篇关于在s3中将文件从一个文件夹移动到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆