【问题标题】:Download all the files from Azure blob storage , zip it and upload the zip file in JAVA从 Azure blob 存储下载所有文件,对其进行压缩并以 JAVA 格式上传 zip 文件
【发布时间】:2021-09-28 18:18:30
【问题描述】:

我想从 Azure blob 存储下载所有文件,从这些文件中创建一个 zip 文件,然后将 zip 文件上传回 blob 存储。 由于文件大小可能非常大,我不想最大化内存。 此操作还需要非常快。

JAVA SDK - azure-storage-blob 12.8.0

编辑:到目前为止编写的代码。不知道如何进一步并行上传 pipedinputstream 数据。

 String zipFileName = formFileName(exportRequest, requestId);
        final PipedOutputStream pipedOutputStream = new PipedOutputStream();
        final PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);

  AzureObjectStoreService objectStoreService =managedObjectStoreUtils.getObjectStoreService();

            if (filesToZip.size() > 0) {
                System.out.println("Files to zip "+ filesToZip.size());
                CompletableFuture<Boolean> zipCreationFuture = CompletableFuture.runAsync(() -> {
                    LoggerHelper.logInfo(logger, "Inside createZIP file async function");
                    ZipOutputStream zipOutputStream = new ZipOutputStream(pipedOutputStream);
                    try {
                        for (String fileName : filesToZip) {
                            try {
                                BlobClient blobClient = objectStoreService.getBlobContainerClient().getBlobClient(fileName);
                                LoggerHelper.logInfo(logger, "Adding zipEntry for file : " + fileName);
                                final ZipEntry zipEntry = new ZipEntry(fileName);
                                zipOutputStream.putNextEntry(zipEntry);
                                byte[] buffer;
                                ByteArrayOutputStream output = new ByteArrayOutputStream();
                                buffer= output.toByteArray();
                                blobClient.getBlockBlobClient().download(output);
                                int len;
                                while ((len = buffer.length) > 0) {
                                    zipOutputStream.write(buffer, 0, len);
                                }
                                zipOutputStream.closeEntry();
                            } catch (SdkClientException e) {
                                LoggerHelper.logExceptionWithMessage(logger, this.getClass().getName(), (Exception) e);
                                LoggerHelper.logError(logger, "Failed while getting s3 object");
                            }
                        }
                        zipOutputStream.finish();
                    } catch (IOException ex) {
                        LoggerHelper.logExceptionWithMessage(logger, this.getClass().getName(), (Exception) ex);
                        LoggerHelper.logError(logger, "Creating zip file failed");
                    } finally {
                        try {
                            zipOutputStream.close();
                            } catch (IOException e) {
                            LoggerHelper.logExceptionWithMessage(logger, this.getClass().getName(), (Exception) e);
                            LoggerHelper.logError(logger, "Failed to close the zip output stream");
                        }
                    }
                    LoggerHelper.logInfo(logger, "Completed createZIP file async function");
        //            return true;
                }).handle((o, exception) -> {
                    LoggerHelper.logExceptionWithMessage(logger, this.getClass().getName(), (Exception) exception);
                    LoggerHelper.logError(logger, "Creating zip file failed");
                    return null;
                });

【问题讨论】:

  • 欢迎来到 Stack Overflow。请编辑您的问题并包含您目前编写的代码以及您遇到的问题。
  • @GauravMantri 已编辑问题。我正在使用 pipedinputstream 并希望并行上传这些数据,但不确定哪些 azure blob 函数支持这一点。如果没有,请建议是否有其他方法可以实现这一目标。
  • @GauravMantri。有什么我可以检查/尝试的吗?

标签: java azure zip azure-blob-storage


【解决方案1】:

能够做到这一点。如果有人有更好的方法,请告诉我。

CompletableFuture.runAsync(() -> {
                  
                    BlobClient blobClient = objectStoreService.getBlobContainerClient().getBlobClient(zipFileName);
                    BlobOutputStream blobOutputStream = blobClient.getBlockBlobClient().getBlobOutputStream();

                     try {
                            int nextData= pipedInputStream.read();
                            while (nextData!=-1) {
                                blobOutputStream.write(nextData);
                                nextData = pipedInputStream.read();
                            }blobOutputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 2020-12-16
    • 2021-08-21
    • 2021-11-19
    • 2015-08-22
    • 2018-01-09
    • 2021-08-11
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多