0
votes

TARGET: Do the Azure Function tutorial on and copied code, but got several errors when executing locally on VS2017. I appreciate you help.

https://www.cyotek.com/blog/upload-data-to-blob-storage-with-azure-functions

ERROR 1 - related to Run:

CS0116 A namespace cannot directly contain members such as fields or methods UploadToBlobFunctionApp C:\AzureFunctions\UploadToBlobFunctionApp\UploadToBlobFunctionApp\UploadToBlobFunction.cs 15 Active

ERROR 2 - related to Task CreateBlob:

CS0116 A namespace cannot directly contain members such as fields or methods UploadToBlobFunctionApp C:\AzureFunctions\UploadToBlobFunctionApp\UploadToBlobFunctionApp\UploadToBlobFunction.cs 45 Active

ERROR 3 - related to await CreateBlob:

CS0103 The name 'CreateBlob' does not exist in the current context UploadToBlobFunctionApp C:\AzureFunctions\UploadToBlobFunctionApp\UploadToBlobFunctionApp\UploadToBlobFunction.cs 36 Active

CODE Function.cs:

using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
HttpStatusCode result;
string contentType;

result = HttpStatusCode.BadRequest;

contentType = req.Content.Headers?.ContentType?.MediaType;

if (contentType == "application/json")
{
    string body;

    body = await req.Content.ReadAsStringAsync();

    if (!string.IsNullOrEmpty(body))
    {
        string name;

        name = Guid.NewGuid().ToString("n");

        await CreateBlob(name + ".json", body, log);

        result = HttpStatusCode.OK;
    }
}

return req.CreateResponse(result, string.Empty);
}

private async static Task CreateBlob(string name, string data, 
TraceWriter log)
{
string accessKey;
string accountName;
string connectionString;
CloudStorageAccount storageAccount;
CloudBlobClient client;
CloudBlobContainer container;
CloudBlockBlob blob;

accessKey = "qwertyw4VhRajxlZn9C4hTMB8oSwE4klNUsvTy9VeTCIQ11111vFVVGExDwJ+JUboFv2B79j+W6foqLWE92w==";
accountName = "mystorage";
connectionString = "DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accessKey + ";EndpointSuffix=core.windows.net";
storageAccount = CloudStorageAccount.Parse(connectionString);

client = storageAccount.CreateCloudBlobClient();

container = client.GetContainerReference("functionupload");

await container.CreateIfNotExistsAsync();

blob = container.GetBlockBlobReference(name);
blob.Properties.ContentType = "application/json";

using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
{
    await blob.UploadFromStreamAsync(stream);
}
}
1

1 Answers

0
votes

The example that you are referencing is using scripted functions (csx file). They are mostly used while editing code directly in Azure portal.

I think you are trying to create a precompiled application with csproj and cs files. In this case, your code should be a valid C#, i.e. all methods should be inside classes.

Have a look at this example.

You can also use attributes to mark your functions and triggers instead of authoring function.json manually, see examples here.