Skip to content

Commit

Permalink
Merge branch 'feature/add-cdk' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
msudgh committed Aug 18, 2024
2 parents 9d99b60 + caaf03e commit 734d5ef
Show file tree
Hide file tree
Showing 22 changed files with 1,759 additions and 1,071 deletions.
176 changes: 78 additions & 98 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,30 @@
[![Codecov Status](https://codecov.io/gh/msudgh/sync-cloud-storage/branch/main/graph/badge.svg?token=2BY6063VOY)](https://codecov.io/gh/msudgh/sync-cloud-storage)
[![License](https://img.shields.io/github/license/msudgh/sync-cloud-storage)](LICENSE)

Synchronize files and directories between a remote computer and multiple Serverless cloud providers' storages.

Supported cloud providers:

- [x] AWS S3
Synchronize files and directories between a remote machine and a cloud storage via cloud frameworks and stacks consisting of [AWS SAM (Serverless)](https://www.serverless.com/) and [AWS Cloud Development Kit (CDK)](https://aws.amazon.com/cdk/). This package supports the following cloud storage providers: [AWS S3](https://aws.amazon.com/s3/).

## Features

- Sync multiple storages at once and flexible file matching (single or multiple file/dir sync) by defining patterns of [`glob`](<https://en.wikipedia.org/wiki/Glob_(programming)>) to include or exclude
- Supports a set of options as following for each file based on storage: `Prefix`, `Access Control List (ACL)`, `Tags`, `Metadata`
- Select a list of specific sync actions for each storage: `uploading`, `deleting`
- Modern and uses the latest official cloud provider's SDK
- AWS S3: [`[email protected]`](https://www.npmjs.com/package/@aws-sdk/client-s3)
- Sync multiple storages at once and flexible file matching (single or multiple file/dir sync) by just defining patterns of [`glob`](<https://en.wikipedia.org/wiki/Glob_(programming)>) to include or exclude
- Set the following configs for each synced file:
- prefix
- access control list (ACL)
- tags
- metadata
- Select a list of specific sync actions for each storage:
- upload
- delete
- Written in **TypeScript** with strong type checking
- Integration test with cloud providers

## Installation

1. [**ni**](https://github.com/antfu/ni): `ni sync-cloud-storage -D`
2. [**npm**](https://npmjs.com/): `npm i sync-cloud-storage -D`
3. [**yarn**](https://yarnpkg.com/): `yarn add sync-cloud-storage -D`
4. [**pnpm**](https://pnpm.io/): `pnpm add sync-cloud-storage -D`
- [**npm**](https://npmjs.com/): `npm i sync-cloud-storage`
- [**yarn**](https://yarnpkg.com/): `yarn add sync-cloud-storage`
- [**pnpm**](https://pnpm.io/): `pnpm add sync-cloud-storage`
- [**ni**](https://github.com/antfu/ni): `ni sync-cloud-storage`

## Usage

### Serverless
### AWS S3

#### Serverless

#### AWS S3
Sync storages action as a pre-deploy hook in the `serverless.yml`:

```yaml
plugins:
Expand All @@ -59,79 +49,69 @@ custom:
bar: foo
```
## Configuration Reference
This section provides a detailed reference for all configuration options.
- `name`:

- Type: `string`
- Required: `true`
- Minimum length: 1
- Example: `name: assets`

- `patterns`: File patterns to include or exclude during synchronization.

- Type: `array` of `string`
- Required: `true`
- Minimum items: 1
- Example:
```yaml
patterns:
- 'assets/*'
- '!assets/temp/*'
```

- `actions`: List of actions to perform during synchronization.

- Type: `array` of `string`
- Required: `true`
- Valid Values: `upload`, `delete`
- Example:
```yaml
actions:
- upload
- delete
```

- `prefix`: The prefix to apply to all synced files in the bucket.

- Type: `string`
- Required: `false`
- Default: `""`
- Example: `prefix: assets`

- `enabled`: Whether to enable the sync for the storage.

- Type: `boolean`
- Required: `false`
- Default: `true`

- `acl`: Access control list setting for the synced files.

- Type: `string`
- Required: `false`
- Valid Values: `private`, `public-read`, `public-read-write`, `authenticated-read`
- Reference: [AWS S3 Canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl)
- Example: `acl: public-read`

- `metadata`: Custom metadata to apply to the synced files.

- Type: `object`
- Required: `false`
- Example:
```yaml
metadata:
foo: bar
bar: foo
```

- `tags`: Custom tags to apply to the synced files.
- Type: `object`
- Required: `false`
- Default: `{}`
- Example:
```yaml
tags:
environment: production
```
#### CDK
Call sync storages action after setting up a CDK App:
```typescript
import { Stack, App } from '@aws-cdk/core'
import SyncCloudStorage from 'sync-cloud-storage'

const app = new App()
const stack = new Stack(app, 'MyStack')
const syncCloudStorage = new SyncCloudStorage(stack, {
storages: [
{
name: 'my-bucket',
patterns: ['assets/*'],
actions: ['upload', 'delete'],
prefix: 'assets',
acl: 'public-read',
metadata: {
foo: 'bar',
bar: 'foo',
},
},
],
})

// Sync storages
syncCloudStorage.storages()

// Sync tags
syncCloudStorage.tags()

// Sync metadata
syncCloudStorage.metadata()
```

## Options

### General

| Option | Notes | Type | Required | Default |
| -------- | ---------------------------------- | -------------------------------- | -------- | ---------------------------------------------------- |
| storages | List of storages, Minimum items: 1 | `array` of [`storage`](#storage) | true | undefined |
| region | Cloud (AWS) region | `string` | false | undefined or `AWS_REGION` environment variable |
| endpoint | Cloud (AWS) Endpoint URL | `string` | false | undefined or `AWS_ENDPOINT_URL` environment variable |
| offline | Offline mode | `boolean` | false | false or `IS_OFFLINE` environment variable |
| disabled | Disable sync | `boolean` | false | false |
| silent | Silent output logs | `boolean` | false | false |

### Storage

| Option | Notes | Type | Required | Default |
| ----------- | ----------------------------------------------------------------------------------------------------------- | ------------------- | -------- | ------------------ |
| name | Name of storage (AWS S3 Bucket), Minimum length: 1 | `string` | true | undefined |
| patterns | Patterns of [`glob`][glob] paths to include or exclude on sync action, Minimum items: 1 | `array` of `string` | true | undefined |
| actions | Sync actions, Valid values: `upload`, `delete` | `array` of `string` | false | `upload`, `delete` |
| prefix | Prefix for the storage files and folders | `string` | false | `''` |
| enabled | Enable or disable the storage on sync action | `boolean` | false | `true` |
| acl | [AWS S3 Canned ACL][acl], Valid values: `private`, `public-read`, `public-read-write`, `authenticated-read` | `string` | false | undefined |
| metadata | A set of metadata key/value pair to be set or unset on the object | `object` | false | undefined |
| tags | A set of tag key/value pair to be set or unset on the object | `object` | false | `{}` |
| gitignore | Use .gitignore file to exclude files and directories | `boolean` | false | false |
| ignoreFiles | Ignore files and directories to exclude from sync action | `array` of `string` | false | undefined |

[glob]: https://en.wikipedia.org/wiki/Glob_(programming)
[acl]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {
'/node_modules/',
'./test/setupEnvs.ts',
'./test/mocks',
'./test/schemas/input.fixture.ts',
],
transform: {
'^.+\\.ts$': [
Expand Down
51 changes: 36 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"scripts": {
"prepare": "is-ci || husky",
"build": "rm -rf ./dist && tsc -p tsconfig.build.json",
"build": "pnpm run clean && tsc -p tsconfig.build.json",
"build:watch": "tsc -p tsconfig.build.json -w",
"build:type-check": "tsc -p tsconfig.build.json --noEmit",
"test": "node --trace-warnings --experimental-vm-modules 'node_modules/jest/bin/jest.js'",
Expand All @@ -34,7 +34,20 @@
"lint:fix": "eslint . --fix",
"docs:build": "pnpm run build && rm -rf ./docs && ./node_modules/.bin/jsdoc -c jsdoc.json",
"docs:start": "pnpm run docs:build && ./node_modules/.bin/http-server -c-1 -o docs",
"preversion": "pnpm run build && pnpm run format"
"preversion": "pnpm run lint && pnpm run build && pnpm run format && pnpm run docs:build",
"clean": "rm -rf ./dist ./docs",
"start": "node dist/esm/index.js"
},
"lint-staged": {
"(/test/.*\\.test\\.ts)$": [
"npm run test"
],
"*.{ts,js}": [
"npm run lint"
],
"*.{ts,js,json,md,yaml}": [
"npm run format"
]
},
"devDependencies": {
"@faker-js/faker": "8.4.1",
Expand All @@ -45,6 +58,8 @@
"@types/serverless": "3.12.22",
"@typescript-eslint/eslint-plugin": "6.21.0",
"@typescript-eslint/parser": "6.21.0",
"aws-cdk-lib": "^2.80.0",
"constructs": "^10.3.0",
"eslint": "8.56.0",
"eslint-config-prettier": "9.1.0",
"eslint-import-resolver-typescript": "3.6.1",
Expand All @@ -53,12 +68,13 @@
"http-server": "14.1.1",
"husky": "9.0.10",
"is-ci": "3.0.1",
"jest": "v30.0.0-alpha.3",
"jest": "v30.0.0-alpha.6",
"jest-mock-extended": "3.0.5",
"jsdoc": "4.0.2",
"lint-staged": "^15.2.9",
"prettier": "3.2.5",
"serverless": "3.38.0",
"ts-jest": "29.1.2",
"serverless": ">=3.38.0",
"ts-jest": "29.2.4",
"typescript": "5.3.3"
},
"dependencies": {
Expand All @@ -69,15 +85,20 @@
"winston": "^3.11.0",
"zod": "3.23.8"
},
"lint-staged": {
"(/test/.*\\.test\\.ts)$": [
"npm run test"
],
"*.{ts,js}": [
"npm run lint"
],
"*.{ts,js,json,md,yaml}": [
"npm run format"
]
"peerDependencies": {
"aws-cdk-lib": "^2.80.0",
"constructs": "^10.0.0",
"serverless": ">=3.38.0"
},
"pnpm": {
"overrides": {
"es5-ext@>=0.10.0 <0.10.63": ">=0.10.63",
"follow-redirects@<=1.15.5": ">=1.15.6",
"tar@<6.2.1": ">=6.2.1",
"braces@<3.0.3": ">=3.0.3",
"ws@>=7.0.0 <7.5.10": ">=7.5.10",
"fast-xml-parser@<4.4.1": ">=4.4.1",
"axios@>=1.3.2 <=1.7.3": ">=1.7.4"
}
}
}
Loading

0 comments on commit 734d5ef

Please sign in to comment.