koa-gcs

1.0.4 • Public • Published

Koa GCS

Koa GCS middleware

npm downloads Dependency Status NPM version

Maintainers

Installation and Usage

npm install koa-gcs --save
const Koa = require('koa')
const cors = require('@koa/cors')
const Router = require('@koa/router')
const KoaGCS = require('koa-gcs')

const app = new Koa()
const router = new Router()

const config = {
  gcs: {
    bucket: 'your-backet-name',
  },
  maxFileSizeLimit: 5 * 1024 * 1024, // 5mb
  storageOption: {
    projectId: 'your-gcp-project-id',
  },
  image: {
    thumbnail: true,
  },
}

router.post('/upload', koaGCS.multer.single('file'), async (ctx) => {
  const file = await koaGCS.sendUploadToGCS(ctx.req.file, 'users')
  const {
    cloudStoragePublicUrl,
    mimetype,
    fieldname,
    originalname,
    encoding,
    size,
    cloudStorageObject,
    thumbnailUrl,
  } = file
  const { width, height } = ctx.req.body

  ctx.body = {
    thumbnail: thumbnailUrl,
    url: cloudStoragePublicUrl,
    width,
    height,
    mimeType: mimetype,
  }
})
app.use(cors())
app.use(router.routes())
app.use(router.allowedMethods())
app.listen(3000)

example

npm run demo

default config

const defaultConfig = {
  gcs: {
    bucket: 'your-bucket-name',
  },
  maxFileSizeLimit: 5 * 1024 * 1024, // 5mb
  storageOption: {
    keyFilename: path.join(__dirname, './firebase-serviceAccountKey.json'),
    projectId: 'your-gcp-project-id',
  },
  image: {
    resize: '800x',
    quality: 90,
    thumbnail: false,
  },
}

kill server when seeing “EADDRINUSE: address already in use”

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1228:14)
    at listenInCluster (net.js:1276:12)
    at Server.listen (net.js:1364:7)
lsof -i tcp:3000
kill -9 ?????

related projects

License

MIT © alincode

Package Sidebar

Install

npm i koa-gcs

Weekly Downloads

3

Version

1.0.4

License

MIT

Unpacked Size

11.5 kB

Total Files

9

Last publish

Collaborators

  • alincode