我阅读并发现,上传图像的最佳方法是使用Blob存储。我想将其与node.js一起使用。例如,如何使用node.js代码将图像上传到Blob?
答案 0 :(得分:1)
您可以使用下面列出的SDK
https://github.com/Azure/azure-storage-node#microsoft-azure-storage-sdk-for-nodejs-and-javascript-for-browsers
如果您不修改现有代码并从头开始编写,请使用第一个代码(适用于JavaScript的Storage SDK v10)。您可以在下面引用我的代码:
const {
Aborter, BlobURL,
BlockBlobURL, ContainerURL,
ServiceURL,
StorageURL,
SharedKeyCredential,
generateBlobSASQueryParameters,
uploadStreamToBlockBlob,
BlobSASPermissions,
SASProtocol,
AnonymousCredential,
} = require("@azure/storage-blob");
const STORAGE_ACCOUNT_NAME = process.env.STORAGE_ACCOUNT;
const ACCOUNT_ACCESS_KEY = process.env.STORAGE_KEY;
const AS = {}; // Azure Storage
console.log(STORAGE_ACCOUNT_NAME);
/*
const ONE_MEGABYTE = 1024 * 1024;
const FOUR_MEGABYTES = 4 * ONE_MEGABYTE;
const ONE_MINUTE = 60 * 1000;
By default, credential is always the last element of pipeline factories
const factories = serviceURL.pipeline.factories;
const sharedKeyCredential = factories[factories.length - 1];
*/
AS.getServiceUrl = () => {
const credentials = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
const pipeline = StorageURL.newPipeline(credentials);
const serviceURL = new ServiceURL(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, pipeline);
return serviceURL;
}
AS.createBlockBlobSASToken = (blobName, containerName, options) => {
const now = new Date();
now.setMinutes(now.getMinutes() - 5); // Skip clock skew with server
const tmr = new Date();
tmr.setDate(tmr.getDate() + 1);
const credentials = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
const blobSAS = generateBlobSASQueryParameters(
{
blobName,
containerName,
expiryTime: tmr,
permissions: BlobSASPermissions.parse("racwd").toString(),
protocol: SASProtocol.HTTPSandHTTP,
startTime: now,
},
credentials
);
//const sasURL = `${blobURL.url}?${blobSAS}`;
return blobSAS
}
AS.createContainerSASToken = (containerName, options) => {
const now = new Date();
now.setMinutes(now.getMinutes() - 5); // Skip clock skew with server
const tmr = new Date();
tmr.setDate(tmr.getDate() + 1);
const credentials = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
const containerSAS = generateBlobSASQueryParameters(
{
containerName,
expiryTime: tmr,
permissions: ContainerSASPermissions.parse("racwdl").toString(),
protocol: SASProtocol.HTTPSandHTTP,
startTime: now,
version: "2016-05-31"
},
credentials
);
// const sasURL = `${containerURL.url}?${containerSAS}`;
return containerSAS;
}
AS.createBlockBlobSASURL = (blobName, containerName) => {
let blockBlobURL = getBlockBlobUrl(blobName, containerName);
let blobSAS = createBlockBlobSASToken(blobName, containerName);
const sasURL = `${blockBlobURL.url}?${blobSAS}`;
return sasURL;
}
AS.createContainer = (containerName) => {
const serviceURL = getServiceUrl();
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
return containerURL.create(Aborter.none);
}
AS.getBlockBlobUrl = (blobName, containerName) => {
const serviceURL = getServiceUrl();
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
return blockBlobURL;
}
AS.uploadToBlockBlobFromStream = (blobName, containerName, readableStream, fileSizeInBytes) => {
const blockBlobURL = getBlockBlobUrl(blobName, containerName);
return uploadStreamToBlockBlob(Aborter.none,
readableStream,
blockBlobURL,
256 * 1024,
2,
{
progress: function (ev) {
console.log((ev.loadedBytes / fileSizeInBytes) * 100);
}
}
)
}
// const uploadFileFromBuffer = (blobName, containerName,buffer, fileSizeInBytes) =>{
// }
AS.uploadBlockBlobWithSAS = (blobName, containerName, readableStream, fileSizeInBytes) => {
const blockBlobSasUrl = createBlockBlobSASURL(blobName, containerName);
let blockBlobURL = new BlockBlobURL(blockBlobSasUrl, StorageURL.newPipeline(new AnonymousCredential()))
return uploadStreamToBlockBlob(Aborter.none,
readableStream,
blockBlobURL,
4 * 1024 * 1024,
2,
{
progress: function (ev) {
console.log((ev.loadedBytes / fileSizeInBytes) * 100);
}
}
)
}
AS.deleteBlockBlob = (blobName, containerName) => {
const blockBlobURL = getBlockBlobUrl(blobName, containerName);
return blockBlobURL.delete(Aborter.none)
}
AS.downloadBlob = async (blobName, containerName) => {
const blockBlobURL = await getBlockBlobUrl(blobName, containerName);
const downloadStream = await blockBlobURL.download(aborter, 0);
return downloadStream;
}
module.exports = AS;
答案 1 :(得分:0)
如果您想将Blob与nodejs一起使用,则可以使用发布包来实现它。它是一个Node.js程序包和一个与浏览器兼容的JavaScript客户端库,可轻松使用和管理Microsoft Azure存储服务。
更多详细信息,请参考以下github页面:Azure Storage SDK for Node.js。您可以直接使用createBlockBlobFromLocalFile
将本地文件上传到blob。
您还可以将文件上传到流中以创建blob。如果需要一些示例,可以参考下面的代码。
var azure = require('azure-storage');
var blobService = azure.createBlobService("connection string");
blobService.createBlockBlobFromLocalFile('mycontainer', 'taskblob', 'task1-upload.txt', function(error, result, response) {
if (!error) {
// file uploaded
}
});
除此之外,您还可以参考有关Azure Blob nodejs的官方教程:using the client library for Node.js。