微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 Node.js 的 readStream 文件存储到 Redis 中,以及如何从 Redis 中检索存储的 readStream 文件?

如何解决如何将 Node.js 的 readStream 文件存储到 Redis 中,以及如何从 Redis 中检索存储的 readStream 文件?

我尝试将 readStream (Image) 转换为字符串,然后将其存储在 Redis 中。然后从 Redis 检索字符串并将其转换回 readStream。但是没有成功。

function getFile(fileKey) {
  console.log(fileKey);
  const downloadParams = {
    Key: fileKey,Bucket: bucketName,};

  return s3.getobject(downloadParams).createReadStream();
}

exports.getFile = getFile;

为了将流转换为字符串,我使用了流到字符串。它被转换并存储在 Redis 中。

const { getFile } = require("../s3");
const redis = require("redis");

const client = redis.createClient();

var toString = require("stream-to-string");

exports.getFileFromS3Controller = async (req,res) => {
  console.log(req.params);
  const path = req.params.path;
  const key = req.params.key;
  const readStream = getFile(path + "/" + key);

  toString(readStream).then(function (msg) {
    // Set data to Redis
    client.setex(key,3600,msg);
  });

  readStream.pipe(res);
};

在从 Redis 中检索时,我没有得到它。

const redis = require("redis");
const client = redis.createClient(null,null,{ detect_buffers: true });
const Readable = require("stream").Readable;

// Cache middleware
function cache(req,res,next) {
  const { path,key } = req.params;

  client.get(key,(err,data) => {
    if (err) throw err;

    if (data !== null) {
      var s = new Readable();
      s.push(data);
      s.push(null);
      s.pipe(res);
    } else {
      next();
    }
  });
}

router.get("/:path/:key",cache,getFileFromS3Controller);

解决方法

下一个不是你打电话。另一个错误是流没有保存在请求中的任何位置,因此您可以稍后从控制器访问。据我所知,您直接在 res 中编写它,这是一个问题,因为在此之后您不能再使用 res 发送任何其他内容。

这是代码(未测试)

exports.getFileFromS3Controller = (req,res) => {
  if (req.fileStream) {
      req.fileStream.pipe(res);
      return
  }

  console.log(req.params);
  const path = req.params.path;
  const key = req.params.key;
  const readStream = getFile(path + "/" + key);

  toString(readStream).then(function (msg) {
      // Set data to Redis
      client.setex(key,3600,msg);

      // Conver string to readable
      const readable = new Readable();
      readable.push(msg);
      readable.push(null);
      readable.pipe(res);
  });
};

function cache(req,res,next) {
    const { path,key } = req.params;

    client.get(key,(err,data) => {
        if (err) throw err;

        if (data !== null) {
            var s = new Readable();
            s.push(data);
            s.push(null);

            req.fileStream = s;
        }

        next();
    });
}

编辑我修正了答案中的一个错误,因为可读流无法倒回。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。