Skip to content

Commit

Permalink
Merge pull request #79 from kking124/feature/support-offline-s3-local
Browse files Browse the repository at this point in the history
add serverless-offline + serverless-s3-local support
  • Loading branch information
k1LoW authored Feb 12, 2021
2 parents 710c29c + 53859a7 commit 7eaca9c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,24 @@ Run `sls deploy --nos3sync`, deploy your serverless stack without syncing local
### `sls s3sync`

Sync local directories and S3 prefixes.

### Offline usage

If also using the plugins `serverless-offline` and `serverless-s3-local`, sync can be supported during development by placing the bucket configuration(s) into the `buckets` object and specifying the alterate `endpoint` (see below).

```yaml
custom:
s3Sync:
# an alternate s3 endpoint
endpoint: http://localhost:4569
buckets:
# A simple configuration for copying static assets
- bucketName: my-static-site-assets # required
bucketPrefix: assets/ # optional
localDir: dist/assets # required
# ...
```

run `sls offline start --profile s3local` to sync to the local s3 bucket instead of Amazon AWS S3

run `sls deploy` for normal deployment
44 changes: 39 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ServerlessS3Sync {

this.hooks = {
'after:deploy:deploy': () => options.nos3sync ? undefined : BbPromise.bind(this).then(this.sync).then(this.syncMetadata).then(this.syncBucketTags),
'after:offline:start:init': () => options.nos3sync ? undefined : BbPromise.bind(this).then(this.sync).then(this.syncMetadata).then(this.syncBucketTags),
'after:offline:start': () => options.nos3sync ? undefined : BbPromise.bind(this).then(this.sync).then(this.syncMetadata).then(this.syncBucketTags),
'before:remove:remove': () => BbPromise.bind(this).then(this.clear),
's3sync:sync': () => BbPromise.bind(this).then(this.sync),
's3sync:metadata': () => BbPromise.bind(this).then(this.syncMetadata),
Expand All @@ -58,6 +60,14 @@ class ServerlessS3Sync {
};
}

isOffline() {
return String(this.options.offline).toUpperCase() === 'TRUE' || process.env.IS_OFFLINE;
}

getEndpoint() {
return this.serverless.service.custom.s3Sync.hasOwnProperty('endpoint') ? this.serverless.service.custom.s3Sync.endpoint : null;
}

client() {
const provider = this.serverless.getProvider('aws');
let awsCredentials, region;
Expand All @@ -77,15 +87,30 @@ class ServerlessS3Sync {
region = provider.getCredentials().region
awsCredentials = provider.getCredentials().credentials
}
let s3Options = {
region: region,
credentials: awsCredentials
};
if(this.getEndpoint() && this.isOffline()) {
s3Options.endpoint = new provider.sdk.Endpoint(this.serverless.service.custom.s3Sync.endpoint);
s3Options.s3ForcePathStyle = true;
}
const s3Client = new provider.sdk.S3({
region: region,
credentials: awsCredentials
});
return s3.createClient({ s3Client });
if(this.getEndpoint() && this.isOffline()) {
//see: https://github.com/aws/aws-sdk-js/issues/1157
s3Client.shouldDisableBodySigning = () => true
}
return s3.createClient({ s3Client });
}

sync() {
const s3Sync = this.serverless.service.custom.s3Sync;
let s3Sync = this.serverless.service.custom.s3Sync;
if(s3Sync.hasOwnProperty('buckets')) {
s3Sync = s3Sync.buckets;
}
const cli = this.serverless.cli;
if (!Array.isArray(s3Sync)) {
cli.consoleLog(`${messagePrefix}${chalk.red('No configuration found')}`)
Expand Down Expand Up @@ -207,7 +232,10 @@ class ServerlessS3Sync {
}

clear() {
const s3Sync = this.serverless.service.custom.s3Sync;
let s3Sync = this.serverless.service.custom.s3Sync;
if(s3Sync.hasOwnProperty('buckets')) {
s3Sync = s3Sync.buckets;
}
if (!Array.isArray(s3Sync)) {
return Promise.resolve();
}
Expand Down Expand Up @@ -255,7 +283,10 @@ class ServerlessS3Sync {
}

syncMetadata() {
const s3Sync = this.serverless.service.custom.s3Sync;
let s3Sync = this.serverless.service.custom.s3Sync;
if(s3Sync.hasOwnProperty('buckets')) {
s3Sync = s3Sync.buckets;
}
const cli = this.serverless.cli;
if (!Array.isArray(s3Sync)) {
cli.consoleLog(`${messagePrefix}${chalk.red('No configuration found')}`)
Expand Down Expand Up @@ -342,7 +373,10 @@ class ServerlessS3Sync {
}

syncBucketTags() {
const s3Sync = this.serverless.service.custom.s3Sync;
let s3Sync = this.serverless.service.custom.s3Sync;
if(s3Sync.hasOwnProperty('buckets')) {
s3Sync = s3Sync.buckets;
}
const cli = this.serverless.cli;
if (!Array.isArray(s3Sync)) {
cli.consoleLog(`${messagePrefix}${chalk.red('No configuration found')}`)
Expand Down

0 comments on commit 7eaca9c

Please sign in to comment.