-1

How to write java code for egnyte chunked upload and send to rest service of egnyte api.

https://developers.egnyte.com/docs/read/File_System_Management_API_Documentation#Chunked-Upload

long size = f.getTotalSpace();
        int sizeOfFiles = 1024 * 1024;// 1MB
        byte[] buffer = new byte[sizeOfFiles];
        ResponseEntity<String> responseEntity = null;
        String fileName = f.getName();
        String url = DOWNLOAD_OR_UPLOAD + "-chunked" + egnyteSourcePath + f.getName();
        HttpHeaders headers = buildEgnyteEntity();
        HttpEntity entity = new HttpEntity<>(headers);
        //try-with-resources to ensure closing stream
        try (FileInputStream fis = new FileInputStream(f);
             BufferedInputStream bis = new BufferedInputStream(fis)) {

            int bytesAmount = 0;
            while ((bytesAmount = bis.read(buffer)) > 0) {
                //write each chunk of data into separate file with different number in name
                String filePartName = String.format("%s.%03d", fileName, partCounter++);
                File newFile = new File(f.getParent(), filePartName);
                responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

            }
        }
        return responseEntity;
Udhayam P
  • 11
  • 4

1 Answers1

0

I think there's a couple of things missing in your code.

First thing is that you don't specify required headers. You should provide X-Egnyte-Chunk-Num with int value with number of your chunk, starting from 1. In X-Egnyte-Chunk-Sha512-Checksum header you should provide SHA512 checksum.

Second thing is that first request will give you an UploadId in response header in X-Egnyte-Upload-Id. You need to specify that as a header in your second and following requests.

Third thing is that I don't see you use your bytesAmount in the request. I'm not sure you're providing the data.

I'm not a Java guy, more of a C# one, but I've written a post how to upload and download big files with Egnyte API on my blog: http://www.michalbialecki.com/2018/02/11/sending-receiving-big-files-using-egnyte-api-nuget-package/. This can give you an idea how sending loop can be structured.

Hope that helps.

Mik
  • 3,998
  • 2
  • 26
  • 16