简体   繁体   English

将 zip 文件夹上传到云存储

[英]Uploading a zip folder to cloud storage

Here's the scenario.这是场景。

User uploads a zip file from a form.用户从表单上传 zip 文件。 On the backend, I get the ZipInputStream and convert the inputstream to bytes and upload to GCS在后端,我得到 ZipInputStream 并将输入流转换为字节并上传到 GCS

`public String upload(
      String bucketName,
      String objectName,
      String contentType,
      InputStream objectInputStream)
      throws IOException {
    if (contentType == null) contentType = ContentType.CONTENT_TYPE_TEXT_PLAIN_UTF8;

    BlobId blobId;
    if (largeFile) {
      blobId = BlobId.of(bucketName, objectName);
      BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(contentType).build();
      WriteChannel writer = storage.writer(blobInfo);
      if (storage.get(blobId) != null) writer = storage.update(blobInfo).writer();

      byte[] buffer = new byte[1024];
      int limit;
      while ((limit = objectInputStream.read(buffer)) >= 0) {
        try {
          writer.write(ByteBuffer.wrap(buffer, 0, limit));
        } catch (Exception e) {
          logger.error("Exception uploadObject", e);
        }
      }
      writer.close();
    } else {
      byte[] objectBytes = ByteStreams.toByteArray(objectInputStream);
      blobId = storeByteArray(storage, bucketName, objectName, contentType, objectBytes);
      if (Objects.isNull(blobId)) return null;
    }
    return url(bucketName, objectName);
  }`

COde that gets the filepart and calls the above method获取文件部分并调用上述方法的代码

ZipInputStream filePartInputStream = new ZipInputStream(filePart.getInputStream());
storageGateway.uploadObject(
          "bucket_name",
          "objectname",
          filePart.getContentType(),
          filePartInputStream
       );

The upload works as expected but when I download the zip folder from GCS bucket, it seems to be corrupted.上传按预期工作,但是当我从 GCS 存储桶下载 zip 文件夹时,它似乎已损坏。 I was not able to unzip it.我无法解压缩它。

Am I missing anyhting here?我在这里想念什么吗? If not what's the correct way to upload a zip file to google cloud storage如果不是将 zip 文件上传到谷歌云存储的正确方法是什么

Posting this as Community Wiki answer, based in the comment provided by @Valkyrie, informing what she did to fix it.根据@Valkyrie 提供的评论,将此作为社区 Wiki 答案发布,告知她为修复它所做的工作。

The solution is to convert the fileInptStream to a byteArray and then, convert the byteArray to a byteArrayInputStream as below:解决方案是将fileInptStream转换为byteArray ,然后将byteArray转换为byteArrayInputStream ,如下所示:

byte[] data = IOUtils.toByteArray(filePartInputStream) 
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data)

This way, once the file is downloaded after being uploaded to Cloud Storage, the file should not be corrupted.这样,一旦文件上传到云存储后下载,文件就不会损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Google Cloud Storage显示文件上传进度 - Display file uploading progress with Google Cloud Storage 将小块的 zip 文件上传到 azure 云 blob 存储 - Upload a zip file in small chunks to azure cloud blob storage 将其他元数据作为文件上传请求的一部分上传到Google Cloud Storage - Uploading additional metadata as part of file upload request to Google Cloud Storage 用于将大型文件从多个端点上传到云存储的体系结构 - Architecture for uploading large files from many end points to the cloud storage 在 node.js 中将文件上传到 Google Cloud Storage 时出错 - Error when uploading file to Google Cloud Storage in node.js 上传到Google云端存储后,使用上传表单重定向回主页 - after upload to google cloud storage, redirect back to the main page with the uploading form NextJS 文件上传到谷歌云存储成功但总是上传 0 字节文件 - 使用强大 - NextJS file upload to Google Cloud Storage successful but always uploading 0 byte file - using formidable 上传100%后,Blueimp jquery文件上传插件抛出错误,机架空间云存储 - Blueimp jquery file upload plugin throwing error after uploading 100% is done to rackspace cloud storage 使用NodeJS REST API通过Google云端存储获取文件上传进度 - Get file uploading progress with Google Cloud Storage using NodeJS REST API 文件未上传到快速存储中 - file not uploading in swift storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM