@@ -12,7 +12,7 @@ export default {
1212 Accepts a file path or folder path starting from /tmp, then uploads the contents to S3.
1313 [See the docs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html)
1414 ` ) ,
15- version : "1.0.2 " ,
15+ version : "1.0.3 " ,
1616 type : "action" ,
1717 props : {
1818 aws : common . props . aws ,
@@ -44,27 +44,46 @@ export default {
4444 } ,
4545 methods : {
4646 ...common . methods ,
47+ getFilesRecursive ( dir ) {
48+ let results = [ ] ;
49+ const items = fs . readdirSync ( dir ) ;
50+ for ( const item of items ) {
51+ const itemPath = join ( dir , item ) ;
52+ const stat = fs . statSync ( itemPath ) ;
53+ if ( stat . isDirectory ( ) ) {
54+ results = results . concat ( this . getFilesRecursive ( itemPath ) ) ;
55+ } else {
56+ results . push ( itemPath ) ;
57+ }
58+ }
59+ return results ;
60+ } ,
4761 async uploadFolderFiles ( $ ) {
4862 const {
4963 uploadFile,
5064 bucket,
5165 folderPath,
5266 prefix,
5367 } = this ;
54-
55- const files = fs . readdirSync ( folderPath ) ;
56- const promises = [ ] ;
57- for ( const filename of files ) {
58- const fileContent = fs . readFileSync ( join ( folderPath , filename ) , {
68+ const files = this . getFilesRecursive ( folderPath ) ;
69+ const response = await Promise . all ( files . map ( async ( filePath ) => {
70+ const fileContent = fs . readFileSync ( filePath , {
5971 encoding : "base64" ,
6072 } ) ;
61- promises . push ( uploadFile ( {
73+ const relativePath = filePath . substring ( folderPath . length + 1 ) ;
74+ const s3Key = join ( prefix , relativePath ) ;
75+
76+ await uploadFile ( {
6277 Bucket : bucket ,
63- Key : join ( prefix , filename ) ,
78+ Key : s3Key ,
6479 Body : Buffer . from ( fileContent , "base64" ) ,
65- } ) ) ;
66- }
67- const response = await Promise . all ( promises ) ;
80+ } ) ;
81+ return {
82+ filePath,
83+ s3Key,
84+ status : "uploaded" ,
85+ } ;
86+ } ) ) ;
6887 $ . export ( "$summary" , `Uploaded all files from ${ folderPath } to S3` ) ;
6988 return response ;
7089 } ,
0 commit comments