简体   繁体   English

我想将文件上传到 Blob 存储,而不是作为流(缓冲区、base64)而是作为原始文件(jpg 、 png jpeg)

[英]I want to upload files to Blob Storage NOT AS STREAMS (Buffer, base64) BUT AS original file (jpg , png jpeg)

I am unable to find a way to upload a file not as a stream (buffer, base64) but as a file(png,jgeg,jpg) to Azure Storage Blob.我无法找到一种方法将文件上传到 Azure 存储 Blob,而不是作为流(缓冲区、base64)而是作为文件(png、jgeg、jpg)上传。

MY Stream Code is我的流代码是

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  sharedKeyCredential, defaultAzureCredentials
);

createBlob = (blobName,blob)=>{
    try{
      async function main() {
          const containerClient = blobServiceClient.getContainerClient('blue');
          const content = base64_encode(blob.buffer);
          const blockBlobClient = containerClient.getBlockBlobClient(blobName);
          const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
          console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
          return uploadBlobResponse.requestId;
        }

        main();
  }
  catch(err){
      res.send(err)
  }
}

function base64_encode(file) {
    // read binary data
    //var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    return file.toString('base64');
}

It seems that you were using @azure/storage-blob and your code inspired from Create a blob by uploading data to .您似乎正在使用@azure/storage-blob并且您的代码灵感来自Create a blob by uploading data to .

There is a function uploadFile of BlockBlobClient that can help to directly upload a local file to Azure Blob Storage, as the figure below. BlockBlobClient有一个uploadFile函数,可以帮助直接上传本地文件到Azure Blob Storage,如下图。

在此处输入图片说明

Here is my sample code.这是我的示例代码。

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

// Enter your storage account name and shared key
const account = "<your account name>";
const accountKey = "<your account key>";

// Use StorageSharedKeyCredential with storage account and account key
// StorageSharedKeyCredential is only avaiable in Node.js runtime, not in browsers
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  sharedKeyCredential
);

var containerName = '<your container name>';
var blobName = '<your blob name>';

const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);

var filePath = '<your local file path>';
blockBlobClient.uploadFile(filePath);

You can specify the content type in the options您可以在选项中指定内容类型

  await blockBlobClient.uploadStream(stream, bufferSize, maxConcurrency, {
    blobHTTPHeaders: {
      blobContentType: "image/jpeg"
    }
  })

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

相关问题 readAsDataURL 为 PNG 返回无效的 Base64 字符串,但对于 JPG/JPEG 则不返回 - readAsDataURL returns an invalid Base64 string for PNG, but not for JPG/JPEG 将base64转换为图像文件(jpeg,jpg) - Converting base64 to image file (jpeg, jpg) 将base64编码的jpeg上传到Firebase存储(Admin SDK) - Upload base64 encoded jpeg to Firebase Storage (Admin SDK) 如何从bot Emulator上传文件(.pdf,.jpg..jpeg)并转换为二进制(base64)而不将文件保存到本地驱动器? - How to upload file (.pdf,.jpg..jpeg) from bot Emulator and convert to binary (base64) without saving file into local drive? 确定 base64 字符串或缓冲区是否包含没有元数据的 JPEG 或 PNG? 可能吗? - Determine if a base64 string or a buffer contains JPEG or PNG without metadata? Possible? 读取base64中的blob文件并上传到服务器上 - Read blob file in base64 and upload on the server 如何将 base64 图像上传到 firebase 存储? - How can I upload base64 image to firebase storage? 在Node.js中将base64 png转换为jpeg图像 - Convert base64 png to jpeg image in Nodejs 如何使用 Node.js 将 Blob base64 字符串上传到谷歌云存储 - How do you upload a blob base64 string to google cloud storage using Node.js Nodejs 使用 .createBlockBlobFromLocalFile() 将 base64 图像上传到 azure blob 存储 - Nodejs upload base64 image to azure blob storage using .createBlockBlobFromLocalFile()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM