无法流式传输和上传图像
Not able to stream and upload image
我正在尝试将多图像上传到 azure blob 服务,一切看起来都很好,尽管我遗漏了一些东西
我得到的错误
events.js:292
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open 'logo192.png' Emitted
'error' event on ReadStream instance at:
at internal/fs/streams.js:163:14
at FSReqCallback.oncomplete (fs.js:159:23) { errno: -2, code: 'ENOENT', syscall: 'open', path: 'logo192.png' }
const uuidv1 = require("uuid/v1");
var azure = require("azure-storage");
var fs = require("fs");
const containerName = "image";
const fileUpload = async (req, res, next) => {
let imageStatus = [];
console.log(req.files);
await req.files.forEach((reqfile, i) => {
const blobName = uuidv1() + "-" + reqfile.originalname;
const blobSvc = azure.createBlobService();
const file = reqfile.originalname;
const stream = fs.createReadStream(file).pipe(blobSvc.createWriteStreamToBlockBlob(containerName, blobName, { blockIdPrefix: "block" }));
let dataLength = 0;
stream
.on("data", function (chunk) {
dataLength += chunk.length;
})
.on("end", function () {
console.log("The length was:", dataLength);
});
blobSvc.createBlockBlobFromStream(containerName, blobName, stream, dataLength, function (error, result, response) {
if (!error) {
console.log("ok Blob uploaded");
imageStatus.push({
imageName: result.name,
imagePath: result.name,
});
} else {
console.log(error);
}
});
});
res.status(200).json({ imageStatus });
};
module.exports = fileUpload;
req.files
的控制台输出
[
{
fieldname: 'files',
originalname: 'logo192.png',
encoding: '7bit',
mimetype: 'image/png',
buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 c0 00 00 00 c0 08 03 00 00 00 65 02 9c 35 00 00 00 87 50 4c 54 45 00 00 00 64 da fb 61 da fc ... 5297 more bytes>,
size: 5347
}
]
如果您想在节点应用程序中将图像上传到 Azure blob,我建议您使用新的 SDK @azure/storage-blob
。 SDK 是 azure-storage
是 Legacy。
例如
- SDK
npm install into-stream @azure/storage-blob multer
PS:
- sdk
into-stream
用于Buffer转流
- sdk
multer
用于处理文件数据。它将读取文件数据作为缓冲区。
- 代码
const {
BlobServiceClient,
StorageSharedKeyCredential,
newPipeline,
} = require("@azure/storage-blob");
//define multer
const multer = require("multer");
const inMemoryStorage = multer.memoryStorage();
const uploadStrategy = multer({ storage: inMemoryStorage }).array("images");
// configure storage
const accountNmae = "jimtestdiag924";
const accountKey =
"";
const sharedKeyCredential = new StorageSharedKeyCredential(
accountNmae,
accountKey,
);
const pipeline = newPipeline(sharedKeyCredential);
const blobServiceClient = new BlobServiceClient(
`https://${accountNmae}.blob.core.windows.net`,
pipeline,
);
const uploadOptions = { bufferSize: 4 * 1024 * 1024, maxConcurrency: 20 };
const getStream = require("into-stream");
const contanierName = "images";
const fileUpload = async (req, res, next) => {
let imageStatus = [];
console.log(req.files);
try {
await req.files.forEach(async (reqfile, i) => {
const blobName = reqfile.originalname;
const stream = getStream(reqfile.buffer);
const containerClient = blobServiceClient.getContainerClient(
contanierName,
);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.uploadStream(
stream,
uploadOptions.bufferSize,
uploadOptions.maxConcurrency,
{ blobHTTPHeaders: { blobContentType: reqfile.mimetype } },
);
});
res.status(200).json({ imageStatus });
res.render("success", { message: "File uploaded to Azure Blob storage." });
} catch (err) {
throw err;
}
};
module.exports = fileUpload;
详情请参考here。
我正在尝试将多图像上传到 azure blob 服务,一切看起来都很好,尽管我遗漏了一些东西 我得到的错误
events.js:292 throw er; // Unhandled 'error' event ^
Error: ENOENT: no such file or directory, open 'logo192.png' Emitted 'error' event on ReadStream instance at: at internal/fs/streams.js:163:14 at FSReqCallback.oncomplete (fs.js:159:23) { errno: -2, code: 'ENOENT', syscall: 'open', path: 'logo192.png' }
const uuidv1 = require("uuid/v1");
var azure = require("azure-storage");
var fs = require("fs");
const containerName = "image";
const fileUpload = async (req, res, next) => {
let imageStatus = [];
console.log(req.files);
await req.files.forEach((reqfile, i) => {
const blobName = uuidv1() + "-" + reqfile.originalname;
const blobSvc = azure.createBlobService();
const file = reqfile.originalname;
const stream = fs.createReadStream(file).pipe(blobSvc.createWriteStreamToBlockBlob(containerName, blobName, { blockIdPrefix: "block" }));
let dataLength = 0;
stream
.on("data", function (chunk) {
dataLength += chunk.length;
})
.on("end", function () {
console.log("The length was:", dataLength);
});
blobSvc.createBlockBlobFromStream(containerName, blobName, stream, dataLength, function (error, result, response) {
if (!error) {
console.log("ok Blob uploaded");
imageStatus.push({
imageName: result.name,
imagePath: result.name,
});
} else {
console.log(error);
}
});
});
res.status(200).json({ imageStatus });
};
module.exports = fileUpload;
req.files
的控制台输出[
{
fieldname: 'files',
originalname: 'logo192.png',
encoding: '7bit',
mimetype: 'image/png',
buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 c0 00 00 00 c0 08 03 00 00 00 65 02 9c 35 00 00 00 87 50 4c 54 45 00 00 00 64 da fb 61 da fc ... 5297 more bytes>,
size: 5347
}
]
如果您想在节点应用程序中将图像上传到 Azure blob,我建议您使用新的 SDK @azure/storage-blob
。 SDK 是 azure-storage
是 Legacy。
例如
- SDK
npm install into-stream @azure/storage-blob multer
PS:
- sdk
into-stream
用于Buffer转流 - sdk
multer
用于处理文件数据。它将读取文件数据作为缓冲区。
- 代码
const {
BlobServiceClient,
StorageSharedKeyCredential,
newPipeline,
} = require("@azure/storage-blob");
//define multer
const multer = require("multer");
const inMemoryStorage = multer.memoryStorage();
const uploadStrategy = multer({ storage: inMemoryStorage }).array("images");
// configure storage
const accountNmae = "jimtestdiag924";
const accountKey =
"";
const sharedKeyCredential = new StorageSharedKeyCredential(
accountNmae,
accountKey,
);
const pipeline = newPipeline(sharedKeyCredential);
const blobServiceClient = new BlobServiceClient(
`https://${accountNmae}.blob.core.windows.net`,
pipeline,
);
const uploadOptions = { bufferSize: 4 * 1024 * 1024, maxConcurrency: 20 };
const getStream = require("into-stream");
const contanierName = "images";
const fileUpload = async (req, res, next) => {
let imageStatus = [];
console.log(req.files);
try {
await req.files.forEach(async (reqfile, i) => {
const blobName = reqfile.originalname;
const stream = getStream(reqfile.buffer);
const containerClient = blobServiceClient.getContainerClient(
contanierName,
);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.uploadStream(
stream,
uploadOptions.bufferSize,
uploadOptions.maxConcurrency,
{ blobHTTPHeaders: { blobContentType: reqfile.mimetype } },
);
});
res.status(200).json({ imageStatus });
res.render("success", { message: "File uploaded to Azure Blob storage." });
} catch (err) {
throw err;
}
};
module.exports = fileUpload;
详情请参考here。