大家好,我正在尝试制作一些像流视频这样的网站,所以我使用了react (客户端),node (服务器),mangodb (数据库),google cloude storage (视频云存储)
我的问题是,如何从nodejs导入文件到GCS并获得链接,这样我就可以将链接保存到mangodb中,然后我可以将视频流式传输到客户端,谢谢
发布于 2020-11-04 18:32:07
与其将文件的url保存到Mongodb,不如生成一个signed url。这将为用户提供对文件的临时访问权限。通过这种方式,您可以轻松控制谁可以访问您的视频。另一方面,如果你不使用这种方法,那么你将被迫公开你的文件,任何人都可以访问它们,我猜这不是你想要的。
要生成带签名的url,请检查下面的示例
// Imports the Google Cloud client library
const { Storage } = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function generateSignedUrl() {
// These options will allow temporary read access to the file
const options = {
version: 'v2', // defaults to 'v2' if missing.
action: 'read',
expires: Date.now() + 1000 * 60 * 60, // one hour
};
// Get a v2 signed URL for the file
const [url] = await storage.bucket(bucketName).file(filename).getSignedUrl(options);
console.log(`The signed url for ${filename} is ${url}.`);
}https://stackoverflow.com/questions/64678120
复制相似问题