node.js - 在没有公共(public) URL 的 Node.js 中从 Google Cloud Storage 检索图像

标签 node.js google-cloud-storage

我正在尝试将图像返回给我的用户以在 DOM 上查看,而不创建图像的公共(public)链接(在取回图像之前,此路由受到我的服务器身份验证的保护)。这是我到目前为止所尝试过的,但这可能是错误的方法。

我按照 Google Cloud Storage with Node.js 指南将图像上传添加到我的项目中。我现在上传工作正常,图像保存在 Google 云存储中。该指南展示了如何检索公共(public) URL:

https://cloud.google.com/nodejs/getting-started/using-cloud-storage

我正在尝试使用文件流而不是公共(public) URL,以便我的图像不会公开。我猜它看起来与此存储库中的流类似: https://github.com/GoogleCloudPlatform/google-cloud-node

但我试图通过 HTTP 响应返回图像,而不是简单地将它们存储在本地文件系统上。

我的客户端 Javascript 收到的响应看起来像一个巨大的 Blob 。 ����JFIF���Photoshop 3.08BIM� 元数据让我认为这实际上是正确的图像,但我不确定如何让它恢复到图像的样子。

这是我的 Node 服务器文件。上传到 Google Cloud Storage 的 /post 正在运行,我可以看到正在添加的图像。

Google Cloud Storage Upload Working

/get 返回的内容看起来像一个巨大的 Blob ,看起来像是我想要的图像(就像一个我不知道如何转换的奇怪 Blob )。

我的代码如下所示:

var express = require('express');
var router = express.Router();
const Multer = require('multer');
const multer = Multer({
  storage: Multer.memoryStorage(),
  limits: {
    fileSize: 5 * 1024 * 1024 // no larger than 5mb, you can change as needed.
  }
});

const Storage = require('@google-cloud/storage');

const storage = Storage({
  keyFilename: 'server/firebase-service-account.json',
  projectId: process.env.FIREBASE_PROJECT_ID
});

const bucket = storage.bucket(process.env.FIREBASE_STORAGE_BUCKET);

// Process the file upload and upload to Google Cloud Storage.
router.post('/', multer.single('file'), (req, res, next) => {
  console.log('req.body', req.body);
  console.log('req.file', req.file);
  if (!req.file) {
    res.status(400).send('No file uploaded.');
    return;
  }

  // Create a new blob in the bucket and upload the file data.
  const blob = bucket.file(req.file.originalname);
  const blobStream = blob.createWriteStream();

  blobStream.on('error', (err) => {
    next(err);
    return;
  });

  blobStream.on('finish', () => {
    res.status(200).send('The image has been successfully uploaded to google cloud storage');
  });

  blobStream.end(req.file.buffer);
});

router.get('/', function (req, res, next) {
  var stream = bucket.file('Toast.jpg').createReadStream();

  stream.pipe(res);

  stream.on('data', function (data) {
    res.write(data);
  });

  stream.on('error', function (err) {
    console.log('error reading stream', err);
  });

  stream.on('end', function () {
    res.set({
      "Content-Disposition": 'attachment; filename="Toast.jpg"',
      "Content-Type": 'image/jpg'
    });
    res.end();
  });
});

module.exports = router;

这接近吗?有没有更好的方法通过我的身份验证来保护我的图像?

最佳答案

我的想法是错误的。我需要路由我的 <img src=""到正确的位置。

HTML

<img src="/uploads/1234">

服务器 Node

var express = require('express');
var router = express.Router();
const Multer = require('multer');
const multer = Multer({
  storage: Multer.memoryStorage(),
  limits: {
    fileSize: 5 * 1024 * 1024 // no larger than 5mb, you can change as needed.
  }
});

const Storage = require('@google-cloud/storage');

const storage = Storage({
  keyFilename: 'server/firebase-service-account.json',
  projectId: process.env.FIREBASE_PROJECT_ID
});

const bucket = storage.bucket(process.env.FIREBASE_STORAGE_BUCKET);

// Process the file upload and upload to Google Cloud Storage.
router.post('/', multer.single('file'), (req, res, next) => {
  console.log('req.body', req.body);
  console.log('req.file', req.file);
  if (!req.file) {
    res.status(400).send('No file uploaded.');
    return;
  }

  // Create a new blob in the bucket and upload the file data.
  const blob = bucket.file(req.file.originalname);
  const blobStream = blob.createWriteStream();

  blobStream.on('error', (err) => {
    next(err);
    return;
  });

  blobStream.on('finish', () => {
    res.status(200).send('The image has been successfully uploaded to google cloud storage');
  });

  blobStream.end(req.file.buffer);
});

router.get('/:imageId', function (req, res, next) {
  var stream = bucket.file('Toast.jpg').createReadStream();

  res.writeHead(200, {'Content-Type': 'image/jpg' });

  stream.on('data', function (data) {
    res.write(data);
  });

  stream.on('error', function (err) {
    console.log('error reading stream', err);
  });

  stream.on('end', function () {
    res.end();
  });
});

module.exports = router;

我仍然希望得到有关这是否是解决此问题的正确方法的反馈。 (显然,需要进行硬编码。)

关于node.js - 在没有公共(public) URL 的 Node.js 中从 Google Cloud Storage 检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43171193/

相关文章:

javascript - 如何在 Node.js 测试中关闭 net.Server?

node.js - 在 Mongoose 上切换数据库安全吗?

java - Google App Engine 和云存储 : The AppIdentity service threw an unexpected error

python-3.x - 可以将我的临时文件放入我的应用程序中的 tmp/目录吗?

google-cloud-storage - 允许对现有文件使用 gzip

mysql - 创建pdf时 Node 中的同步mysql查询

node.js - 通过 through2 进行管道输出

node.js - node-websocket-server : possible to have multiple, 单独的 "broadcasts"用于单个 node.js 进程?

google-cloud-storage - 属性错误 : 'AuthorizedSession' object has no attribute 'configure_mtls_channel'

google-app-engine - GCS bucket Java 客户端 api 中的通配符