Skip to content

Instantly share code, notes, and snippets.

@shaffeeullah
Last active July 25, 2023 15:15
Show Gist options
  • Save shaffeeullah/088aa29c3ee9876a6fec7b3fbbf1862f to your computer and use it in GitHub Desktop.
Save shaffeeullah/088aa29c3ee9876a6fec7b3fbbf1862f to your computer and use it in GitHub Desktop.
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) {
const storage = new Storage();
const options = {
version: 'v4',
action: action,
expires: minutesToExpiration * 60 * 1000 + Date.now(),
};
const [url] = await storage
.bucket(bucketName)
.file(filename)
.getSignedUrl(options);
return url;
}
/////////////////////////////////////////////////////////
////////////////////// DOWNLOADS ////////////////////////
/////////////////////////////////////////////////////////
// Signed URL
app.get('/', async (req, res) => {
// Get signed URL with action 'read'
const signedUrl = await getSignedUrl(bucketName, videoName, 10, 'read');
let output = `<video width="400" controls>
<source src=${signedUrl} type="video/mp4">
</video>`
res.send(output);
});
// Stream
app.get('/', async (req, res) => {
const bucket = storage.bucket(bucketName);
const file = bucket.file(fileName);
const readStream = file.createReadStream();
//Pipe data from the GCS read-stream into the HTTP response body
readStream.pipe(res);
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`bypass32mbLimit: listening on port ${port}`);
});
@onceknown
Copy link

I have tried the streaming approach using v2 google cloud functions, which are backed by cloud run, and the streaming response is truncated at 31MB each time. Not sure what's changed, but the streaming approach looks to no longer bypass the 32mb response limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment