The UploadFile function

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

{
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJAAAACVCAYAAAC3i3MLAA",
"fileName": "MyFile.png",
"fileType": "image/png",
"fileExt": "png"
}

The UploadFile function parses the JSON in the request body and then calls the UploadBlobAsync function. In this function, we upload the file to the Azure Blob Storage container and we return the URI of the uploaded file.

The UploadFile function's code is as follows:

[FunctionName("UploadFile")]
public static async Task<IActionResult> Upload(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string base64String = data.base64;
string fileName = data.fileName;
string fileType = data.fileType;
string fileExt = data.fileExt;
Uri uri = await UploadBlobAsync(base64String, fileName, fileType,
fileExt);
return fileName != null
? (ActionResult)new OkObjectResult($"File {fileName} stored. URI = {uri}")
: new BadRequestObjectResult("Error on input parameter (object)");
}

UploadBlobAsync is a function that performs the Blob upload to the d365bcfiles container in the Azure Storage account. Its code is as follows:

public static async Task<Uri> UploadBlobAsync(string base64String, string fileName, string fileType, string fileExtension)
{
string contentType = fileType;
byte[] fileBytes = Convert.FromBase64String(base64String);
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(BLOBStorageConnectionString);
CloudBlobClient client = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference("d365bcfiles");
await container.CreateIfNotExistsAsync(
BlobContainerPublicAccessType.Blob,
new BlobRequestOptions(),
new OperationContext());
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
blob.Properties.ContentType = contentType;
using (Stream stream = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
await blob.UploadFromStreamAsync(stream).ConfigureAwait(false);
}
return blob.Uri;
}

..................Content has been hidden....................

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