Reputation: 1556
I have made a request to server via post method.
It's working when file is on local
Here's the request working
var options = {
'method': 'POST',
'url':'https://api.powerbi.com/v1.0/myorg/groups/xxxxx/imports?datasetDisplayName=test',
'headers': {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${tokenResponse.accessToken} `
},
formData: {
'': {
'value': fs.createReadStream('/Users/userName/Downloads/file.pbix'),
'options': {
'filename': 'file.pbix',
'contentType': null
}
}
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
Now I would change it to get file from Azure storage
const containerClient = blobServiceClient.getContainerClient('tenants');
const baseUrl = containerClient.url
const blobClient = containerClient.getBlobClient('file/Url/On/AzureBlob')
let blobData
try {
blobData = await blobClient.download(0)
console.log(blobData)
} catch (error) {
console.log(error)
}
var options = {
'method': 'POST',
'url':'https://api.powerbi.com/v1.0/myorg/groups/xxxxx/imports?datasetDisplayName=test',
'headers': {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${tokenResponse.accessToken} `
},
formData: {
'': {
'value': fs.createReadStream('blobData.blobDownloadStream'),
'options': {
'filename': 'file.pbix',
'contentType': null
}
}
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
Here I get an error message from the server ( MS Server )
'{"error":{"code":"UnknownError","pbi.error":{"code":"UnknownError","parameters":{},"details":[],"exceptionCulprit":1}}}'
I guess that the error came from the way I download file and sent it to the server.
Is there a way to get the same file type/format as fs.createReadStream
for AzureBlobClient ?
Upvotes: 1
Views: 142
Reputation: 12153
You should specify a file path for fs.createReadStream(some path), but in your second scenario, you provided a string that not a valid file path.
So basically,if you want to use fs.createReadStream
in your code, you should download the file to local as a temp file and then upload it. Try code below :
const fileName = "blob Name"
const containerClient = blobServiceClient.getContainerClient('tenants')
const blobClient = containerClient.getBlobClient(fileName)
const tempFilePath = "/some local temp path/" + fileName
blobClient.downloadToFile(tempFilePath).then(function(){
console.log("file downloaded")
var options = {
'method': 'POST',
'url':'https://api.powerbi.com/v1.0/myorg/groups/xxxxx/imports?datasetDisplayName=test',
'headers': {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${tokenResponse.accessToken} `
},
formData: {
'': {
'value': fs.createReadStream(tempFilePath),
'options': {
'filename': 'file.pbix',
'contentType': null
}
}
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
//remove temp file after upload
fs.unlinkSync(tempFilePath)
})
Upvotes: 1