问题描述
我有一个文件,例如 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 Storage是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 存储中创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!