-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98bccc2
commit cb76f65
Showing
7 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import path from 'path'; | ||
import { S3Client } from '@aws-sdk/client-s3'; | ||
import { S3SyncClient } from 's3-sync-client'; | ||
import mime from 'mime-types'; | ||
|
||
export default class S3Syncer | ||
{ | ||
download() { | ||
this.log('Downloading files...'); | ||
|
||
return Promise.all([ | ||
this.syncClient.sync(this.publicBucket, `${this.localPath}/dist`, { | ||
filters: this.filters, | ||
}), | ||
this.syncClient.sync(this.privateBucket, `${this.localPath}/storage`), | ||
]); | ||
} | ||
|
||
upload() { | ||
this.log('Uploading files...'); | ||
|
||
return Promise.all([ | ||
this.syncClient.sync(`${this.localPath}/dist`, this.publicBucket, { | ||
filters: this.filters, | ||
commandInput: input => ({ | ||
ACL: 'public-read', | ||
ContentType: mime.lookup(input.Key), | ||
CacheControl: input.Key.startsWith('data/') | ||
? 'no-cache, stale-while-revalidate=5, stale-if-error=86400' | ||
: undefined, | ||
}), | ||
}), | ||
this.syncClient.sync(`${this.localPath}/storage`, this.privateBucket), | ||
]); | ||
} | ||
|
||
get s3Client() { | ||
return this._s3Client ??= new S3Client({ | ||
endpoint: process.env.AWS_S3_ENDPOINT, | ||
region: process.env.AWS_REGION, | ||
credentials: { | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | ||
}, | ||
}); | ||
} | ||
|
||
/** @returns {S3SyncClient} */ | ||
get syncClient() { | ||
return this._syncClient ??= new S3SyncClient({ client: this.s3Client }); | ||
} | ||
|
||
get publicBucket() { | ||
return `s3://${process.env.AWS_S3_BUCKET}`; | ||
} | ||
|
||
get privateBucket() { | ||
return `s3://${process.env.AWS_S3_PRIVATE_BUCKET}`; | ||
} | ||
|
||
get localPath() { | ||
return path.resolve('.'); | ||
} | ||
|
||
get filters() { | ||
return [ | ||
{ exclude: () => true }, // Exclude everything by default | ||
{ include: (key) => key.startsWith('assets/splatnet/') }, | ||
{ include: (key) => key.startsWith('data/') }, | ||
{ include: (key) => key.startsWith('status-screenshots/') }, | ||
]; | ||
} | ||
|
||
log(message) { | ||
console.log(`[S3] ${message}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import S3Syncer from './S3Syncer.mjs'; | ||
|
||
export function canSync() { | ||
return !!( | ||
process.env.AWS_ACCESS_KEY_ID && | ||
process.env.AWS_SECRET_ACCESS_KEY && | ||
process.env.AWS_S3_BUCKET && | ||
process.env.AWS_S3_PRIVATE_BUCKET | ||
); | ||
} | ||
|
||
async function doSync(download, upload) { | ||
if (!canSync()) { | ||
console.warn('Missing S3 connection parameters'); | ||
return; | ||
} | ||
|
||
const syncer = new S3Syncer(); | ||
|
||
if (download) { | ||
console.info('Downloading files...'); | ||
await syncer.download(); | ||
} | ||
|
||
if (upload) { | ||
console.info('Uploading files...'); | ||
await syncer.upload(); | ||
} | ||
} | ||
|
||
export function sync() { | ||
return doSync(true, true); | ||
} | ||
|
||
export function syncUpload() { | ||
return doSync(false, true); | ||
} | ||
|
||
export function syncDownload() { | ||
return doSync(true, false); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters