使用节点 js 将图像上传到 Microsoft Azure Blob Storage
Upload a image to Microsoft Azure Blob Storage with node js
我正在尝试将图像上传到我的 blob 存储,但它上传的是文件路径的文本而不是图像
const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
const account = "<account name hided";
const accountKey = "<key hided>";
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);
var containerName = 'democontainer1';
async function createContainer() {
const containerClient = blobServiceClient.getContainerClient(containerName);
var blobName = "newblob" + new Date().getTime();
var filePath = "./newblob.jpg";
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(filePath, filePath.length);
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
}
createContainer();
我想要这样:
但输出是:
请更改以下代码行:
const uploadBlobResponse = await blockBlobClient.upload(filePath, filePath.length);
至
const uploadBlobResponse = await blockBlobClient.uploadFile(filePath);
您应该会看到正在上传正确的 blob。
我正在尝试将图像上传到我的 blob 存储,但它上传的是文件路径的文本而不是图像
const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
const account = "<account name hided";
const accountKey = "<key hided>";
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);
var containerName = 'democontainer1';
async function createContainer() {
const containerClient = blobServiceClient.getContainerClient(containerName);
var blobName = "newblob" + new Date().getTime();
var filePath = "./newblob.jpg";
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(filePath, filePath.length);
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
}
createContainer();
我想要这样:
但输出是:
请更改以下代码行:
const uploadBlobResponse = await blockBlobClient.upload(filePath, filePath.length);
至
const uploadBlobResponse = await blockBlobClient.uploadFile(filePath);
您应该会看到正在上传正确的 blob。