# @growit-io/google-cloud-storage-function

> Background Cloud Function to monitor a Cloud Storage bucket

Latest version **0.2.4** (published 2018-01-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install @growit-io/google-cloud-storage-function
pnpm add @growit-io/google-cloud-storage-function
yarn add @growit-io/google-cloud-storage-function
bun add @growit-io/google-cloud-storage-function
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.4 |
| Published | 2018-01-29 |
| First published | 2017-12-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Uwe Stuehler |
| Maintainers | ustuehler |
| Keywords | google, cloud, storage, function, trigger |

## Links

- npm: https://www.npmjs.com/package/@growit-io/google-cloud-storage-function
- Repository: https://github.com/growit-io/google-cloud-storage-function
- Homepage: https://github.com/growit-io/google-cloud-storage-function#readme
- Issues: https://github.com/growit-io/google-cloud-storage-function/issues
- npm.io page: https://npm.io/package/@growit-io/google-cloud-storage-function

## Dependencies (8)

- [sharp](https://npm.io/package/sharp.md) ^0.18.4
- [kraken](https://npm.io/package/kraken.md) ^0.3.3
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [request](https://npm.io/package/request.md) ^2.83.0
- [deepmerge](https://npm.io/package/deepmerge.md) ^2.0.1
- [stream-buffers](https://npm.io/package/stream-buffers.md) ^3.0.1
- [streaming-cache](https://npm.io/package/streaming-cache.md) ^0.5.1
- [@google-cloud/storage](https://npm.io/package/@google-cloud/storage.md) ^1.4.0

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 0.2.4 (latest) — 2018-01-29
- 0.2.3 — 2017-12-06
- 0.2.2 — 2017-12-06
- 0.2.1 — 2017-12-06
- 0.1.1 — 2017-12-05
- 0.1.0 — 2017-12-05

## README

# Google Cloud Storage Function
[Background Function](https://cloud.google.com/functions/docs/writing/background) that handles object change events from a Cloud Storage bucket, and runs predefined actions for events matching a configurable set of rules.

## Prerequisites
- Access to an existing Google Cloud project.
- A Cloud Storage bucket to monitor in your project.
- An idea of some actions you want to perform when events are emitted by the bucket.

## Initial Setup
Check out the [examples](examples/README.md) and copy one of them as a starting point for your own Cloud Function. Each example comes with complete setup instructions.

## Basic Usage
```javascript
/**
 * Background Cloud Function triggered by Cloud Storage events
 * @param {object} event The Cloud Storage event
 * @return {Promise}
 */
exports.monitor = require('@growit-io/google-cloud-storage-function')({
  // See Configuration, below
  loadConfigFromBucket: false,
  loadConfigFromSource: false,
  directories: {
    source: 'content',
    target: 'cache'
  },
  rules: [{
    events: ['add','update'],
    actions: ['copy']
  },{
    events: ['delete'],
    actions: ['deleteAll']
  }]
})
```

## Configuration
The default configuration is in [config.js](config.js). This default configuration will be merged at runtime with your own deployment configuration in `config.json`, the configuration supplied to the constructor of this Background Function, and with `/config.json` from the bucket, if it exists and **loadConfigFromBucket** is `true`.

### Function
The top-level configuration options below affect the Background Function's mode of operation, and how it generates its runtime configuration.

| option | default | description |
| :----- | :------ | :---------- |
| **dev** | `true` if **directories.source** exists in local filesystem; `false`, otherwise | Whether to enable local development mode. In local development node, all Cloud Storage operations are mapped to the local filesystem. |
| **loadConfigFromBucket** | `true` | Whether load `/config.json` from the Cloud Storage bucket, if it exists. |
| **loadConfigFromSource** | `true` | Whether load `config.json` from the Cloud Function source directory, if it exists. |

### Logging
The verbosity of the Background Function logs can be controlled with the **logger.level** option. Leave this at a reasonable low setting to avoid log spam if your bucket receives a lot of changes.

| option | default | description |
| :----- | :------ | :---------- |
| **logger.level** | `"warn"` | Log level for the modules in the Background Function (`"error"` &lt; `"warn"` &lt; `"info"` &lt; `"debug"`) |

### Directories
The Background Function will only evaluate rules for objects within **directories.source**, and modify objects only in **directories.target**. This restriction has a dual-purpose: to avoid event loops, and to encourage a distinction between original content and generated content in your bucket. If this is too restrictive for your scenario, set both options to `""` to evaluate rules on all events, and to let actions modify objects anywhere in the bucket.

| option | default | description |
| :----- | :------ | :---------- |
| **directories.source** | `content` | Input directory within the Cloud Storage bucket to monitor for changes. |
| **directories.target** | `cache` | Output directory within the Cloud Storage bucket where the source hierarchy is mirrored, and possibly altered by the configured **actions** for a matching rule. |

### File Types
The only file type that has special significance is `image`; other file types may be defined to simplify your rules. The **optiimise** function in [file.js](file.js) detects whether a file is an image based on the configured `image` file type.

| option | default | description |
| :----- | :------ | :---------- |
| **filetypes[name].match** |  | Regular expression matching the relative path of the object in the event |

Example:

```json
{
  "filetypes": {
    "image": {
      "match": "\\.(png|jpg|gif)$"
    },
    "json": {
      "match": "\\.json$"
    }
  }
}
```

### Actions
Predefined actions can be referenced in the **Rules** section by name. By default, all exported functions in the [file](file.js) module are available to rules as actions. All other actions have to be defined in the configuration before they can be referenced in a rule.

| option | default | description |
| :----- | :------ | :---------- |
| **actions[name].module** | `"file"` | Name of a JavaScript module within **@growit-io/google-cloud-storage-function** without the `.js` suffix |
| **actions[name].function** | `name` | Name of an exported function in the module. Defaults to the name of the defined action. |
| **actions[name].options** | `{}` | Options to pass as `options` argument to the action function |

### Rules
Rules are evaluated sequentially for each change event emitted by the Cloud Storage bucket that relates to a *file object* within **directories.source**. Events for directories and file objects outside of the source directory are ignored. If a rule matches, then its **actions** are run in sequence and the rule evaluation stops, unless the rule has **continue** set to `true`.

| option | default | description |
| :----- | :------ | :---------- |
| **rules[].events** | | Names of event types to match (`"add"`, `"update"`, `"delete"`) |
| **rules[].filetypes** | | Names of defined **File Types** the object in the event must match |
| **rules[].match** | | Regular expression matched against the path of the object in the event |
| **rules[].actions** | `[]` | Names of defined **Actions** to run in sequence, if this rule matches |
| **rules[].continue** | `false` | Whether to continue evaluation of other rules, if this one matches |

## Development
    npm install --dev
    mkdir -p content
    npm run watch

## Changelog
See [CHANGELOG.md](CHANGELOG.md).

## License
See the [LICENSE](LICENSE) file.

---
_Source: https://npm.io/package/@growit-io/google-cloud-storage-function · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
