Reputation: 81
I need to create a function to accept an incoming form with a file attachment and upload it to an Azure blob storage container. I have gotten the function to upload to Azure. Initially I was just getting a blank file. But then I used the information in this stackoverflow question to restructure my function. Now I'm getting an object instead of a blank file.
Currently my function looks like this:
async function uploadFile (req, res) {
try {
var formidable = require('formidable');
var fs = require('fs');
const storage = require('@azure/storage-blob')
const { randomBytes } = require('crypto');
const blobNameUniq = Math.random().toString(36).slice(2) + randomBytes(8).toString('hex') + new Date().getTime();
const accountname ="MYACCOUNT_HERE";
const key = "MYKEY_HERE";
const cerds = new storage.StorageSharedKeyCredential(accountname,key);
const blobServiceClient = new storage.BlobServiceClient(`https://${accountname}.blob.core.windows.net`,cerds);
const containerName= blobServiceClient.getContainerClient('dev');
const containerClient = await blobServiceClient.getContainerClient(containerName.containerName);
let form = new formidable.IncomingForm();
form.parse(req, async function (err, fields, files) {
const file = files.file;
const blobName = blobNameUniq + files.file;
const contentType = file.type;
console.log('content type is: ', contentType)
const filePath = file.path;//This is where you get the file path.
console.log('filePath is: ', filePath)
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.uploadFile(filePath);
return uploadBlobResponse
});
}
catch (error) {
console.log(error);
}
}
What I see in Azure (as shown in the link below) is [object Object] at the end of the file name and application/octet-stream as the type. (The file I was testing with is a .png.)
It feels like the problem is that I'm not actually sending just the file to Azure. But I haven't been able to work out what I need to change.
For what it's worth, I have a function successfully connecting to my Azure blob storage to return a SAS for existing blobs. So I feel sure my account and account keys are correct. I'm also getting no error messages back from Azure. Via console.log I'm also able to see that the form parses properly and I can see the proper name, path and mime type.
Any suggestions on what I need to change to make this work?
Upvotes: 1
Views: 886
Reputation: 81
Thank you! Gaurav's change fixed the file name issue.
I also had to go back and add in options on the upload to set the content type as below. But it is working perfectly now.
Last part of the code was changed to:
form.parse(req, async function (err, fields, files) {
const file = files.file;
const blobName = blobNameUniq + files.file.name;
const contentType = file.type;
console.log('content type is: ', contentType)
const filePath = file.path;//This is where you get the file path.
console.log('filePath is: ', filePath)
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
// set mimetype as determined from browser with file upload control
const options = { blobHTTPHeaders: { blobContentType: file.type } };
const uploadBlobResponse = await blockBlobClient.uploadFile(filePath, options);
return uploadBlobResponse
Upvotes: 1
Reputation: 136196
The reason you're getting this is because files.file
is an object.
const blobName = blobNameUniq + files.file
Please change the above line of code to:
const blobName = blobNameUniq + files.file.name
And you should see proper name for your blob.
Upvotes: 0