【问题标题】:Problem in Uploading Image in Azure Blob Storage在 Azure Blob 存储中上传图像时出现问题
【发布时间】:2019-05-26 07:16:16
【问题描述】:

我正在尝试将图像上传到 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
})
}  )

【问题讨论】:

  • 似乎这是一个关于 multer 的问题,而不是关于 Azure 存储的问题。 Azure 存储不会生成随机 blob 名称;当您不指定文件名时,看起来这是 multer 的默认行为。也许看看 multer DiskStorage 文档,它似乎涵盖了它。

标签: node.js azure express azure-storage multer


【解决方案1】:

根据MantaCodeDevs/multer-azure-storageREADME.md 描述,fileName 可选属性必须是返回自定义文件名作为存储到 Azure Blob 存储中的 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
  })
});

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2015-12-10
    • 2018-12-17
    • 1970-01-01
    • 2019-05-26
    • 2016-10-11
    相关资源
    最近更新 更多