Skip to content

Instantly share code, notes, and snippets.

View shaffeeullah's full-sized avatar

Sameena Shaffeeullah shaffeeullah

View GitHub Profile
@shaffeeullah
shaffeeullah / generateV4SignedPolicy.js
Last active October 17, 2023 07:50
Generate signed policy URL for upload via PUT request
async function generateV4SignedPolicy() {
// The ID of your GCS bucket
const bucketName = 'your-unique-bucket-name';
// The ID of your GCS file
const fileName = 'your-file-name';
const bucket = storage.bucket(bucketName);
const file = bucket.file(fileName);
@shaffeeullah
shaffeeullah / generateV4UploadSignedUrl.js
Last active October 17, 2023 07:50
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.
@shaffeeullah
shaffeeullah / useCreateReadStreamWithExpress.js
Created November 1, 2021 17:11
Used createReadStream to read data from a Cloud Storage file
app.get('/', async (req, res) => {
const bucket = storage.bucket(bucketName);
const file = bucket.file(fileName);
const readStream = file.createReadStream();
//Pipe data from the read-stream into the HTTP response body
readStream.pipe(res);
});
@shaffeeullah
shaffeeullah / useSignedURLForDownloadWithExpress.js
Last active October 17, 2023 07:50
Use Cloud Storage signed URL with Express.js
app.get('/', async (req, res) => {
// Get signed URL with action 'read'
const signedUrl = await getReadSignedUrl(bucketName, videoName, 10);
let output = `<video width="400" controls>
<source src=${signedUrl} type="video/mp4">
</video>`
res.send(output);
});
@shaffeeullah
shaffeeullah / generateGetSignedURL.js
Last active November 1, 2021 21:19
Generate a Google Cloud Storage Signed URL to download data via GET request
async function getReadSignedUrl(bucketName, filename, minutesToExpiration) {
const storage = new Storage();
const options = {
version: 'v4',
action: 'read',
expires: minutesToExpiration * 60 * 1000 + Date.now(),
};
const [url] = await storage
.bucket(bucketName)
.file(filename)
@shaffeeullah
shaffeeullah / downloadFilesLargerThan32mb.js
Last active July 25, 2023 15:15
Bypass the Cloud Run 32mb Limit with Cloud Storage
const express = require('express');
const app = express();
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucketName = "my-bucket";
const fileName = "myFile.txt";
const videoName = "myVideo.mov";
async function getSignedUrl(bucketName, filename, minutesToExpiration, action) {