簡體   English   中英

從 azure blob 存儲文件生成一個 Zip 文件

[英]generate a Zip file from azure blob storage files

我的 Windows azure blob 存儲中存儲了一些文件。 我想獲取這些文件並創建一個 zip 文件並存儲到一個新文件夾中。然后返回 zip 文件的路徑。 設置對 zip 文件位置的權限,以便我的用戶可以通過單擊鏈接將 zip 文件下載到他們的本地計算機

 https://mystorage.blob.core.windows.net/myfiles/2b5f8ea6-3dc2-4b77-abfe-4da832e02556/AppList/isjirleq/mydocs1.doc
 https://mystorage.blob.core.windows.net/myfiles/2b5f8ea6-3dc2-4b77-abfe-4da832e02556/tempo/xyz/mymusic.mp3
 https://mystorage.blob.core.windows.net/myfiles/2b5f8ea6-3dc2-4b77-abfe-4da832e02556/general/video/myVideo.wmv
 https://mystorage.blob.core.windows.net/myfiles/2b5f8ea6-3dc2-4b77-abfe-4da832e02556/photo/photo1.png

我希望能夠遍歷這些文件並將它們全部壓縮在一起以創建一個新的 zip 文件

( https://mystorage.blob.core.windows.net/myzippedfiles/allmyFiles.zip ) 並返回 zip 文件的路徑

我的天藍色 blob 中有大量文件。 所以下載它們並壓縮它們並上傳不是一個好主意。

我該怎么做。我需要一些示例代碼來做到這一點

通過使用 blob 流將文件直接壓縮到輸出流,我們已經(部分地)解決了這個問題。 這避免了下載壓縮然后發送的問題,並避免了發生這種情況時的延遲(我們使用了 ICSharpZipLib,參考)。 但這仍然意味着通過 Web 服務器路由流:

  public void ZipFilesToResponse(HttpResponseBase response, IEnumerable<Asset> files, string zipFileName)
    {
        using (var zipOutputStream = new ZipOutputStream(response.OutputStream))
        {
            zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
            response.BufferOutput = false;
            response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
            response.ContentType = "application/octet-stream";

            foreach (var file in files)
            {
                var entry = new ZipEntry(file.FilenameSlug())
                {
                    DateTime = DateTime.Now,
                    Size = file.Filesize
                };
                zipOutputStream.PutNextEntry(entry);
                storageService.ReadToStream(file, zipOutputStream);
                response.Flush();
                if (!response.IsClientConnected)
                {
                   break;
                }
            }
            zipOutputStream.Finish();
            zipOutputStream.Close();
        }
        response.End();
    }

存儲服務只是這樣做:

public void ReadToStream(IFileIdentifier file, Stream stream, StorageType storageType = StorageType.Stored, ITenant overrideTenant = null)
    {
        var reference = GetBlobReference(file, storageType, overrideTenant);
        reference.DownloadToStream(stream);
    }
private CloudBlockBlob GetBlobReference(IFileIdentifier file, StorageType storageType = StorageType.Stored, ITenant overrideTenant = null)
        {
            var filepath = GetFilePath(file, storageType);
            var container = GetTenantContainer(overrideTenant);
            return container.GetBlockBlobReference(filepath);
        }

由於 blob 存儲“只是”一個對象存儲,您需要將它們下載到某個地方(可以是網絡/工作者角色或您的本地計算機),將它們壓縮,然后重新上傳 zip 文件。 據我所知,這是唯一的方法。

我認為您無法避免下載它們,在本地壓縮它們,然后將它們上傳回來。

壓縮實用程序僅使用本地資源。 Azure 存儲本身沒有能夠自行壓縮某些文件的概念/能力

我 99% 確定您使用的任何 zip 庫都需要本地資源/本地文件才能創建 zip 文件。

查看用於工作角色的 Azure 本地存儲。

http://vkreynin.wordpress.com/2010/01/10/learning-azure-local-storage-with-me/

您將能夠在您的輔助角色中指定一定數量的本地存儲空間,以保存在處理過程中可訪問的內容。

例如

//在您的配置中創建一個本地存儲部分。

<WebRole name="...">
 <LocalResources>
   <LocalStorage name="myLocalStorage" sizeInMB="50"/>
 </LocalResources>
</WebRole>

//然后將您的文件保存到本地存儲

CloudBlobContainer container = blobClient.GetContainerReference("myfiles");
CloudBlob blob = container.GetBlobReference("2b5f8ea6-3dc2-4b77-abfe-4da832e02556/AppList/isjirleq/mydocs1.doc");

LocalResource myStorage = RoleEnvironment.GetLocalResource("myLocalStorage");
string filePath = Path.Combine(myStorage.RootPath, "mydocs1.doc");
blob.DownloadToFile(filePath);

將所有文件保存在 LocalStorage 中后,使用 ZipLibrary 將所有文件路徑捆綁在一起

我認為您可以使用 webjob 或 worker 角色來做到這一點。 當您收到用戶的請求時,將此請求推送到隊列中,然后將作業 id 返回給用戶。 Webjob 或輔助角色從隊列中獲取請求,下載這些文件並壓縮它們,然后將 zip 文件上傳回存儲 blob。 前端代碼可以使用帶有作業 id 的 ajax 滾動輪詢來獲取完成后的真實下載 url。

我已經完成了使用網站上的 JSZip 從 Azure Blob 存儲下載多個文件

var urls = [
"images/20170420_145140.jpg",
"images/20170503_142841.jpg",
"images/20170503_084035.jpg"];


download() {

    urls.forEach(function (url) {
        JSZipUtils.getBinaryContent(url, function (err, data) {
            if (err) {
                throw err; // or handle the error
            }
            try {
                zip.file(count + ".jpg", data, { binary: true });
                count++;
                if (count == urls.length) {
                    zip.generateAsync({ type: "blob" }).then(function (content) {
                        FileSaver.saveAs(content, zipFilename);
                    });
                }
            } catch (e) {
                console.log("errorrr...k", e)
            }
        });
    });
}

不要認為 azure 為壓縮提供了任何開箱即用的功能。如果您需要避免周轉時間,也許。使用后台工作者角色是一個好主意,它將從某些隊列中選擇您的文件。壓縮它們上傳它們並為您存儲 url 像sql db。我在與 db 通信時做了類似的事情,所以你可以放心,它會工作得非常快,而用戶不會意識到它實際上並沒有發生在前端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM