Skip to content

Add support for New File Version Chunked Upload #1949

Description

@unofficialbox

Is your feature request related to a problem? Please describe.

Current the SDK supports uploading new file versions and chunked uploads independently

Describe the solution you'd like

Describe alternatives you've considered

Manually construct the the upload session. This adds roughly 40-50 lines of extra code

Additional context

public static FileFull uploadLargeFileVersion(BoxClient client, Path filePath, String fileId)
      throws IOException {
    long fileSize = Files.size(filePath);
    String fileName = filePath.getFileName().toString();
    ChunkedUploadsManager chunkedUploads = client.getChunkedUploads();
    System.out.printf("Uploading %s (%,d bytes) as a new version of %s...%n", fileName, fileSize, fileId);

    UploadSession session =
        chunkedUploads.createFileUploadSessionForExistingFile(
            fileId,
            new CreateFileUploadSessionForExistingFileRequestBody.Builder(fileSize)
                .fileName(fileName)
                .build());
    String uploadPartUrl = session.getSessionEndpoints().getUploadPart();
    Hash fileHash = new Hash(HashName.SHA1);

    try (InputStream stream = Files.newInputStream(filePath)) {
      Iterator<InputStream> chunks = iterateChunks(stream, session.getPartSize(), fileSize);
      PartAccumulator uploaded =
          reduceIterator(
              chunks,
              chunkedUploads::reducer,
              new PartAccumulator(-1, Collections.emptyList(), fileSize, uploadPartUrl, fileHash));

      long registered =
          chunkedUploads
              .getFileUploadSessionPartsByUrl(session.getSessionEndpoints().getListParts())
              .getTotalCount();
      if (registered != session.getTotalParts()) {
        throw new IllegalStateException(
            String.format(
                "Box registered %d of %d parts; refusing to commit.",
                registered, session.getTotalParts()));
      }

      return chunkedUploads
          .createFileUploadSessionCommitByUrl(
              session.getSessionEndpoints().getCommit(),
              new CreateFileUploadSessionCommitByUrlRequestBody(uploaded.getParts()),
              new CreateFileUploadSessionCommitByUrlHeaders("sha=" + fileHash.digestHash("base64")))
          .getEntries()
          .get(0);
    }
  }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementAdded to issues that describes enhancements

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions