使用 Node 和 graphql 将上传文件流式传输到 Azure blob 存储 [英] Stream upload file to Azure blob storage with Node and graphql

查看:33
本文介绍了使用 Node 和 graphql 将上传文件流式传输到 Azure blob 存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 nodejs 和 apollo-server-express 将图像存储到 Azure blob 存储.我使用 createBlockBlobFromStream 进行存储,但它需要流长度.我怎样才能找到流长度?有没有更好的方法使用 graphql 将图像存储到 azure blob 存储

I need to store an image to Azure blob storage using nodejs and apollo-server-express.I am using createBlockBlobFromStream for storing but it requires stream length. how can I find the stream length? Is there any better way to store images to azure blob storage using graphql

uploadImageFunc 由解析器通过传递参数调用

uploadImageFunc is called by the resolver by passing the args

const fs = require('fs')
const azure = require('azure-storage')
const blobService = azure.createBlobService("connection string")


const uploadImageFunc = async function(args) {
    return args.file.then(file => {
        const {createReadStream, filename, mimetype} = file

        const fileStream = createReadStream()
        
        blobService.createBlockBlobFromStream('container-name',filename,fileStream,"fileStream.length",(error,response) => {
          if(!error){
            console.log(response)
          }
        })

        fileStream.pipe(fs.createWriteStream(__dirname+`/uploadedFiles/${filename}`))

        return file;
        
      });
}

module.exports = uploadImageFunc;

感谢回复

推荐答案

实际上,流大小存在于请求的内容长度中.req.headers['content-length'] 将内容长度作为字符串.使用 parseInt 将其转换为 int 并将其传递给 createBlockBlobFromStream 函数.

Actually, the stream size was present in the content length of the request. req.headers['content-length'] has the content length as a string. convert it into int using parseInt and pass it to the createBlockBlobFromStream function.

const fs = require('fs')
const { v4: uuidv4 } = require('uuid');
const azure = require('azure-storage')
const blobService = azure.createBlobService(process.env.AZURE_STORAGE_CONNECTION_STRING)

const contestEntryModel = require('../models/contestEntryModel')


const uploadImageFunc = async function(args,{req, res}) {
      return args.file.then ( file => {
        const {createReadStream, filename, mimetype} = file

        let streamSize = parseInt(req.headers['content-length'])

        const fileStream = createReadStream()

        const newFilename = uuidv4()
        
      blobService.createBlockBlobFromStream('<container name>',`${args.contestId}/${newFilename}`,fileStream,streamSize,(error,response) => {
        if(!error){
          let entry = {
            url:`<blobstorage url>/${args.contestId}/${newFilename}`,
            participant: req.userId
          }
          contestEntryModel.findOneAndUpdate({contestId: args.contestId},{$push:{entries:entry}},(err, doc) => {
            console.log("updated")
          })
        }
      })


        return true;
        
      });
    
}

module.exports = uploadImageFunc;

这篇关于使用 Node 和 graphql 将上传文件流式传输到 Azure blob 存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆