![](/img/trans.png)
[英]Microsoft Azure: How to create sub directory in a blob container
[英]Create a sub folder in a container Azure C programmatically
目前我有很多容器。 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}");
}
}
}
正如@GauravMantri所提到的,我們無法獨立創建文件夾,因為文件夾是blob存儲中的虛擬實體。 使用前無需創建文件夾。 例如,即使文件夾1在容器中不存在,我們也可以獲得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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.