The DownloadFile function

The DownloadFile function receives a JSON object in the body via an HTTP POST request, as follows:

{
"url": "https://d365bcfilestorage.blob.core.windows.net/d365bcfiles/MasteringD365BC.png",
"fileType": "image/png",
"fileName": "MasteringD365BC.png"
}

This function retrieves the details of the file to download from the request body and calls the DownloadBlobAsync function. Then, it returns the content of the downloaded file (Base64-encoded string):

[FunctionName("DownloadFile")]
public static async Task<IActionResult> Download(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string url = data.url;
string contentType = data.fileType;
string fileName = data.fileName;
byte[] x = await DownloadBlobAsync(url, fileName);
//Returns the Base64 string of the retrieved file
return (ActionResult)new OkObjectResult($"{Convert.ToBase64String(x)}");
}
catch(Exception ex)
{
log.LogInformation("Bad input request: " + ex.Message);
return new BadRequestObjectResult("Error on input parameter (object): " +
ex.Message);
}
}

DownloadBlobAsync is the function that connects to the Azure Storage Blob container, checks for the file, and (if it is found) returns the byte array (stream) of this file:

public static async Task<byte[]> DownloadBlobAsync(string url, string fileName)
{
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(BLOBStorageConnectionString);
CloudBlobClient client = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference("d365bcfiles");
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
await blob.FetchAttributesAsync();
long fileByteLength = blob.Properties.Length;
byte[] fileContent = new byte[fileByteLength];
for (int i = 0; i < fileByteLength; i++)
{
fileContent[i] = 0x20;
}
await blob.DownloadToByteArrayAsync(fileContent, 0);
return fileContent;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.185.180