英文:
How to upload files(images, pdf) using Azure blob storage with standard account?
问题
我正在使用Azure Blob Storage标准帐户。当我尝试上传文件时,它会抛出错误'BlobTypeNotSupported'。我知道标准帐户不支持'块 Blob'。是否有其他方法可以使用标准帐户上传文件?
英文:
I am using azure blob storage standard account. When I try to upload files, it throws the error 'BlobTypeNotSupported'. I know standard account doesn't support 'Block Blobs'. Is there any other way to upload files using standard account?
答案1
得分: 1
以下是翻译好的内容:
我在我的环境中尝试并获得以下结果:
我通过 Azure 门户创建了一个标准存储帐户:
我使用以下代码来使用 Nest.js 上传图像
代码:
import { BlobServiceClient, BlockBlobClient } from '@azure/storage-blob';
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
azureConnection = "<连接字符串>";
containerName = "test";
getBlobClient(imageName: string): BlockBlobClient {
const blobClientService = BlobServiceClient.fromConnectionString(this.azureConnection);
const containerClient = blobClientService.getContainerClient(this.containerName);
const blobClient = containerClient.getBlockBlobClient(imageName);
return blobClient;
}
async upload(file: Express.Multer.File) {
const blobClient = this.getBlobClient(file.originalname);
await blobClient.uploadData(file.buffer);
}
}
控制台:
Postman:
您可以使用 Postman 发送 POST 请求来上传图像。
门户:
上述请求成功地通过 Azure 存储标准帐户使用 Nest.js 上传了文件,文件类型为 Block-blob。
参考链接:
[NestJS 使用 Azure Blob 存储进行 API 文件操作 (learmoreseekmore.com)](https://www.learmoreseekmore.com/2021/03/nestjs-api-file-operations-using-azure-blob-storage.html#:%7E:text=在本文中,我们将使用 Azure Blob 存储执行 API 文件操作,将大量的文件数据作为非结构化数据上传。)
作者:Naveen。
英文:
I tried in my environment and got below results:
I created a standard storage account through azure portal :
I used the below code to upload images using nest.js
Code:
import { BlobServiceClient, BlockBlobClient } from '@azure/storage-blob';
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
azureConnection = "< Connection string >";
containerName = "test";
getBlobClient(imageName:string):BlockBlobClient{
const blobClientService = BlobServiceClient.fromConnectionString(this.azureConnection);
const containerClient = blobClientService.getContainerClient(this.containerName);
const blobClient = containerClient.getBlockBlobClient(imageName);
return blobClient;
}
async upload(file:Express.Multer.File){
const blobClient = this.getBlobClient(file.originalname);
await blobClient.uploadData(file.buffer);
}
}
console:
Postman:
You can use the post request to upload image via postman.
Portal:
The above request worked successfully and uploaded file through azure storage standard account using nest.js with Block-blob type.
Reference:
NestJS API File Operations Using Azure Blob Storage (learmoreseekmore.com)
by Naveen.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论