Skip to content

Instantly share code, notes, and snippets.

@hades2510
Created March 28, 2021 11:45
Show Gist options
  • Save hades2510/991560d3428ae2717ff7d27b99fe17a0 to your computer and use it in GitHub Desktop.
Save hades2510/991560d3428ae2717ff7d27b99fe17a0 to your computer and use it in GitHub Desktop.
Limit Google Storage file upload
import { GetSignedUrlConfig, Storage } from '@google-cloud/storage'
const storage = new Storage()
// this comes from the front end
const input = {
contentType: 'application/jpeg',
contentLength: 10000 // size in bytes
}
const filename = 'somefilename'
const writeOptions: GetSignedUrlConfig = {
version: 'v4',
action: 'write',
expires: Date.now() + parseInt(process.env.MAX_UPLOAD_TIME_IN_MILLISECONDS),
contentType: input.contentType,
extensionHeaders: {
"content-length": input.contentLength
}
}
const url = await storage
.bucket(process.env.BUCKET_NAME)
.file(filename)
.getSignedUrl(writeOptions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment