0

I'm trying to have an Azure function create a file on the blob storage with a given filename. I'm not getting any errors, but also the file doesn't seem to appear. It doesn't work locally, or when deployed.

I'm not very experienced with setting up the blob storage and its access, what am I missing?

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace My.Functions
{
    public static class UploadGhost
    {
        [FunctionName("UploadGhost")]
        [StorageAccount("AzureWebJobsStorage")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log,
            IBinder binder)
        {
        
            string filename = "ghostsdev/myFile.txt";
            var attribute = new BlobAttribute(filename, FileAccess.Write);

            attribute.Connection = "AzureWebJobsStorage";
            using (var writer = await binder.BindAsync<TextWriter>(attribute))
            {
                writer.Write("myData");
                log.LogInformation("I made a file! or did I... ");
            }

            return new OkObjectResult("Executed without errors!");
        }
    }
}

output:

2021-09-17T17:13:07  Welcome, you are now connected to log-streaming service. The default timeout is 2 hours. Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds). 
2021-09-17T17:13:15.436 [Information] Executing 'UploadGhost' (Reason='This function was programmatically called via the host APIs.', Id=3d956f6a-e9c5-42fc-bf8d-d1ff55810f9c)
2021-09-17T17:13:15.849 [Information] I made a file! or did I...
2021-09-17T17:13:16.030 [Information] Executed 'UploadGhost' (Succeeded, Id=3d956f6a-e9c5-42fc-bf8d-d1ff55810f9c, Duration=615ms)

EDIT: I was unaware there was a storage account associated with the functions by default, and the files were appearing there.

1 Answer 1

1

By making the below changes to the shared code in my local machine visual studio 2019 & adding the "Microsoft.Azure.WebJobs.Extensions.Storage(4.0.5)" dependency we are able to create a blob in the storage account & write the data to that created blob when Http trigger got triggered.

enter image description here

Here is the sample output for reference :

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I was unaware there was a storage account associated with the functions by default, and the files were appearing there. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.