[英]Azure storage avoid uploading file if name already exists
我正在尝试将文件上传到 azure 存储。 我使用 package multer-azure
让它工作,但是如果我上传一个与已存储在存储中的文件同名的文件,第一个文件将被替换。
从文档看来,我需要添加一个ETagMatch
,但我不确定 go 应该在哪里。
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,它将被覆盖。 您可以简单地使用doesBlobExist
的 dosBlobExist 方法来检查 NodeJS 中的路径中是否存在 blob。 如果不存在,您可以将文件上传到 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...');
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.