簡體   English   中英

如何在 Java 中將 AppendBlob/大於 4mb 限制的文件上傳到 Azure 存儲/Blob?

[英]How to upload AppendBlob/a file larger than the 4mb limit into Azure Storage/Blob in Java?

如何使用適用於 Java 的 Azure Storage Blob 客戶端庫將大型(> 4mb)文件上傳為 AppendBlob?

我已經成功地實現了 BlockBlob 上傳大文件,似乎該庫在內部處理了單個請求的 4mb(?) 限制,並將文件分成多個請求。

然而,該庫似乎無法為 AppendBlob 做同樣的事情,那么如何手動完成這種分塊呢? 基本上我認為這需要將 InputStream 分成更小的批次......

使用 Azure Java SDK 12.14.1

受以下答案的啟發(與在 C# 中執行此操作相關): c-sharp-azure-appendblob-appendblock-adding-a-file-larger-than-the-4mb-limit

...我最終在 Java 中這樣做:

    AppendBlobRequestConditions appendBlobRequestConditions = new AppendBlobRequestConditions()
            .setLeaseId("myLeaseId");
    
    try (InputStream input = new BufferedInputStream(
            new FileInputStream(file));) {
        byte[] buf = new byte[AppendBlobClient.MAX_APPEND_BLOCK_BYTES];
        int bytesRead;
        while ((bytesRead = input.read(buf)) > 0) {
            if (bytesRead != buf.length) {
                byte[] smallerData = new byte[bytesRead];
                smallerData = Arrays.copyOf(buf, bytesRead);
                buf = smallerData;
            }
            try (InputStream byteStream = new ByteArrayInputStream(buf);) {
                appendBlobClient
                        .appendBlockWithResponse(byteStream, bytesRead,
                                null, appendBlobRequestConditions, null,
                                null);
            }
        }
    }

當然,在此之前您需要做很多事情,例如確保 AppendBlob 存在,如果不存在,則在嘗試附加任何數據之前創建它。

暫無
暫無

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

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