【问题标题】:Synchronizing stream outputs同步流输出
【发布时间】:2019-02-15 14:29:13
【问题描述】:

我正在努力将不同尺寸的图片上传到亚马逊 s3 存储。每当我上传图像时,我都会创建三个独立的图像可读流,并将其通过管道传输到我的调整大小函数和 s3 上传函数。

每个流都将不同大小的图像上传到 s3 云。我希望同步流的输出,以便在所有三个流中的上传完成后发送已上传照片的 URL。

我无法知道我的所有直播何时结束。或者我怎样才能等到我的所有流都结束了,然后返回所有上传图片的 URL?

这是我的代码

调整大小.js

import fs from 'fs';
import sharp from 'sharp';

export const resizeThumbnail = function (path, format) {
  const readStream = fs.createReadStream(path);
  let transform = sharp();
  if (format) {
    transform = transform.toFormat(format);
  }
  transform = transform.resize(100);
  return readStream.pipe(transform);
};

export const resizeLowRes = function (path, format) {
  const readStream = fs.createReadStream(path);
  let transform = sharp();
  if (format) {
    transform = transform.toFormat(format);
  }
  transform = transform.resize(500);
  return readStream.pipe(transform);
};

export const resizeHighRes = function (path, format) {
  const readStream = fs.createReadStream(path);
  let transform = sharp();
  if (format) {
    transform = transform.toFormat(format);
  }
  transform = transform.resize(1080);
  return readStream.pipe(transform);
};

resizer.js

import { resizeThumbnail, resizeLowRes, resizeHighRes } from './resize';
import awsConfig from '../config/awsConfig';
import { uploadFromStream } from './amazonS3';

// receives a list of images
export const resizer = (images, id, callback) => {
  const imageList = images.split(',');
  const store = [];
  imageList.forEach((imagePath, i) => {
    sizeCreator(imagePath, id, i, (err, localstore) => {
      if (!err) {
        store.push(localstore);
        console.log(store);
      }
      callback(err);
    });
  });
};

const sizeCreator = (imagePath, id, i, callback) => {
  const localstore = {};
  resizeThumbnail(imagePath).pipe(uploadFromStream(awsConfig, id, i, 'thumbnail', (err, data) => {
    localstore.thumbnail = {
      url: data.Location,
      filename: data.key,
    };
  }));
  resizeLowRes(imagePath).pipe(uploadFromStream(awsConfig, id, i, 'lowres', (err, data) => {
    localstore.lowres = {
      url: data.Location,
      filename: data.key,
    };
  }));
  resizeHighRes(imagePath).pipe(uploadFromStream(awsConfig, id, i, 'highres', (err, data) => {
    localstore.highres = {
      url: data.Location,
      filename: data.key,
    };
    callback(err, localstore);
  }));
};

amazonS3.js

import stream from 'stream';

export const uploadFromStream = (s3, id, index, res, callback) => {
  const pass = new stream.PassThrough();
  const params = {
    Bucket: 'Bucket',
    Key: `${id}_${res}_${index}`,
    Body: pass,
    ContentType: 'image/jpeg',
  };
  s3.upload(params, (err, data) => {
    callback(err, data);
  });
  return pass;
};

【问题讨论】:

    标签: node.js express stream synchronization aws-sdk


    【解决方案1】:

    您可以计算已开始的流并在完成流事件时更新计数器。当最后一个流完成时,计数器的值应该为 0。然后您可以触发发送 url 的不同事件。

    【讨论】:

    • 谢谢,您的想法有所帮助。现在可以了。但是有没有更好的方法通过异步等待来处理?
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2011-03-05
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    相关资源
    最近更新 更多