繁体   English   中英

@azure/storage-blob 如何从 Azuree 获取 uploadStream 图像 url 的结果

[英]@azure/storage-blob how to get result of uploadStream image url from Azuree

我正在将流上传到 azure blob 容器,并且它可以正常上传到容器,但我不知道如何获取图像创建的 url,以便我可以将它返回到我的初始调用并在我的 UI 中呈现它。

我可以取回请求 ID,这是否允许我从那里发出请求并获取图像 URL,我是否需要另一个请求,如果需要,我将如何编写它?

提前致谢

我有以下

import formidable from 'formidable';
let streamifier = require('streamifier');
const fs = require('fs');
const os = require('os');

const {
  BlobServiceClient,
  StorageSharedKeyCredential,
  newPipeline
} = require('@azure/storage-blob');

const containerName1 = 'images';
const ONE_MEGABYTE = 1024 * 1024;
const uploadOptions = { bufferSize: 4 * ONE_MEGABYTE, maxBuffers: 20 };

const sharedKeyCredential = new StorageSharedKeyCredential(
  process.env.AZURE_STORAGE_ACCOUNT_NAME,
  process.env.AZURE_STORAGE_ACCOUNT_ACCESS_KEY
);
const pipeline = newPipeline(sharedKeyCredential);

const blobServiceClient = new BlobServiceClient(
  `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,
  pipeline
);

// first we need to disable the default body parser
export const config = {
  api: {
    bodyParser: false
  }
};

export default async function image(req, res) {
  try {
    const form = new formidable.IncomingForm();
    form.uploadDir = os.tmpdir();
    form.keepExtensions = true;
    form.parse(req, async (err, fields, files) => {
      var oldPath = files.image.path;
      var rawData = fs.readFileSync(oldPath);

      const buffer = rawData;

      const stream = streamifier.createReadStream(buffer);

      const containerClient = blobServiceClient.getContainerClient(
        containerName1
      );
      const blockBlobClient = containerClient.getBlockBlobClient(
        files.image.name
      );

      const result = await blockBlobClient.uploadStream(
        stream,
        uploadOptions.bufferSize,
        uploadOptions.maxBuffers,
        { blobHTTPHeaders: { blobContentType: 'image/jpeg' } }
      );

      console.log('result >>>>', result);
      // HERE I AM TRYING TO GET THE IMAGE URL, BUT IT JUST GIVES ME A OBJECT WITH REQUEST 
      // ID AND SUCH

      res.status(200).send({ message: 'File Uploaded' });
    });
  } catch (error) {
    console.error(error);
    res.status(error.requestResult.statusCode).send(error.message);
  }
}

如果您感兴趣的只是获取 blob URL,则无需执行任何特殊操作。 BlockBlobClient有一个名为url的属性,它将为您提供 blob 的 URL。

您的代码可能很简单:

res.status(200).send({ message: 'File Uploaded', url: blockBlobClient.url });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM