node.js - 在 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
})
} )
解决方案
根据 的README.md
描述MantaCodeDevs/multer-azure-storage
,fileName
可选属性必须是返回自定义文件名作为存储在 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
})
});
推荐阅读
- excel - 如何制作多个IF AND OR函数
- tensorflow - 有没有办法将我所有的 GPU 都设置为不是 XLA,这样我就可以使用多个 GPU 而不是一个 GPU 进行训练?
- c++ - `decltype(auto)` 变量是否有任何实际的用例?
- regex - .htaccess 重写规则杀了我
- azure-cosmosdb - 分区键的基数越高越好吗?
- laravel - laravel 不通过资产()选择文件
- android - 我们可以使用 Apktool 更改 APK 的 targetSdkVersion
- angular - 在 Ionic 4 中处理异步和存储以进行 api 调用
- arrays - Swift:使用 .sorted on optional 时对成员“+”的模糊引用
- python - 将参数传递给 login_redirect_url