Skip to content

Commit 32c761d

Browse files
authored
feat: add gcs driver to project (#7)
* feat: add gcs driver to project * ci: convert to base64 * ci: create new step to create the gcs file * ci: fix run command step Create GCS Credential * ci: fix run command step Create GCS Credential * ci: fix run command step Create GCS Credential * test: adjust build storage test, add gcs to array * test: remove some tests from gcs to not break the rate limit * test: remove some tests from gcs to not break the rate limit
1 parent 516842e commit 32c761d

File tree

14 files changed

+1460
-74
lines changed

14 files changed

+1460
-74
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
APP_NAME="SecJS"
22
APP_URL="https://cdn.secjs.io"
3+
34
AWS_REGION=""
45
AWS_KEY=""
56
AWS_BUCKET=""
67
AWS_SECRET=""
78
AWS_ENDPOINT=""
9+
10+
GCS_BUCKET=""
11+
GCS_ENDPOINT="https://storage.googleapis.com/storage/v1/b/${GCS_BUCKET}"
12+
GCS_PROJECT=""
13+
GCS_SECRET="./gcs-credentials.json"

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ jobs:
2727
- name: Verify project lint and try to fix it
2828
run: npm run lint:fix
2929

30+
- name: Create GCS Credential file to run tests
31+
run: echo ${{ secrets.GCS_FILE_CONTENT }} | base64 -d > gcs-credentials.json
32+
3033
- name: Run the tests from project
3134
run: npm run test
3235
env:
@@ -37,3 +40,7 @@ jobs:
3740
AWS_BUCKET: ${{ secrets.AWS_BUCKET }}
3841
AWS_SECRET: ${{ secrets.AWS_SECRET }}
3942
AWS_ENDPOINT: ${{ secrets.AWS_ENDPOINT }}
43+
GCS_BUCKET: ${{ secrets.GCS_BUCKET }}
44+
GCS_ENDPOINT: ${{ secrets.GCS_ENDPOINT }}
45+
GCS_PROJECT: ${{ secrets.GCS_PROJECT }}
46+
GCS_SECRET: ${{ secrets.GCS_SECRET }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ dist
129129
build
130130

131131
storage
132+
gcs-credentials.json

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ export default {
9191
region: Env('AWS_REGION', ''),
9292
bucket: Env('AWS_BUCKET', ''),
9393
endpoint: Env('AWS_ENDPOINT', '')
94-
}
94+
},
95+
gcs: {
96+
driver: 'gcs',
97+
project: Env('GCS_PROJECT', ''),
98+
secret: Env('GCS_SECRET', ''),
99+
bucket: Env('GCS_BUCKET', ''),
100+
endpoint: Env('GCS_ENDPOINT', ''),
101+
},
95102
},
96103
}
97104
```
@@ -168,9 +175,9 @@ storage
168175
.put('file.txt', Buffer.from('Hello World'))
169176
```
170177

171-
### Using S3 disk
178+
### Using S3 or GCS disk
172179

173-
> You can use s3 disk to make all this actions inside your s3 bucket
180+
> You can use **s3** or **gcs** disk to make all this actions inside buckets
174181
175182
```ts
176183
storage.disk('s3').put('folder/file.txt', Buffer.from('Hello world!'))
@@ -179,12 +186,34 @@ storage.disk('s3').put('folder/file.txt', Buffer.from('Hello world!'))
179186
> It could be a little repetitive calling disk all the time, so you can change the default disk for that storage instance
180187
181188
```ts
182-
storage.changeDefaultDisk('s3')
189+
storage.changeDefaultDisk('gcs')
183190

184-
// All actions will be using s3 disk
191+
// All storage actions of this instance will use gcs from now on
185192
storage.put('folder/file.txt', Buffer.from('Hello world!'))
186193
```
187194

195+
> Be careful with **addConfig**, **removeConfig** and **resetConfig** because they create a new instance
196+
> of the default driver you are using, if you want to subscribe some config and use a different disk,
197+
> use this methods first, example:
198+
199+
```ts
200+
proccess.env.FILESYSTEM_DISK = 'local'
201+
202+
// BAD!!!!!
203+
// This will create the file using local disk
204+
storage
205+
.disk('s3')
206+
.addConfig('bucket', 'test-bucket')
207+
.put('file.txt', Buffer.from('Hello World'))
208+
209+
// GOOD!!!!!
210+
// This will create the file using s3 disk
211+
storage
212+
.addConfig('bucket', 'test-bucket')
213+
.disk('s3')
214+
.put('file.txt', Buffer.from('Hello World'))
215+
```
216+
188217
### Extending disks and drivers
189218

190219
> Nowadays, @secjs/storage has only LocalDriver and S3Driver support, but you can extend the drivers for Storage class if you implement DriverContract interface

config/filesystem.default.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,12 @@ export default {
4545
bucket: Env('AWS_BUCKET', ''),
4646
endpoint: Env('AWS_ENDPOINT', ''),
4747
},
48+
gcs: {
49+
driver: 'gcs',
50+
project: Env('GCS_PROJECT', ''),
51+
secret: Env('GCS_SECRET', ''),
52+
bucket: Env('GCS_BUCKET', ''),
53+
endpoint: Env('GCS_ENDPOINT', ''),
54+
},
4855
},
4956
}

config/filesystem.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,12 @@ export default {
5050
bucket: Env('AWS_BUCKET', ''),
5151
endpoint: Env('AWS_ENDPOINT', ''),
5252
},
53+
gcs: {
54+
driver: 'gcs',
55+
project: Env('GCS_PROJECT', ''),
56+
secret: Env('GCS_SECRET', ''),
57+
bucket: Env('GCS_BUCKET', ''),
58+
endpoint: Env('GCS_ENDPOINT', ''),
59+
},
5360
},
5461
}

0 commit comments

Comments
 (0)