Create a sub folder in a container Azure C programmatically

Viewed 10863

At the moment I have a number of containers. Foreach container I want to add an empty folder from a different storage account just containing the folders name. I then want to populate it with the necessary data.

I'm not sure if there is an property that can create a folder within a container.

Here I have two containers one from my sourceAccount and the other to my targetAccount. I'm sending data from my sourceAccout to my tagetAccount. In my target account within my container dayBlob I want to create the sub folder.

In this section of code I'm getting all the containers. When I get these containers I get the name of each of them. I want to add sub folders in my target container with the names that I get in my foreach

        foreach (var items in containers)
        {
            var containerName = items.Name;

        }

My code is as follows

static CloudStorageAccount sourceAccount = new CloudStorageAccount(new StorageCredentials("name", "key"), true);
static CloudStorageAccount targertAccount = new CloudStorageAccount(new StorageCredentials("name", "key"), true);

        static void Main(string[] args)
    {
                  DateTime dateToday = DateTime.Today;

        DateTime date = new DateTime();
        DateTime dateutc = TimeZoneInfo.ConvertTimeToUtc(date);
        TimeSpan startDay = new TimeSpan(00, 00, 00);
        TimeSpan endDay = new TimeSpan(23, 59, 59);
        var sourceClient = sourceAccount.CreateCloudBlobClient();
        var targetClient = targetAccount.CreateCloudBlobClient();
        var testContainer = sourceClient.GetContainerReference("test");
        var sourceContainer = sourceClient.GetContainerReference("downloads");
        var itDropBoxContainer = sourceClient.GetContainerReference("it-dropbox");
        var dayBlob = targetClient.GetContainerReference($"day{dateToday.Day}");

        date = DateTime.Parse($"{dateToday.Day}/{dateToday.Month}/{dateToday.Year}");
        var start = date + startDay;
        var end = date + endDay;
        IEnumerable<CloudBlobContainer> containers = sourceClient.ListContainers();

        foreach (var items in containers)
        {
            var containerName = items.Name;

        }

            foreach (IListBlobItem item in testContainer.ListBlobs(useFlatBlobListing: true))
            {

                var blob = item as CloudBlockBlob;
                var modificationDate = blob.Properties.LastModified;
                // to set the modfication date as local time
                var britishZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
                var lastModified = TimeZoneInfo.ConvertTime((DateTimeOffset)modificationDate, britishZone);
                if (lastModified > start && lastModified < end)
                {
                    try
                    {

                        if (blob != null)
                        {
                            CloudBlockBlob sourceBlob = testContainer.GetBlockBlobReference(blob.Name);
                            CloudBlockBlob targetBlob = dayBlob.GetBlockBlobReference(blob.Name);

                            Console.WriteLine($"Successfully created a snapshot of blob {blob.Name}");
                        }
                    }
                    catch (Exception ex)
                    {

                        ExceptionHandler.LogError(ex, "Failed to copy to the target folder");
                    }
                }

                else
                {
                    Console.WriteLine($"Failed to create a snapshot of blob {blob.Name}");

                }

            }
    }
1 Answers
Related