19
19
// description: Uploads full hierarchy of a local directory to a bucket.
20
20
// usage: node files.js upload-directory <bucketName> <directoryPath>
21
21
22
- async function main ( bucketName , directoryPath ) {
22
+ function main (
23
+ bucketName = 'your-unique-bucket-name' ,
24
+ directoryPath = './local/path/to/directory'
25
+ ) {
23
26
// [START upload_directory]
24
27
/**
25
28
* TODO(developer): Uncomment the following lines before running the sample.
@@ -36,68 +39,53 @@ async function main(bucketName, directoryPath) {
36
39
// Creates a client
37
40
const storage = new Storage ( ) ;
38
41
42
+ const { promisify} = require ( 'util' ) ;
39
43
const fs = require ( 'fs' ) ;
40
44
const path = require ( 'path' ) ;
41
- const fileList = [ ] ;
42
45
43
- async function uploadDirectory ( ) {
44
- // Get a list of files from the specified directory
45
- let dirCtr = 1 ;
46
- let itemCtr = 0 ;
47
- const pathDirName = path . dirname ( directoryPath ) ;
48
-
49
- getFiles ( directoryPath ) ;
50
-
51
- function getFiles ( directory ) {
52
- fs . readdir ( directory , ( err , items ) => {
53
- dirCtr -- ;
54
- itemCtr += items . length ;
55
- items . forEach ( item => {
56
- const fullPath = path . join ( directory , item ) ;
57
- fs . stat ( fullPath , ( err , stat ) => {
58
- itemCtr -- ;
59
- if ( stat . isFile ( ) ) {
60
- fileList . push ( fullPath ) ;
61
- } else if ( stat . isDirectory ( ) ) {
62
- dirCtr ++ ;
63
- getFiles ( fullPath ) ;
64
- }
65
- if ( dirCtr === 0 && itemCtr === 0 ) {
66
- onComplete ( ) ;
67
- }
68
- } ) ;
69
- } ) ;
70
- } ) ;
46
+ const readdir = promisify ( fs . readdir ) ;
47
+ const stat = promisify ( fs . stat ) ;
48
+
49
+ async function * getFiles ( directory = '.' ) {
50
+ for ( const file of await readdir ( directory ) ) {
51
+ const fullPath = path . join ( directory , file ) ;
52
+ const stats = await stat ( fullPath ) ;
53
+
54
+ if ( stats . isDirectory ( ) ) {
55
+ yield * getFiles ( fullPath ) ;
56
+ }
57
+
58
+ if ( stats . isFile ( ) ) {
59
+ yield fullPath ;
60
+ }
71
61
}
62
+ }
72
63
73
- async function onComplete ( ) {
74
- const resp = await Promise . all (
75
- fileList . map ( filePath => {
76
- let destination = path . relative ( pathDirName , filePath ) ;
77
- // If running on Windows
78
- if ( process . platform === 'win32' ) {
79
- destination = destination . replace ( / \\ / g, '/' ) ;
80
- }
81
- return storage
82
- . bucket ( bucketName )
83
- . upload ( filePath , { destination} )
84
- . then (
85
- uploadResp => ( { fileName : destination , status : uploadResp [ 0 ] } ) ,
86
- err => ( { fileName : destination , response : err } )
87
- ) ;
88
- } )
89
- ) ;
90
-
91
- const successfulUploads =
92
- fileList . length - resp . filter ( r => r . status instanceof Error ) . length ;
93
- console . log (
94
- `${ successfulUploads } files uploaded to ${ bucketName } successfully.`
95
- ) ;
64
+ async function uploadDirectory ( ) {
65
+ const bucket = storage . bucket ( bucketName ) ;
66
+ let successfulUploads = 0 ;
67
+
68
+ for await ( const filePath of getFiles ( directoryPath ) ) {
69
+ try {
70
+ const dirname = path . dirname ( directoryPath ) ;
71
+ const destination = path . relative ( dirname , filePath ) ;
72
+
73
+ await bucket . upload ( filePath , { destination} ) ;
74
+
75
+ console . log ( `Successfully uploaded: ${ filePath } ` ) ;
76
+ successfulUploads ++ ;
77
+ } catch ( e ) {
78
+ console . error ( `Error uploading ${ filePath } :` , e ) ;
79
+ }
96
80
}
81
+
82
+ console . log (
83
+ `${ successfulUploads } files uploaded to ${ bucketName } successfully.`
84
+ ) ;
97
85
}
98
86
99
87
uploadDirectory ( ) . catch ( console . error ) ;
100
88
// [END upload_directory]
101
89
}
102
90
103
- main ( ...process . argv . slice ( 2 ) ) . catch ( console . error ) ;
91
+ main ( ...process . argv . slice ( 2 ) ) ;
0 commit comments