简体   繁体   中英

Create a sub folder in a container Azure C programmatically

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}");

                }

            }
    }

As @GauravMantri mentioned, we can't create folder without independently because folder is a virtual entity in blob storage. You don't need to create a folder before using it. For example, we can get the reference of folder1 even if the folder1 is not exist in the container.

var directory = container.GetDirectoryReference("folder1");
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = directory.GetBlockBlobReference("myblob");

If you did want to create a folder, you did need to create a blob within it.

var directory = container.GetDirectoryReference("folder1");
CloudBlockBlob blockBlob = directory.GetBlockBlobReference("dummy.txt");
blockBlob.UploadFromByteArray(new byte[0], 0, 0);

To list all the folders in a container, you could use following code.

var folders = container.ListBlobs().Where(b => b as CloudBlobDirectory != null).ToList();
foreach (var folder in folders)
{
    Console.WriteLine(folder.Uri);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM