gpt4 book ai didi

c# - 如何在blob存储中创建文件夹

转载 作者:行者123 更新时间:2023-12-02 23:01:33 25 4
gpt4 key购买 nike

我有一个诸如 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);
}

最佳答案

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

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

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

关于c# - 如何在blob存储中创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53825968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com