最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

同步流输出

运维笔记admin13浏览0评论

同步流输出

同步流输出

我正在努力将不同的图像尺寸上传到亚马逊s3存储。每当我上传图像时,我都会创建三个独立的可读图像流,并将其传送到我的缩放器功能和s3上传功能。

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

我无法知道我的所有视频流何时结束。或者我怎么能等到我的所有流都结束然后返回所有上传图像的URL?

这是我的代码

resize.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;
};
回答如下:

您可以在完成流事件中计算启动流和更新计数器。当最后一个流完成时,计数器应该具有值0.然后,您可以触发发送URL的不同事件。

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论