Skip to content

Instantly share code, notes, and snippets.

@shaffeeullah
Last active October 17, 2023 07:50
Show Gist options
  • Save shaffeeullah/37eb920033bc7eefbcf89976a04e413c to your computer and use it in GitHub Desktop.
Save shaffeeullah/37eb920033bc7eefbcf89976a04e413c to your computer and use it in GitHub Desktop.
Get a v4 signed URL for uploading file
async function generateV4UploadSignedUrl() {
// The ID of your GCS bucket
const bucketName = 'your-unique-bucket-name';
// The full path of your file inside the GCS bucket, e.g. 'yourFile.jpg'
// or 'folder1/folder2/yourFile.jpg'
const fileName = 'your-file-name';
// These options will allow temporary uploading of the file with outgoing
// Content-Type: application/octet-stream header.
const options = {
version: 'v4',
action: 'write',
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
contentType: 'application/octet-stream',
};
// Get a v4 signed URL for uploading file
const [url] = await storage
.bucket(bucketName)
.file(fileName)
.getSignedUrl(options);
console.log('Generated PUT signed URL:');
console.log(url);
console.log('You can use this URL with any user agent, for example:');
console.log(
"curl -X PUT -H 'Content-Type: application/octet-stream' " +
`--upload-file my-file '${url}'`
);
}
// Full sample here:
// https://github.com/googleapis/nodejs-storage/blob/main/samples/generateV4UploadSignedUrl.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment