如何通过 Node.js 应用程序正确传输 Google 云存储资产,以包含缓存 headers 等

How to properly pipe Google Cloud Storage asset through Node.js app, to include cache headers and such

所以我基本上有这个(来自here):

const { Storage } = require('@google-cloud/storage')
const express = require('express')
const app = express()

const storage = new Storage({
  projectId: 'my-project'
})

const bucket = storage.bucket('my.bucket')

// logo and assets
app.get('/:id.:ext', (req, res) => {
  const remote = bucket.file(`${req.params.id}.${req.params.ext}`)
  remote.createReadStream().pipe(res)
})

我想知道我需要做什么来设置缓存 headers 或者它是否以某种方式通过管道自动发生。我希望它永久缓存一些,1 个月缓存其他。

如果您想在响应中设置 headers,您需要使用 Express API 来实现。看看您可以使用 Response object. You'll likely want to use res.set() 做什么。管道只是要处理响应的 body 的内容。