Skip to content

Commit

Permalink
feat: add gcs driver to project (#7)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jlenon7 authored Jan 6, 2022
1 parent 516842e commit 32c761d
Show file tree
Hide file tree
Showing 14 changed files with 1,460 additions and 74 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
APP_NAME="SecJS"
APP_URL="https://cdn.secjs.io"

AWS_REGION=""
AWS_KEY=""
AWS_BUCKET=""
AWS_SECRET=""
AWS_ENDPOINT=""

GCS_BUCKET=""
GCS_ENDPOINT="https://storage.googleapis.com/storage/v1/b/${GCS_BUCKET}"
GCS_PROJECT=""
GCS_SECRET="./gcs-credentials.json"
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Verify project lint and try to fix it
run: npm run lint:fix

- name: Create GCS Credential file to run tests
run: echo ${{ secrets.GCS_FILE_CONTENT }} | base64 -d > gcs-credentials.json

- name: Run the tests from project
run: npm run test
env:
Expand All @@ -37,3 +40,7 @@ jobs:
AWS_BUCKET: ${{ secrets.AWS_BUCKET }}
AWS_SECRET: ${{ secrets.AWS_SECRET }}
AWS_ENDPOINT: ${{ secrets.AWS_ENDPOINT }}
GCS_BUCKET: ${{ secrets.GCS_BUCKET }}
GCS_ENDPOINT: ${{ secrets.GCS_ENDPOINT }}
GCS_PROJECT: ${{ secrets.GCS_PROJECT }}
GCS_SECRET: ${{ secrets.GCS_SECRET }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ dist
build

storage
gcs-credentials.json
39 changes: 34 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ export default {
region: Env('AWS_REGION', ''),
bucket: Env('AWS_BUCKET', ''),
endpoint: Env('AWS_ENDPOINT', '')
}
},
gcs: {
driver: 'gcs',
project: Env('GCS_PROJECT', ''),
secret: Env('GCS_SECRET', ''),
bucket: Env('GCS_BUCKET', ''),
endpoint: Env('GCS_ENDPOINT', ''),
},
},
}
```
Expand Down Expand Up @@ -168,9 +175,9 @@ storage
.put('file.txt', Buffer.from('Hello World'))
```

### Using S3 disk
### Using S3 or GCS disk

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

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

> Be careful with **addConfig**, **removeConfig** and **resetConfig** because they create a new instance
> of the default driver you are using, if you want to subscribe some config and use a different disk,
> use this methods first, example:
```ts
proccess.env.FILESYSTEM_DISK = 'local'

// BAD!!!!!
// This will create the file using local disk
storage
.disk('s3')
.addConfig('bucket', 'test-bucket')
.put('file.txt', Buffer.from('Hello World'))

// GOOD!!!!!
// This will create the file using s3 disk
storage
.addConfig('bucket', 'test-bucket')
.disk('s3')
.put('file.txt', Buffer.from('Hello World'))
```

### Extending disks and drivers

> Nowadays, @secjs/storage has only LocalDriver and S3Driver support, but you can extend the drivers for Storage class if you implement DriverContract interface
Expand Down
7 changes: 7 additions & 0 deletions config/filesystem.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,12 @@ export default {
bucket: Env('AWS_BUCKET', ''),
endpoint: Env('AWS_ENDPOINT', ''),
},
gcs: {
driver: 'gcs',
project: Env('GCS_PROJECT', ''),
secret: Env('GCS_SECRET', ''),
bucket: Env('GCS_BUCKET', ''),
endpoint: Env('GCS_ENDPOINT', ''),
},
},
}
7 changes: 7 additions & 0 deletions config/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,12 @@ export default {
bucket: Env('AWS_BUCKET', ''),
endpoint: Env('AWS_ENDPOINT', ''),
},
gcs: {
driver: 'gcs',
project: Env('GCS_PROJECT', ''),
secret: Env('GCS_SECRET', ''),
bucket: Env('GCS_BUCKET', ''),
endpoint: Env('GCS_ENDPOINT', ''),
},
},
}
Loading

0 comments on commit 32c761d

Please sign in to comment.