简体   繁体   English

如何使用 Nodejs 在两个 S3 存储桶之间复制对象

[英]How to copy objects between two S3 buckets using Nodejs

I've to copy S3 objects (text and media files) from one S3 bucket into another.我必须将 S3 对象(文本和媒体文件)从一个 S3 存储桶复制到另一个存储桶中。 Although bucket names are different, both buckets allow access using the same credentials.尽管存储桶名称不同,但两个存储桶都允许使用相同的凭据进行访问。 How can I achieve that using Nodejs?如何使用 Nodejs 实现这一目标?

I've so far been able to get a list of objects.到目前为止,我已经能够获得对象列表。 For some reason it just gives me the list of first 100 objects and show that 900 more items are available.出于某种原因,它只给了我前 100 个对象的列表,并显示还有900 more items Here is what I've so far:这是我到目前为止的内容:

index.js index.js

require("dotenv").config();
const AWS = require('aws-sdk');

let s3 = new AWS.S3({
    secretAccessKey: process.env.SECRET_ACCESS_KEY,
    accessKeyId: process.env.ACCESS_KEY_ID,
    region: process.env.REGION
});

const BUCKET = process.env.BUCKET_NAME;
console.log(BUCKET);

var params = {
    Bucket: BUCKET, 
};

const obj = s3.listObjectsV2(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
})

I've two questions:我有两个问题:

  1. How can I copy S3 objects between S3 buckets?如何在 S3 存储桶之间复制 S3 对象? Do I need to use a combination of getObject() and putObject() or copyObjects() ?我需要使用getObject() and putObject()copyObjects()的组合吗?
  2. Why the listObject() only gives me 100 objects with a mention of 900 more items.为什么 listObject() 只给了我 100 个对象并提到了 900 个以上的项目。 The total number of S3 objects is a lot more than these numbers combined. S3 对象的总数远远超过这些数字的总和。 How can I get all the objects in the bucket?如何获取存储桶中的所有对象?

You can use copyObject to move an object between buckets.您可以使用copyObject在存储桶之间移动 object。 Just make sure you pass CopySource as the object you want to copy, and Bucket as the target bucket.只需确保将CopySource作为您要复制的 object 传递,并将Bucket作为目标存储桶。

To answer your second question: listObjectsV2 is a paginated SDK function.要回答您的第二个问题: listObjectsV2是分页的 SDK function。 If you want to list more than the max (defaults to 1,000) objects, you'll have to iteratively call listObjectsV2 with the StartAfter option set to the last key of the previous request, which will start listing objects after that key.如果要列出超过最大(默认为 1,000)个对象,则必须迭代调用listObjectsV2并将StartAfter选项设置为上一个请求的最后一个键,这将在该键之后开始列出对象。 Keep calling this, updating StartAfter , until some number of objects is returned less than 1,000.继续调用它,更新StartAfter ,直到返回的对象数量少于 1,000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Node.js或Lambda函数在s3中为存储桶创建事件 - How do I create events for the buckets in s3 using nodejs or from lambda function 使用AWS S3存储桶的授权错误 - Authorization Error Using AWS S3 Buckets 创建 S3 存储桶 nodejs 时出现 aws-sdk 问题 - Problem with aws-sdk while creating S3 Buckets nodejs 如何从S3存储桶的单个请求中获取存储桶中的所有对象列表(包括标签) - How to get all list of objects in buckets including tags in single request from S3 bucket 如何将文件上传到 AWS S3?,执行此操作时出现错误(使用私有存储桶) - How to upload files to AWS S3?, I get an error when doing it (using private buckets) Nodejs S3删除多个对象错误 - Nodejs S3 Delete Multiple Objects Error 如何使用 Node.js 的 AWS SDK 将 Amazon S3 中的所有对象从一个前缀复制/移动到另一个前缀 - How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js S3:如何使用aws-sdk在nodejs中使用S3上传大文件 - S3 : How to upload a large file using S3 in nodejs using aws-sdk 如何使用Expresses NodeJs在S3中编写csv文件 - how to write the csv file in S3 using expresses NodeJs 如何在 NODEJS 项目中使用 IAM 角色访问 S3 存储桶 - How to access S3 bucket using IAM roles in NODEJS project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM