微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将所有格式的 excel 文件上传/下载到 azure blob 存储 Nodejs 服务器端

如何解决如何将所有格式的 excel 文件上传/下载到 azure blob 存储 Nodejs 服务器端

我正在尝试将 excel 文件上传到 azure blob 存储

import { DefaultAzureCredential } from '@azure/identity';
import { BlobServiceClient } from '@azure/storage-blob';

// Enter your storage account name
const account = process.env.ACCOUNT_NAME;

// Unique name for the container
const containerName = process.env.CONTAINER_NAME;

// Azure AD Credential information is required to run this sample:
if (
  !process.env.AZURE_TENANT_ID ||
  !process.env.AZURE_CLIENT_ID ||
  !process.env.AZURE_CLIENT_SECRET || !account || !containerName
) {
  return next(SystemResponse.badRequestError('Azure AD authentication information not provided,but it is required to run this sample. Exiting.'));
}

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



// Get a reference to a container
const containerClient = blobServiceClient.getContainerClient(containerName);

// Upload data to the blob
const blobName = `${req.files.file.name.split('.')[0]}+${new Date().getTime()}.${req.files.file.name.split('.')[1]}`;

// create blobClient for container
const blockBlobClient = containerClient.getBlockBlobClient(blobName);

// upload file
const uploadBlobResponse = await blockBlobClient.upload(`${containerName}${req.files.file.path}`,req.files.file.size);

我正在使用邮递员上传 excel 文件,但是,只有文件路径存储在 excel 文件中,而不是实际内容,我该如何解决这个问题,请帮忙? && 关于下载文件名的响应

解决方法

我创建了一个 azure function,也许您可​​以使用 parse-multipart 来接收您的文件:

安装:

npm install parse-multipart

代码:

import { AzureFunction,Context,HttpRequest } from "@azure/functions"
import { DefaultAzureCredential } from '@azure/identity';
import { BlobServiceClient } from '@azure/storage-blob';

const httpTrigger: AzureFunction = async function (context: Context,req: HttpRequest): Promise<void> {
    var multipart = require('parse-multipart');
    var bodyBuffer = Buffer.from(req.body);
    var boundary = multipart.getBoundary(req.headers['content-type']);
    var parts = multipart.Parse(bodyBuffer,boundary);

    // Enter your storage account name
    const account = process.env.ACCOUNT_NAME;

    // Unique name for the container
    const containerName = process.env.CONTAINER_NAME;

    // Azure AD Credential information is required to run this sample:
    if (
      !process.env.AZURE_TENANT_ID ||
      !process.env.AZURE_CLIENT_ID ||
      !process.env.AZURE_CLIENT_SECRET || !account || !containerName
    ) {
      return next(SystemResponse.badRequestError('Azure AD authentication information not provided,but it is required to run this sample. Exiting.'));
    }

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



    // Get a reference to a container
    const containerClient = blobServiceClient.getContainerClient(containerName);

    // Upload data to the blob
    const blobName = `${parts[0].filename.split('.')[0]}+${new Date().getTime()}.${parts[0].filename.split('.')[1]}`;

    // create blobClient for container
    const blockBlobClient = containerClient.getBlockBlobClient(blobName);

    // upload file
    const uploadBlobResponse = await blockBlobClient.upload(parts[0].data,parts[0].data.length);
};

原因:

function upload(body: HttpRequestBody,contentLength: number,options?: BlockBlobUploadOptions)

HttpRequestBody 的值应该是 Blob,string,ArrayBuffer,ArrayBufferView or a function which returns a new Readable stream whose offset is from data source beginning,所以你不应该使用路径。

================更新========================

根据讨论,应该按照相对路径读取文件,并将文件的二进制数据转换为base64字符串。

var fs = require('fs');
var path = require('path'); 
//set relative path to file 
let relativePath = path.relative(process.cwd(),${req.files.file.path}); 
//reading binary data of file 
let binaryData=fs.readFileSync(relativePath); 
//converting binary data to base64 string 
let base64String=Buffer.from(binaryData).toString("base64")
await blockBlobClient.upload(base64String,req.files.file.size);

================更新2====================

    const containerClient = blobServiceClient.getContainerClient(containerName);

    // Upload data to the blob
    const blobName = `xxx.xlsx`;

    // create blobClient for container
    const blockBlobClient = containerClient.getBlockBlobClient(blobName);

    const downloadBlockBlobResponse = blockBlobClient.downloadToFile("D:\\xxx\\xxx.xlsx");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。