一,安装依赖
1、npm install @azure/storage-blob
二,使用存储连接字符串初始化客户
1、字符串连接客户端
import { BlobServiceClient } from '@azure/storage-blob';
const BlobConnectString= "DefaultEndpointsProtocol=https;AccountName=XXX;AccountKey=XXX;EndpointSuffix=XXX";
const blobServiceClient = BlobServiceClient.fromConnectionString(BlobConnectString);
2、获取客户端存储位置
const containerClient = blobServiceClient.getContainerClient(”xxx-BlobName“);
3、设置存储路径
const blobName="XXX.MP3"
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
4、从上一步传入的二进制音频流
content = Buffer.from(content, 'binary')
5、上传到blob服务
await blockBlobClient.upload(content, Buffer.byteLength(content), {
blobHTTPHeaders: {
blobContentType: 'audio/mp3',
},
metadata: {
encoding: 'mp3',
format: 'fltp',
number_of_channel: '1',
sample_rate: '16000',
file_size: '55728 byte',
duration: '13.932s'
}
});
6、源地址+blobName 就是上传的文件地址,可直接访问