# gcs-upload

> Express middleware to upload file to google cloud storage.

Latest version **1.2.0** (published 2020-01-21) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install gcs-upload
pnpm add gcs-upload
yarn add gcs-upload
bun add gcs-upload
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2020-01-21 |
| First published | 2019-09-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 (+11 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 25 |
| Author | Armedi |
| Maintainers | armedi |
| Keywords | google cloud storage, expressjs, uploader |

## Links

- npm: https://www.npmjs.com/package/gcs-upload
- Repository: https://github.com/armedi/gcs-upload
- Issues: https://github.com/armedi/gcs-upload/issues
- npm.io page: https://npm.io/package/gcs-upload

## Dependencies (2)

- [multer](https://npm.io/package/multer.md) ^1.4.2
- [@google-cloud/storage](https://npm.io/package/@google-cloud/storage.md) ^4.2.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

- 1.2.0 (latest) — 2020-01-21
- 1.1.3 — 2019-12-15
- 1.1.2 — 2019-09-24
- 1.1.1 — 2019-09-23
- 1.1.0 — 2019-09-23
- 1.0.1 — 2019-09-21
- 1.0.0 — 2019-09-21

## README

# gcs-upload

This is a convenient wrapper around multer and google cloud storage API as a single express middleware to upload file from user to Google Cloud Storage.

It will change the field for the uploaded file with a url string like http://storage.googleapis.com/`bucket-name`/`filename` that you can save in database.


### Requirements
1. Make sure you have a google cloud project with billing enabled.
2. [Enable Google Storage API](https://console.cloud.google.com/flows/enableapi?apiid=storage-api.googleapis.com) for the project.
3. [Create a bucket](https://console.cloud.google.com/storage/create-bucket) to store the files.
4. [Create a service account](https://console.cloud.google.com/apis/credentials/serviceaccountkey) and download the credential in JSON format.


## Installation

```bash
npm install gcs-upload
```


## Usage

**options**:
- _limit_: limits of uploaded data in object form (similar to limits option in [multer](https://github.com/expressjs/multer#limits))
- _gcsConfig_:
  - keyFilename: file path for credential that you have downloaded before.
  - bucketName: the bucket name that will contain the uploaded file, you can create one through google cloud console.
  - prefix (optional): it's a string or a function that return a string which will be used as a prefix for filenames being stored. 

It will return an upload object that have 3 methods: (.single(), .array(), and .fields()). You can use all of them just like how you would use [multer](https://github.com/expressjs/multer#singlefieldname).

note:
> Instead of providing keyFilename in config, you can also use `GOOGLE_APPLICATION_CREDENTIALS` environment variable to store absolute path to the credential file.


### Basic Example

Don't forget the enctype="multipart/form-data" in your form.
```html
<form action="/upload-single" method="post" enctype="multipart/form-data">
  <input type="file" name="file" />
</form>
```

```javascript
const express = require('express')
const gcsUpload = require('gcs-upload')

const app = express()

// .......

const upload = gcsUpload({
  limits: {
    fileSize: 1e6 // in bytes
  },
  gcsConfig: {
    keyFilename: '/Users/me/google-credential-keyfile.json',  // this can also be set using GOOGLE_APPLICATION_CREDENTIALS environment variable 
    bucketName: 'my-bucket',
    prefix: () => `${Date.now()}-` // optional, this is it's default value
  }
})

app.post('/upload-single', upload.single('file'), (req, res) => {
  console.log(req.body)
  res.end()
})

app.post('/upload-array', upload.array('files'), (req, res) => {
  console.log(req.body)
  res.end()
})

app.post('/upload-fields',
  upload.fields([{ name: 'file1' }, { name: 'file2' }]),
  (req, res) => {
    console.log(req.body)
    res.end()
  }
)

// .......
```

To make uploaded files available for public view, add `Storage Object Viewer` role for allUsers. Step by step instruction can be found [here](https://cloud.google.com/storage/docs/access-control/making-data-public#buckets)

---
_Source: https://npm.io/package/gcs-upload · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
