Created
March 28, 2021 11:45
-
-
Save hades2510/991560d3428ae2717ff7d27b99fe17a0 to your computer and use it in GitHub Desktop.
Limit Google Storage file upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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