1

I am a java microservices person - not much into .NET.

With the help from StackOverflow community .. I was able to get a working code for my problem.

The code is working perfectly locally ..

But when I tried to move it to Azure as a function . I am getting an error ..

Please guide.

Code working fine locally

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Azure.Storage.Blob;
using System;
using System.IO;
using System.Threading.Tasks;

namespace whizlabblob
{
    class Program
    {
        static string storageconnstring = "DefaultEndpoint*******************************";
        static string containerName = "demo";
        static string filename = "sample.txt";
        static string filepath="C:\\Work\\sample.txt";
        static string downloadpath = "C:\\Work\\sample2.txt";
        static async Task Main(string[] args)
        {
            //Container().Wait();
            //CreateBlob().Wait();
            //GetBlobs().Wait();
            // GetBlob().Wait();
            CopyBlob().Wait();
            Console.WriteLine("Complete");
            Console.ReadKey();
        }

        static async Task CopyBlob()
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);

            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

            BlobClient blobClient = containerClient.GetBlobClient(filename);
            var blobUri = blobClient.Uri;

            BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient("demo-copy");//This is the container where we want to copy the blob
            BlobClient targetBlobClient = targetContainerClient.GetBlobClient(filename);
            await targetBlobClient.StartCopyFromUriAsync(blobUri);
        }

}
}

Error on Azure

Connected!
2020-06-26T13:57:05Z   [Information]   Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=a6869c88-b1ba-4c55-9671-9be26feb66ba)
2020-06-26T13:57:05Z   [Error]   Function compilation error
2020-06-26T13:57:05Z   [Error]   run.csx(8,1): error CS7021: Cannot declare namespace in script code
2020-06-26T13:57:05Z   [Error]   run.csx(1,7): error CS0246: The type or namespace name 'Azure' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:05Z   [Error]   run.csx(2,7): error CS0246: The type or namespace name 'Azure' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:05Z   [Error]   run.csx(3,23): error CS0234: The type or namespace name 'Storage' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)
2020-06-26T13:57:05Z   [Warning]   run.csx(17,27): warning CS7022: The entry point of the program is global script code; ignoring 'Program.Main(string[])' entry point.
2020-06-26T13:57:05Z   [Warning]   run.csx(17,27): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
2020-06-26T13:57:05Z   [Error]   run.csx(27,13): error CS0246: The type or namespace name 'BlobServiceClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(27,55): error CS0246: The type or namespace name 'BlobServiceClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(29,13): error CS0246: The type or namespace name 'BlobContainerClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(31,13): error CS0246: The type or namespace name 'BlobClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(34,13): error CS0246: The type or namespace name 'BlobContainerClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(35,13): error CS0246: The type or namespace name 'BlobClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Warning]   run.csx(15,23): warning CS0414: The field 'Program.filepath' is assigned but its value is never used
2020-06-26T13:57:06Z   [Warning]   run.csx(16,23): warning CS0414: The field 'Program.downloadpath' is assigned but its value is never used
2020-06-26T13:57:06Z   [Error]   Executed 'Functions.HttpTrigger1' (Failed, Id=a6869c88-b1ba-4c55-9671-9be26feb66ba)
ecstasy
  • 161
  • 1
  • 2
  • 9

1 Answers1

3

If you want to use c# as script, you must remove the namespace (as the error describes) and also add a Run method:

//PS: review which package will you use
#r "Microsoft.WindowsAzure.Storage"
#r "Azure.Storage.Blobs"
#r "Microsoft.Azure.Storage.Blob"

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using System;

public static void Run(CloudQueueMessage myQueueItem, ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed: {myQueueItem.AsString}");
    await CopyBlob();
}

static async Task CopyBlob()
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);

            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

            BlobClient blobClient = containerClient.GetBlobClient(filename);
            var blobUri = blobClient.Uri;

            BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient("demo-copy");//This is the container where we want to copy the blob
            BlobClient targetBlobClient = targetContainerClient.GetBlobClient(filename);
            await targetBlobClient.StartCopyFromUriAsync(blobUri);
        }

if you want to keep with your local development experience, you'd better use Azure Functions template in your visual studio. More info:

Azure Functions Visual Studio Tools

Creating a new Azure Functions using Visual Studio

https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-vs

https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp

https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • Thanks Thiago Custodio .. it worked but encountered an issue .. Please follow this -> https://stackoverflow.com/questions/62599809/azure-function-not-working-for-parameterized-api – ecstasy Jun 26 '20 at 17:19
  • one more doubt.. thanks for all the help so far ..https://stackoverflow.com/questions/62601626/regarding-azure-service-principal – ecstasy Jun 26 '20 at 19:32
  • https://stackoverflow.com/questions/62602464/how-to-open-a-container-file-in-browser-azure-c-sharp-function – ecstasy Jun 26 '20 at 20:35