0
votes

i am trying to upload an image to azure blob storage, the problem m facing is the image is getting successfully uploaded but the name of the image on azure is randomly generated by the azure itself, i want to name the image myself from the code

following is the code which i am using

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
})
}  )
1
Seems like this is a question about multer, and not about Azure Storage. Azure Storage doesn't generate random blob names; looks like this is the default behavior for multer when you don't specify a filename. Perhaps look at the multer DiskStorage documentation, which seems to cover it.David Makogon

1 Answers

1
votes

According to the README.md description of MantaCodeDevs/multer-azure-storage, the fileName optional property must be a function which return a custom file name as the blob name stored into Azure Blob Storage.

enter image description here

Otherwise when fileName is not a function, it will use the default blobName function below to generate a unique name to avoid naming conflicts.

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

So I test it with my sample code below, it works for uploading a 1.png file as blob into Azure Blob Storage.

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
  })
});