简体   繁体   English

使用 Nodejs 的 AWS S3 存储桶到存储桶同步

[英]AWS S3 bucket to bucket sync using Nodejs

I have to create a Nodejs script to perform the S3 bucket to bucket sync .我必须创建一个 Nodejs 脚本来执行S3 存储桶到存储桶的同步 I don't want to run this when a file is just uploaded to the master S3, so I think lambda is not an option.我不想在文件刚刚上传到主 S3 时运行它,所以我认为 lambda 不是一个选项。 I need to run the task daily once at a particular time.我需要每天在特定时间运行一次任务。

How can I achieve this S3 bucket sync using NodeJS using aws-sdk ?如何使用aws-sdk使用NodeJS实现此 S3 存储桶同步?

Cron can be used for scheduling. Cron 可用于调度。 I found only aws-sdk code to copy from S3 to another S3.我发现只有 aws-sdk 代码可以从 S3 复制到另一个 S3。 Do we have a code in place to sync two S3 buckets?我们是否有适当的代码来同步两个 S3 存储桶?

You will need a cron job and nodejs provides a library named node-cron您将需要一个 cron 作业,并且 nodejs 提供了一个名为node-cron的库

let cron = require('node-cron');

cron.schedule('* * * * *', () => {
  // TODO
  ...
});

For daily cron you can use something like对于每日 cron,您可以使用类似

0 0 * * *

The first 0 specifies the minutes and the second the hours so this cron will run every day at midnight.第一个 0 指定分钟,第二个指定小时,因此该 cron 将在每天午夜运行。

AWS S3 Bucket synchronization using Nodejs and aws-sdk can be performed by the method of the s3sync package .使用 Nodejs 和aws-sdk AWS S3 Bucket 同步可以通过 s3sync的方法来执行。 If you use it with node-cron , you will be able to implement AWS S3 bucket synchronization scheduling through Nodejs.如果将它与node-cron ,您将能够通过 Nodejs 实现 AWS S3 存储桶同步调度。


I don't know if it'll help, if Cron and aws-cli are available, the purpose can be achieved without Nodejs.不知道有没有用,如果有Cron和aws-cli的话,不用Nodejs也能达到目的。 You simply add the code below to the crontab .您只需将以下代码添加到crontab

0 0 * * * aws s3 sync s3://bucket-name-1 s3://bucket-name-2

在这种情况下,Amazon EventBridge 可能是有价值的解决方案

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM