11const StorageSystem = require ( './storage-system' ) ;
22const { Storage} = require ( '@google-cloud/storage' ) ;
3+ const path = require ( 'path' ) ;
34let storage = new Storage ;
45
56class GoogleCloudStorageSystem extends StorageSystem {
@@ -19,7 +20,7 @@ class GoogleCloudStorageSystem extends StorageSystem {
1920 // (If the contents will change, use cacheControl: 'no-cache')
2021 cacheControl : 'public, max-age=31536000' ,
2122 } ,
22- destination : destination
23+ destination : destination + filename
2324 } ) ;
2425 return ( `${ filename } uploaded to ${ bucketName } .` ) ;
2526 } catch ( err ) {
@@ -37,15 +38,56 @@ class GoogleCloudStorageSystem extends StorageSystem {
3738 */
3839 async download ( bucketName , filename , destination ) {
3940 try {
40- return storage
41+ await storage
4142 . bucket ( bucketName )
4243 . file ( filename )
43- . download ( { destination : destination } ) ;
44+ . download ( { destination : destination + filename } ) ;
45+ return ( `${ filename } download to ${ destination + filename } .` ) ;
4446 } catch ( err ) {
45- console . log ( err ) ;
4647 throw new Error ( err ) ;
4748 }
4849 }
50+
51+ async deleteFile ( buckName , filename , destination ) {
52+ try {
53+ await storage
54+ . bucket ( buckName )
55+ . file ( filename )
56+ . delete ( ) ;
57+ return ( `gs://${ buckName } /${ filename } deleted.` ) ;
58+ } catch ( err ) {
59+ throw new Error ( err )
60+ }
61+ }
62+
63+ async getBucketMetadata ( bucketName ) {
64+ try {
65+ return await storage . bucket ( bucketName ) . getMetadata ( ) ;
66+ } catch ( err ) {
67+ throw new Error ( err )
68+ }
69+ }
70+
71+ async createBucket ( bucketName , location , storageClass ) {
72+ try {
73+ const [ bucket ] = await storage . createBucket ( bucketName , {
74+ location : location ,
75+ storageClass : storageClass ,
76+ } ) ;
77+ return ( `Bucket ${ bucket . name } created.` ) ;
78+ } catch ( err ) {
79+ throw new Error ( err )
80+ }
81+ }
82+
83+ async deleteBucket ( bucketName ) {
84+ try {
85+ await storage . bucket ( bucketName ) . delete ( ) ;
86+ return ( `Bucket ${ bucketName } deleted.` ) ;
87+ } catch ( err ) {
88+ throw new Error ( err )
89+ }
90+ }
4991}
5092
5193module . exports = GoogleCloudStorageSystem ;
0 commit comments