目前我有很多容器。 Foreach容器我想从只包含文件夹名称的其他存储帐户添加一个空文件夹。然后我想用必要的数据填充它。
我不确定是否有可以在容器中创建文件夹的属性。
此处我有两个容器,一个来自我的sourceAccount
,另一个来自我的targetAccount
。我将sourceAccout
的数据发送到tagetAccount
。在我的容器dayBlob
中的目标帐户中,我想创建子文件夹。
在这段代码中,我得到了所有容器。当我得到这些容器时,我得到了每个容器的名称。我想在目标容器中添加子文件夹,其中包含我在foreach
foreach (var items in containers)
{
var containerName = items.Name;
}
我的代码如下
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}");
}
}
}
答案 0 :(得分:5)
正如@GauravMantri所提到的,我们无法独立创建文件夹,因为文件夹是blob存储中的虚拟实体。在使用之前,您不需要创建文件夹。例如,即使容器中不存在folder1,我们也可以获得folder1的引用。
var directory = container.GetDirectoryReference("folder1");
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = directory.GetBlockBlobReference("myblob");
如果您确实想要创建文件夹,则需要在其中创建一个blob。
var directory = container.GetDirectoryReference("folder1");
CloudBlockBlob blockBlob = directory.GetBlockBlobReference("dummy.txt");
blockBlob.UploadFromByteArray(new byte[0], 0, 0);
要列出容器中的所有文件夹,您可以使用以下代码。
var folders = container.ListBlobs().Where(b => b as CloudBlobDirectory != null).ToList();
foreach (var folder in folders)
{
Console.WriteLine(folder.Uri);
}