how to create folder in blob storage

有些话、适合烂在心里 提交于 2021-02-08 10:27:18

问题


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

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

How do I unzip the files to their parent folder? The desired result would be:

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

As you can see above the folder Parent was created.

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

Here's the full source:

[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:


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

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

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.



来源:https://stackoverflow.com/questions/53825968/how-to-create-folder-in-blob-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!