Skip to content

Commit d8c0b2e

Browse files
committed
Implements create, delete and get bucket functionalities
1 parent 99ab06e commit d8c0b2e

7 files changed

Lines changed: 93 additions & 6 deletions

File tree

lib/GoogleCloudStorageSystem.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const StorageSystem = require ('./storage-system');
22
const {Storage} = require('@google-cloud/storage');
3+
const path = require('path');
34
let storage = new Storage;
45

56
class 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

5193
module.exports = GoogleCloudStorageSystem;

lib/storage-system.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ class StorageSystem {
66
download() {
77
throw new Error('concrete implementation should be used');
88
}
9+
deleteFile(){
10+
throw new Error('concrete implementation should be used');
11+
}
12+
getBucketMetadata(){
13+
throw new Error('concrete implementation should be used');
14+
}
15+
createBucket(){
16+
throw new Error('concrete implementation should be used');
17+
}
18+
deleteBucket(){
19+
throw new Error('concrete implementation should be used');
20+
}
921
}
1022

1123
module.exports = StorageSystem;

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"dependencies": {
2828
"@google-cloud/storage": "^4.3.1",
2929
"chai": "^4.2.0",
30-
"fs-extra": "^8.1.0"
30+
"fs-extra": "^8.1.0",
31+
"path": "^0.12.7"
3132
},
3233
"devDependencies": {
3334
"mocha": "^7.0.1"

test.txt

Whitespace-only changes.

test/sdk.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ describe('Simple Storage Test', () => {
66

77
//First stage Unit Test
88
it('should run', () => {
9-
storageSystem.upload();
9+
let data = storageSystem.upload('smc-v1-dev', '/Users/nitrocode/tmp/dev_key.key', '/Users/nitrocode/tmp/');
10+
console.log(data);
1011
});
1112

1213
});

test/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allkadjfnajs
2+
donfsjnkfsnjfnksnfjndsjnfksjdnfjdnksjfnksdj

0 commit comments

Comments
 (0)