1- const fs = require ( "fs" )
2- const path = require ( "path" )
3- const mime = require ( "mime-types" )
4- const { S3Client, PutObjectCommand } = require ( "@aws-sdk/client-s3" )
1+ import fs from "fs"
2+ import path from "path"
3+ import mime from "mime-types"
4+ import { S3Client , PutObjectCommand } from "@aws-sdk/client-s3"
5+ import pLimit from "p-limit"
56
67const configDefaults = {
78 region : null ,
@@ -11,6 +12,7 @@ const configDefaults = {
1112 accessKeyId : null ,
1213 secretAccessKey : null ,
1314 targetDir : null ,
15+ concurrencyLimit : 100 ,
1416}
1517
1618const getConfig = passedConfig => {
@@ -33,14 +35,16 @@ const getKey = (file, root, targetDir) => {
3335 return path . join ( targetDir , filepath )
3436}
3537
36- const uploadFile = ( s3 , config , file , key ) => {
37- return fs . promises . readFile ( file ) . then ( data => {
38+ const uploadFileFunc = ( s3 , config , file , key ) => {
39+ return ( ) => {
40+ const data = fs . readFileSync ( file )
3841 const { bucket } = config
3942 const basename = path . basename ( file )
4043 const contentType = mime . contentType ( basename )
4144 const params = { Bucket : bucket , Body : data , Key : key , ContentType : contentType }
45+
4246 return s3 . send ( new PutObjectCommand ( params ) )
43- } )
47+ }
4448}
4549
4650const upload = argv => {
@@ -61,9 +65,11 @@ const upload = argv => {
6165 return ! exclude . includes ( ext )
6266 } )
6367
68+ const limit = pLimit ( config . concurrencyLimit ) ;
69+
6470 const promises = files . map ( file => {
6571 const key = getKey ( file , directory , targetDir )
66- return uploadFile ( s3 , config , file , key )
72+ return limit ( uploadFileFunc ( s3 , config , file , key ) )
6773 . then ( ( ) => console . log ( "Uploaded" , file ) )
6874 } )
6975
@@ -78,4 +84,4 @@ const upload = argv => {
7884 } )
7985}
8086
81- module . exports = upload
87+ export default upload
0 commit comments