如何在Blob存储中创建文件夹 [英] how to create folder in blob storage

查看:82
本文介绍了如何在Blob存储中创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,例如 Parent.zip ,解压缩后会生成以下文件: child1.jpg child2.txt child3.pdf .

I have a file such as Parent.zip and when unzipped, it will yield these files: child1.jpg , child2.txt , child3.pdf.

通过以下功能运行 Parent.zip 时,文件已正确解压缩到:

When running Parent.zip through the function below, the files are correctly unzipped to:

some-container/child1.jpg
some-container/child2.txt
some-container/child3.pdf

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

some-container/Parent/child1.jpg
some-container/Parent/child2.txt
some-container/Parent/child3.pdf

您可以在上方看到文件夹 Parent 已创建.

As you can see above the folder Parent was created.

我正在使用它在blob中创建文件:

I am using this to create the files in blob:

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

这是完整的来源:

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

推荐答案

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

Simply add directory name before entry name, and we can see the directory created automatically.

var blob = extractcontainer.GetBlockBlobReference ("Parent/"+entry.FullName);

请注意,该目录是虚拟的.Blob存储采用 container/blob 结构,目录实际上是blob名称的前缀,并且Storage Service根据/分隔符为我们显示目录结构.

Note that the directory is virtual. Blob Storage is in container/blob structure, the directories are actually prefixes of blob names, and Storage service displays the directory structure according to the / separator for us.

这篇关于如何在Blob存储中创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆