0

I have a very newbie question.

I am following this docs "Azure Blob storage client library v12 for .NET" - https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet

When I tested on my console, and my Azure storage, it works.

But I was wondering if I can make a controller out of the suggested 'Main' method? Because I want these getting and posting to the server actions initiated when the user input changes from the front end side.

This is what the Main method inside of the Program.cs looks like based on the docs

        static async Task Main()
        {
            Console.WriteLine("Azure Blob storage v12 - .NET quickstart sample\n");

            string connectionString = Environment.GetEnvironmentVariable("My_CONNECTION_STRING");


            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            string containerName = "quickstartblobs" + Guid.NewGuid().ToString();

            BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);


            string localPath = "./data/";
            string fileName = "quickstart" + Guid.NewGuid().ToString() + ".txt";
            string localFilePath = Path.Combine(localPath, fileName);

            // Write text to the file
            await File.WriteAllTextAsync(localFilePath, "Hello, World!");

            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", blobClient.Uri);

            // Open the file and upload its data
            using FileStream uploadFileStream = File.OpenRead(localFilePath);
            await blobClient.UploadAsync(uploadFileStream, true);
            uploadFileStream.Close();

            Console.WriteLine("Listing blobs...");

            // List all blobs in the container
            await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
            {
                Console.WriteLine("\t" + blobItem.Name);
            }
            Console.Write("Press any key to begin clean up");
            Console.ReadLine();

            string downloadFilePath = localFilePath.Replace(".txt", "DOWNLOAD.txt");

            Console.WriteLine("\nDownloading blob to\n\t{0}\n", downloadFilePath);

            // Download the blob's contents and save it to a file
            BlobDownloadInfo download = await blobClient.DownloadAsync();

            using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath))
            {
                await download.Content.CopyToAsync(downloadFileStream);
                downloadFileStream.Close();
            }
        }

So for example, in my HomeController Can I use post related functions as

            [HttpPost]
            public void Post([FromBody]string value)
            {
                //Create a unique name for the container
                string containerName = "filedata" + Guid.NewGuid().ToString();

                // Create the container and return a container client object
                BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);

                // Create a local file in the ./data/ directory for uploading and downloading
                string localPath = "./data/";
                string fileName = "textfile" + Guid.NewGuid().ToString() + ".txt";
                string localFilePath = Path.Combine(localPath, fileName);

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", blobClient.Uri);

            // Open the file and upload its data
            using FileStream uploadFileStream = File.OpenRead(localFilePath);
            await blobClient.UploadAsync(uploadFileStream, true);
            uploadFileStream.Close();


            }

Or is this a no-go?

Thanks for helping this super newbie!

yoonvak
  • 303
  • 1
  • 3
  • 15

1 Answers1

1

So for example, in my HomeController Can I use post related functions Or is this a no-go?

Yes, you can achieve it.

You can use postman to send post request in local to test it. Remember to remove SSL for webserver setting.

Also, change public void Post to public async Task Post and remove using in code:

 FileStream uploadFileStream = File.OpenRead(localFilePath);
 await blobClient.UploadAsync(uploadFileStream, true);
 uploadFileStream.Close()
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Thank you so much! Can I ask you another question regarding this? I created a HomeController, using the ms doc guidance but I get the error where it says I cannot use 'File' under separate method I created (where it includes the uploadFileStream), because it is not valid under given context. - here is the stackoverflow with codes, https://stackoverflow.com/questions/60927331/controllerbase-filebyte-string-is-a-method-which-is-not-valid-in-the-giv – yoonvak Mar 30 '20 at 09:13