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
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); |
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
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. |
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
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); | |
}); |
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
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); | |
}); |
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
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) |
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
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) { |