Skip to content

Instantly share code, notes, and snippets.

@MadhukarMoogala
Created August 6, 2019 10:48
Show Gist options
  • Save MadhukarMoogala/490078e4f63cb19aa8991175515c7f02 to your computer and use it in GitHub Desktop.
Save MadhukarMoogala/490078e4f63cb19aa8991175515c7f02 to your computer and use it in GitHub Desktop.
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using DotNetEnv;
using System;
using System.IO;
using System.Threading.Tasks;

namespace blobdwgTest
{
    class Program
    {
        
        // Get a connection string to our Azure Storage account.You can
        // obtain your connection string from the Azure Portal (click
        // Access Keys under Settings in the Portal Storage account blade)
        // or using the Azure CLI with:
        //
        //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
        //
        // And you can provide the connection string to your application
        // using an environment variable.
        
        static void Main(string[] args)
        {


            Env.Load(@"D:\Work\netCore\blobdwgTest\.env");
            string connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING");

            //NewMethod(connectionString);
            try
            {
                CallUploadAsync(connectionString).Wait();
                CallDownloadAsync(connectionString).Wait();
                //CallDeleteAsync(connectionString).Wait();
            }
            catch (Exception ex)
            {
               Console.WriteLine(ex.Message);
            }
      
        }

        static public async Task CallDeleteAsync(string connectionString)
        {
            // Get a reference to a container named "sample-container" and then create it
            BlobContainerClient container = new BlobContainerClient(connectionString, "zencase4375");
            try
            {
                await container.DeleteAsync();
            }
            catch (StorageRequestFailedException)
            {
                throw;
            }
        }

        static public async Task CallUploadAsync(string connectionString)
        {
            // Get a reference to a container named "sample-container" and then create it
            BlobContainerClient container = new BlobContainerClient(connectionString, "zencase4375");
            try
            {
                await container.CreateAsync();
                // Get a reference to a blob named "sample-file" in a container named "sample-container"
                BlobClient blob = container.GetBlobClient("4375archive");

                // Open a file and upload it's data
                using (FileStream file = File.OpenRead(/*Change Accordingly*/@"D:\Work\netCore\blobdwgTest\files\parent - Standard.zip"))
                {
                    await blob.UploadAsync(file);
                }
                // Verify we uploaded some content
                BlobProperties properties = await blob.GetPropertiesAsync();
                Console.WriteLine($"Content-Type {properties.ContentType} Content-Length {properties.ContentLength}");
            }
            catch (StorageRequestFailedException)
            {
                throw;
            }
           

           
        }

        static public async Task CallDownloadAsync(string connectionString)
        {
            // Get a reference to the public blob at 
            // BlobClient blob = new BlobClient(new Uri("https://azfdastorage.blob.core.windows.net/zencase4375/4375archive"));
            BlobClient blob = new BlobClient(connectionString, "zencase4375", "4375archive", new BlobClientOptions());
            // Download the blob
            BlobDownloadInfo download = await blob.DownloadAsync();
            using (FileStream file = File.OpenWrite(@"D:\Work\netCore\blobdwgTest\files\dwgs.zip"))
            {
                await download.Content.CopyToAsync(file);
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment