问题描述
我正在尝试将文件上传到Azure存储。我可以使用软件包multer-azure
使用它,但是如果我上传的文件与已经存储在存储器中的文件同名,则第一个文件将被替换。
从文档看来,我似乎需要添加ETagMatch
,但是我不确定应该去哪里。
https://azure.github.io/azure-storage-node/global.html#AccessConditions
我的代码:
var upload = multer({
storage: multerazure({
account: STORAGE_ACCOUNT_NAME,//The name of the Azure storage account
key: ACCOUNT_ACCESS_KEY,//A key listed under Access keys in the storage account pane
container: 'demo',//Any cntainer name,it will be created if it doesn't exist
blobPathResolver: (_req,file,callback) => {
let path;
if(_req.body.pathToFile) {
path = `${organization}/${_req.body.pathToFile}/${file.originalname}`
} else {
path = `${organization}/${file.originalname}`;
}
// var blobPath = yourMagicLogic(req,file); //Calculate blobPath in your own way.
callback(null,path);
}
})
}).single('file')
upload(req,res,function (err) {
if (err instanceof multer.MulterError) {
return res.status(500).json(err)
} else if (err) {
return res.status(500).json(err)
}
return res.status(200).send(req.file)
})
解决方法
如果服务上已经存在Blob,它将被覆盖。您可以简单地使用SDK的doesBlobExist
方法来检查NodeJS的路径中是否存在斑点。如果不存在,则可以将文件上传到Azure存储。
var blobService = azure.createBlobService(storageAccount,accessKey);
blobService.doesBlobExist('azureblob','xxx/a.json',function(error,result) {
if (!error) {
if (result.exists) {
console.log('Blob exists...');
} else {
console.log('Blob does not exist...');
}
}
});