如何在Blob存储中创建文件夹

时间:2018-12-18 03:26:55

标签: c# .net azure azure-functions azure-storage-blobs

我有一个文件,例如Parent.zip,解压缩后会产生以下文件:child1.jpgchild2.txtchild3.pdf

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

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已创建。

我正在使用它在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);
}

1 个答案:

答案 0 :(得分:5)

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

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

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

相关问题