Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Azure Create Blob using Function App

Article:
Viewed:  2271 
Posted On:  03/03/2020 18:25:04 
What is Azure Functions? How we can manage Azure Blob using Azure Functions? How to deploy Azure Functions? 

 

In this article, I am going to explain, how we can create a new blob using Azure Function App.

Open Visual Studio => File New Project =>

1.png

Image 1.

2.png

Image 2.

Now Do the below code to create new Blob:

3.png

Image 3.

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
 
namespace AzureFunctionAppCreateBlob
{
    public static class Function1
    {
        [FunctionName("CreateBlobFunction")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function,
                                                                                 "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");
 
            // parse query parameter
            string blobName = req.GetQueryNameValuePairs()
                .FirstOrDefault(q => string.Compare(q.Key, "blobName", true) == 0)
                .Value;
 
            string blobText = req.GetQueryNameValuePairs()
               .FirstOrDefault(q => string.Compare(q.Key, "blobText", true) == 0)
               .Value;
 
            try
            {
                string storageConnectionString =
DefaultEndpointsProtocol=https;AccountName=satestonedemo;AccountKey=keyEndpointSuffix=core.windows.net";
 
                if (CloudStorageAccount.TryParse(storageConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("output");
                    cloudBlobContainer.CreateIfNotExists();
                    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(blobName);
                    cloudBlockBlob.UploadText(blobText);
                    log.Info("Processed successfully, sending success response");
                  
                }
            }
            catch (System.Exception ex)
            {
                log.Info("Error while processing request: " + ex.Message.ToString());
            }
 
            return blobName == null
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name to create a Blob")
                : req.CreateResponse(HttpStatusCode.OK, blobName + " Created Successfull.");
        }
    }
}
 
Now publish your Function App:

4.png

Image 4.

Crate publish Profile.

5.png

Image 5.

6.png

Image 6.

Now Publish.

7.png

Image 7.

Now open Portal and copy function URL with Key:

8.png

Image 8.

Add Blob name and content in Query String.

9.png

Image 9.

Blob has been created now check in Portal.

 

10.png

Image 10.

Blob Created and check content.

11.png

Image 11.

Successful J

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us