[英]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:这是我到目前为止的内容:
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:我有两个问题:
getObject() and putObject()
or copyObjects()
?getObject() and putObject()
或copyObjects()
的组合吗?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.