Blob触发器 - 处理zip文件的问题,其结构类似于folder.zip>文件夹>(170个文件-gif,png,txt等)

问题描述 投票:0回答:1

我有一个天蓝色的功能应用程序,这是一个blob触发器。它会检测blob容器中的新zip文件,并将它们提取到新容器中。它适用于zip文件,其结构类似于file.zip>(170项,如gifs,pngs,txt,html),但它无法处理像file.zip> file>这样的zip文件(170项如gifs,pngs,txt )。我应该使用不同的库还是代码有问题?

using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace IV4.Function
{
    public static class iv4Unzipthis
    {
        [FunctionName("Unziptestiv4")]
        public static async Task Run([BlobTrigger("input-files/{name}", Connection = "unzip_STORAGE")]Stream myBlob, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");

            string destinationStorage = Environment.GetEnvironmentVariable("destinationStorage");
            string destinationContainer = Environment.GetEnvironmentVariable("destinationContainer");

            try{
                if(name.Split('.').Last().ToLower() == "zip"){

                    ZipArchive archive = new ZipArchive(myBlob);

                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(destinationStorage);
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container = blobClient.GetContainerReference(destinationContainer);

                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        log.LogInformation($"Now processing {entry.FullName}");

                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(entry.Name);
                        using (var fileStream = entry.Open())
                        {
                           await blockBlob.UploadFromStreamAsync(fileStream);
                        }
                    }
                }
            }
            catch(Exception ex){
                log.LogInformation($"Error! Something went wrong: {ex.Message}");

            }            
        }
    }
}

我只获取170个文件中的1个文件项,并为文件夹中的其他项引发错误。这是执行的日志流 -

2019-04-01T14:14:54.814 [信息] C#Blob触发函数已处理的blob

名称:Scan_Report.zip

大小:407942字节

2019-04-01T14:14:54.858 [信息]正在处理Scan_Report / index.html

2019-04-01T14:14:54.928 [信息]正在处理Scan_Report /

2019-04-01T14:14:54.928 [信息]错误!出了点问题:参数不能是空字符串。

参数名称:blobName

2019-04-01T14:14:54.929 [信息]执行'Unziptestiv4'(成功,Id = 8fa15f91-928c-4010-93b1-4da9af43bbc3)

c# .net azure visual-studio-code zip
1个回答
0
投票

当使用file.zip>file>(170 items like gifs, pngs, txt)时,会有一个没有文件名的路径,如"file.zip/file/",它会导致错误(其他正确的像file.zip/file/11.txt)。

请尝试以下代码并在我身边正常工作,只需在if (entry.FullName.EndsWith("/")) { continue; }代码中添加foreach(),如下所示:

                    //your other code

                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        // add this line of code.
                        if (entry.FullName.EndsWith("/")) { continue; }

                        log.LogInformation($"Now processing {entry.FullName}");

                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(entry.Name);
                        using (var fileStream = entry.Open())
                        {
                            await blockBlob.UploadFromStreamAsync(fileStream);
                        }
                    }
© www.soinside.com 2019 - 2024. All rights reserved.