首页 > 解决方案 > 在 Azure Blob 存储中上传图像时出现问题

问题描述

我正在尝试将图像上传到 azure blob 存储,我面临的问题是图像正在成功上传,但是 azure 上的图像名称是由 azure 本身随机生成的,我想自己从代码中命名图像

以下是我正在使用的代码

var multer = require('multer')
var MulterAzureStorage = require('multer-azure-storage')
var upload = multer({
storage: new MulterAzureStorage({azureStorageConnectionString:
'DefaultEndpointsProtocol=https;AccountName=mystorageaccount;
AccountKey=mykey;EndpointSuffix=core.windows.net',
containerName: 'photos',
containerSecurity: 'blob',
fileName : ?//how to use this options properties
})
}  )

标签: node.jsazureexpressazure-storagemulter

解决方案


根据 的README.md描述MantaCodeDevs/multer-azure-storagefileName可选属性必须是返回自定义文件名作为存储在 Azure Blob Storage 中的 blob 名称的函数。

在此处输入图像描述

否则当fileName不是函数时,它会使用blobName下面的默认函数来生成一个唯一的名称,以避免命名冲突。

const blobName = (file) => {
    let name = file.fieldname + '-' + uuid.v4() + path.extname(file.originalname)
    file.blobName = name
    return name
}

所以我用下面的示例代码对其进行了测试,它适用于将1.png文件作为 blob 上传到 Azure Blob 存储。

var getFileName = function(file) {
    return '1.png'; 
    // or return file.originalname;
    // or return file.name;
}

var upload = multer({
  storage: new MulterAzureStorage({
    azureStorageConnectionString: 'DefaultEndpointsProtocol=https;AccountName=<your account name>;AccountKey=<your account key>;EndpointSuffix=core.windows.net',
    containerName: 'test',
    containerSecurity: 'blob',
    fileName: getFileName
  })
});

推荐阅读