【问题标题】:Azure File Storage: Create nested directoriesAzure 文件存储:创建嵌套目录
【发布时间】:2019-03-06 16:42:24
【问题描述】:

我的代码是这样的

CloudFileClient client = ...;

client.GetShareReference("fileStorageShare")
    .GetRootDirectoryReference()
    .GetDirectoryReference("one/two/three")
    .Create();

如果目录一或二不存在,则会出现此错误。有没有办法通过一次调用创建这些嵌套目录?

【问题讨论】:

  • 如果我没记错的话,天蓝色 blob 存储中没有嵌套目录,它只是一个平面层次结构,您可以构建一个嵌套目录,以便您可以在资源管理器中看到它仅此而已。
  • @bleh10 你适合 blob 存储,它是文件存储的基础,但文件存储确实有一些目录支持。
  • 哦,我从来不知道!感谢您的信息,可能会派上用场。你的问题有什么新的结果吗?
  • @bleh10 还没有。我怀疑它必须是一个新功能。

标签: c# azure azure-files


【解决方案1】:

这是不可能的。 SDK不支持这种方式,需要一个一个来创建。

一个问题已经提交here

如果您想一一创建,可以使用以下示例代码:

static void NestedDirectoriesTest()
{
   var cred = new StorageCredentials(accountName, accountKey);
   var account = new CloudStorageAccount(cred, true);
   var client = account.CreateCloudFileClient();
   var share = client.GetShareReference("temp2");
   share.CreateIfNotExists();
   var cloudFileDirectory = share.GetRootDirectoryReference();

   //Specify the nested folder
   var nestedFolderStructure = "Folder/SubFolder";
   var delimiter = new char[] { '/' }; 
   var nestedFolderArray = nestedFolderStructure.Split(delimiter);
   for (var i=0; i<nestedFolderArray.Length; i++)
   {
       cloudFileDirectory = cloudFileDirectory.GetDirectoryReference(nestedFolderArray[i]);
       cloudFileDirectory.CreateIfNotExists();
       Console.WriteLine(cloudFileDirectory.Name + " created...");
   }
}

【讨论】:

  • 愚蠢地认为他们似乎忽略了用户对基本功能的要求。
【解决方案2】:

按照Ivan Yang 的建议,我使用 Azure.Storage.Files.Shares(版本=12.2.3.0)调整了我的代码。

这是我的贡献:

readonly string storageConnectionString = "yourConnectionString";
readonly string shareName = "yourShareName";

public string StoreFile(string dirName,string fileName, Stream fileContent)
{
    // Get a reference to a share and then create it
    ShareClient share = new ShareClient(storageConnectionString, shareName);
    share.CreateIfNotExists();

    // Get a reference to a directory and create it
    string[] arrayPath = dirName.Split('/');
    string buildPath = string.Empty;
    var tempoShare = share;
    ShareDirectoryClient directory = null; // share.GetDirectoryClient(dirName);
    // Here's goes the nested directories builder
    for (int i=0; i < arrayPath.Length; i++)
    {
        buildPath += arrayPath[i];
        directory = share.GetDirectoryClient(buildPath);
        directory.CreateIfNotExists();
        buildPath += '/';
    }
     // Get a reference to a file and upload it
    ShareFileClient file = directory.GetFileClient(fileName);
    using (Stream stream = fileContent)
    {
        file.Create(stream.Length);
        file.UploadRange(new HttpRange(0, stream.Length), stream);
    }
    return directory.Path;
}

【讨论】:

    【解决方案3】:

    这里是哈根code的简化版:

    public async Task<ShareFileClient> CreateFileClient(string connection, string shareName, string path)
    {
        var share = new ShareClient(connection, shareName);
        await share.CreateIfNotExistsAsync();
    
        var dir = share.GetRootDirectoryClient();
        var pathChain = path.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
        int dirCount = pathChain.Length - 1;
    
        for (int i = 0; i < dirCount; ++i)
        {
            dir = dir.GetSubdirectoryClient(pathChain[i]);
            await dir.CreateIfNotExistsAsync();
        }
    
        return dir.GetFileClient(pathChain[dirCount]);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 2021-10-10
      • 2012-06-13
      • 2012-06-08
      相关资源
      最近更新 更多