简体   繁体   中英

Uploading a zip folder to cloud storage

Here's the scenario.

User uploads a zip file from a form. On the backend, I get the ZipInputStream and convert the inputstream to bytes and upload to 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. 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

Posting this as Community Wiki answer, based in the comment provided by @Valkyrie, informing what she did to fix it.

The solution is to convert the fileInptStream to a byteArray and then, convert the byteArray to a byteArrayInputStream as below:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM