复制所有文件nodejs

时间:2018-10-27 06:48:04

标签: node.js aws-sdk

如何将所有文件从“ bucketname / one / two / one.file”复制到另一个存储桶,任何人都可以编辑我的代码来执行此类操作

  var params = {
  Bucket: "destinationbucket", 
  CopySource: "/sourcebucket/HappyFacejpg", 
  Key: "HappyFaceCopyjpg"
 };
 s3.copyObject(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response

 });

1 个答案:

答案 0 :(得分:1)

对于单个文件副本,可以使用此方法。对于整个文件夹,您需要列出源文件夹中的文件,然后复制文件。示例工作代码为

const AWS = require('aws-sdk');
AWS.config.loadFromPath('./AwsConfig.json');
var s3 = new AWS.S3({
    params: {
        Bucket: bucketName
    },
    region: XXX
});

// list all files from source folder
s3.listObjects({
    Prefix: sourceFolder
}, function (err, data) {
    if (err) {
        console.log(err, err.stack);
    } // an error occurred
    else {
        if (data.Contents.length) {
            for (i = 1; i <= data.Contents.length; i++) {
                if (data.Contents[i]) {
                    var params = {
                        CopySource: bucketName + '/' + data.Contents[i].Key,
                        Key: data.Contents[i].Key.replace(sourceFolder, destinationFolder)
                    };
                    // copy object to destination folder
                    s3.copyObject(params, function (copyErr, copyData) {
                        if (copyErr) {
                            console.log(err);
                        } else {
                            console.log('Copied: ', params.Key);
                        }
                    });
                }
            }
        }
    }
});  

尝试这个@Dusky Dood
更新代码

const AWS = require('aws-sdk');
AWS.config.loadFromPath('./AwsConfig.json');
var s3 = new AWS.S3();

// region: 'ap-southeast-2'

// list all files from source folder
s3.listObjects({
    Bucket: "sourceBucket",
    Prefix: "sourceFolder"
}, function (err, data) {
    if (err) {
        console.log(err, err.stack);
    } // an error occurred
    else {
        if (data.Contents.length) {
            for (i = 1; i <= data.Contents.length; i++) {
                if (data.Contents[i]) {
                    var params = {
                        Bucket: "destinationBucket",
                        Key: data.Contents[i].Key
                    };
                    // copy object to destination folder
                    s3.putObject(params, function(err, data) {
                        console.log('uploaded') // File uploads correctly.
                    });
                    // s3.copyObject(params, function (copyErr, copyData) {
                    //     if (copyErr) {
                    //         console.log(err);
                    //     } else {
                    //         console.log('Copied: ', params.Key);
                    //     }
                    // });
                }
            }
        }
    }
});  

更新2 这是我确切使用的确切代码,

const AWS = require('aws-sdk');
AWS.config.loadFromPath('./AwsConfig.json');
var s3 = new AWS.S3();

// region: 'ap-southeast-2'

// list all files from source folder
s3.listObjects({
    Bucket: "bucket1", // source bucket name
    Prefix: "report" // source folder
}, function (err, data) {
    if (err) {
        console.log(err, err.stack);
    } // an error occurred
    else {
        if (data.Contents.length) {
            for (i = 1; i <= data.Contents.length; i++) {
                if (data.Contents[i]) {
                    var params = {
                        Bucket: "bucket2", // second bucket ie) destination
                        // new folder: reportNew inside second bucket 
                        Key: `reportNew/${data.Contents[i].Key}`.replace('report/', '')
                    };
                    // copy object to destination folder
                    s3.putObject(params, function(err, data) {
                        console.log('uploaded') // File uploads correctly.
                    });
                    // s3.copyObject(params, function (copyErr, copyData) {
                    //     if (copyErr) {
                    //         console.log(err);
                    //     } else {
                    //         console.log('Copied: ', params.Key);
                    //     }
                    // });
                }
            }
        }
    }
});