如何解决如果名称已经存在,Azure存储避免上传文件
我正在尝试将文件上传到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...');
}
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。