繁体   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