本文介绍了如何复制/使用AWS SDK的Node.js的移动从一个preFIX在Amazon S3中的所有对象到其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从一个preFIX的所有对象复制到其他?我曾尝试复制所有对象一次性从一个preFIX其他,但似乎工作是通过循环对象的列表和一个复制他们彼此的唯一方法所有可能的方式。这实在是低效率的。如果我有数百个文件的文件夹中,我将不得不作出100个电话?

  VAR PARAMS = {
            斗:斗,
            CopySource:斗+/+ oldDirName +'/ FILENAME.TXT,
            重点:newDirName +'/ FILENAME.TXT,
        };
        s3.copyObject(PARAMS,功能(ERR,数据){
            如果(ERR){
                callback.apply(这一点,[{
                    类型:错误,
                    消息:错误而重新命名指南,
                    数据:ERR
                }]);
            } 其他 {
                callback.apply(这一点,[{
                    类型:成功,
                    消息:目录成功更名,
                    数据:数据
                }]);
            }
        });
    }
 

解决方案

您将需要一个 AWS.S3.listObjects()来与一个特定的列出你的对象preFIX。但你是在正确的,你需要打一个电话,为您希望从一个桶/ preFIX复制到同一个或另一个桶/ preFIX每一个对象。

您还可以使用一个实用程序库如异步来管理您的要求。

VAR AWS =需要('AWS-SDK);VAR异步=需要('异步');VAR bucketName ='富';VAR老preFIX ='ABC /';新变种preFIX ='XYZ /';VAR S3 =新AWS.S3({params:一个{斗:bucketName},地区:美西2'});VAR完成=功能(ERR,数据){  如果(ERR)执行console.log(ERR);  其他CONSOLE.LOG(数据);};s3.listObjects({preFIX:老preFIX},功能(ERR,数据){  如果(data.Contents.length){    async.each(data.Contents,函数(文件,CB){      VAR PARAMS = {        CopySource:bucketName +'/'+ file.Key,        重点:file.Key.replace(旧preFIX,新的preFIX)      };      s3.copyObject(PARAMS,功能(copyErr,了CopyData){        如果(copyErr){          执行console.log(ERR);        }        其他 {          的console.log('复制',params.Key);          CB();        }      });    },完成);  }});

希望这有助于!

How do I copy all objects from one prefix to other? I have tried all possible ways to copy all objects in one shot from one prefix to other, but the only way that seems to work is by looping over a list of objects and copying them one by one. This is really inefficient. If I have hundreds of files in a folder, will I have to make 100 calls?

 var params = {
            Bucket: bucket,
            CopySource: bucket+'/'+oldDirName+'/filename.txt',
            Key: newDirName+'/filename.txt',
        };
        s3.copyObject(params, function(err, data) {
            if (err) {
                callback.apply(this, [{
                    type: "error",
                    message: "Error while renaming Directory",
                    data: err
                }]);
            } else {
                callback.apply(this, [{
                    type: "success",
                    message: "Directory renamed successfully",
                    data: data
                }]);
            }
        });
    }
解决方案

You will need to make one AWS.S3.listObjects() to list your objects with a specific prefix. But you are correct in that you will need to make one call for every object that you want to copy from one bucket/prefix to the same or another bucket/prefix.

You can also use a utility library like async to manage your requests.

var AWS = require('aws-sdk');
var async = require('async');
var bucketName = 'foo';
var oldPrefix = 'abc/';
var newPrefix = 'xyz/';
var s3 = new AWS.S3({params: {Bucket: bucketName}, region: 'us-west-2'});

var done = function(err, data) {
  if (err) console.log(err);
  else console.log(data);
};

s3.listObjects({Prefix: oldPrefix}, function(err, data) {
  if (data.Contents.length) {
    async.each(data.Contents, function(file, cb) {
      var params = {
        CopySource: bucketName + '/' + file.Key,
        Key: file.Key.replace(oldPrefix, newPrefix)
      };
      s3.copyObject(params, function(copyErr, copyData){
        if (copyErr) {
          console.log(err);
        }
        else {
          console.log('Copied: ', params.Key);
          cb();
        }
      });
    }, done);
  }
});

Hope this helps!

这篇关于如何复制/使用AWS SDK的Node.js的移动从一个preFIX在Amazon S3中的所有对象到其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:33