首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在blob存储中创建文件夹

问如何在blob存储中创建文件夹
EN

Stack Overflow用户
提问于 2018-12-18 03:26:55
回答 1查看 7.2K关注 0票数 1

我有一个像Parent.zip这样的文件,当解压缩时,它会产生这些文件:child1.jpg、child2.txt、child3.pdf。

当通过下面的函数运行Parent.zip时,文件将正确解压缩到:

代码语言:javascript
复制
some-container/child1.jpg
some-container/child2.txt
some-container/child3.pdf

如何将文件解压缩到它们的父文件夹?所需的结果是:

代码语言:javascript
复制
some-container/Parent/child1.jpg
some-container/Parent/child2.txt
some-container/Parent/child3.pdf

如上面所示,创建了文件夹Parent。

我用它来创建blob中的文件:

代码语言:javascript
复制
            using (var stream = entry.Open ()) {
                //check for file or folder and update the above blob reference with actual content from stream
                if (entry.Length > 0) {
                    await blob.UploadFromStreamAsync (stream);
                }
            }

以下是完整的资料来源:

代码语言:javascript
复制
[FunctionName ("OnUnzipHttpTriggered")]
public static async Task<IActionResult> Run (
    [HttpTrigger (AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log) {
    log.LogInformation ("C# HTTP trigger function processed a request.");

    var requestBody = new StreamReader (req.Body).ReadToEnd ();
    var data = JsonConvert.DeserializeObject<ZipFileMetaData> (requestBody);
    var storageAccount =
        CloudStorageAccount.Parse (Environment.GetEnvironmentVariable ("StorageConnectionString"));
    var blobClient = storageAccount.CreateCloudBlobClient ();
    var container = blobClient.GetContainerReference (data.SourceContainer);
    var blockBlob = container.GetBlockBlobReference (data.FileName);
    var extractcontainer = blockBlob.ServiceClient.GetContainerReference (data.DestinationContainer.ToLower ());
    await extractcontainer.CreateIfNotExistsAsync ();

    var files = new List<string> ();
    // Save blob(zip file) contents to a Memory Stream.
    using (var zipBlobFileStream = new MemoryStream ()) {
        await blockBlob.DownloadToStreamAsync (zipBlobFileStream);
        await zipBlobFileStream.FlushAsync ();
        zipBlobFileStream.Position = 0;
        //use ZipArchive from System.IO.Compression to extract all the files from zip file
        using (var zip = new ZipArchive (zipBlobFileStream)) {
            //Each entry here represents an individual file or a folder
            foreach (var entry in zip.Entries) {
                files.Add (entry.FullName);
                //creating an empty file (blobkBlob) for the actual file with the same name of file
                var blob = extractcontainer.GetBlockBlobReference (entry.FullName);
                using (var stream = entry.Open ()) {
                    //check for file or folder and update the above blob reference with actual content from stream
                    if (entry.Length > 0) {
                        await blob.UploadFromStreamAsync (stream);
                    }
                }

                // TO-DO : Process the file (Blob)
                //process the file here (blob) or you can write another process later
                //to reference each of these files(blobs) on all files got extracted to other container.
            }
        }
    }

    return new OkObjectResult (files);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-18 03:45:15

只需在条目名称之前添加目录名,我们就可以看到自动创建的目录。

代码语言:javascript
复制
var blob = extractcontainer.GetBlockBlobReference ("Parent/"+entry.FullName);

注意,该目录是虚拟的。Blob存储在container/blob结构中,目录实际上是blob名称的前缀,存储服务根据/分隔符为我们显示目录结构。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53825968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档