Skip to content

io.tus.java.client.ProtocolException: unexpected status code (400) while uploading chunk #29

@yonc

Description

@yonc

I can't find the root cause of this problem. The upload almost completed at 096.31% and it raises the above exception.

    private static final String ENDPOINT = "https://api.cloudflare.com/client/v4/zones/73eee5a625f77d66180a52dd72/media";
    try {
      File file = getFile(uploadRequest);
    log.info("file Size {}",file.length());
    if(Validator.isNull(file)){
      log.info("FILE NOT FOUND");
      return;
    }
      // When Java's HTTP client follows a redirect for a POST request, it will change the
      // method from POST to GET which can be disabled using following system property.
      // If you do not enable strict redirects, the tus-java-client will not follow any
      // redirects but still work correctly.
      Map<String,String> headers = new HashMap<>();
      headers.put("X-Auth-Email","");
      headers.put("X-Auth-Key","");
      // Create a new TusClient instance
      final TusClient client = new TusClient();
      client.setHeaders(headers);


      // Configure tus HTTP endpoint. This URL will be used for creating new uploads
      // using the Creation extension
      client.setUploadCreationURL(new URL(ENDPOINT));

      // Enable resumable uploads by storing the upload URL in memory
      client.enableResuming(new TusURLMemoryStore());

      // Open a file using which we will then create a TusUpload. If you do not have
      // a File object, you can manually construct a TusUpload using an InputStream.
      // See the documentation for more information.
      final TusUpload upload = new TusUpload(file);

      // You can also upload from an InputStream directly using a bit more work:
      // InputStream stream = …;
      // TusUpload upload = new TusUpload();
      // upload.setInputStream(stream);
      // upload.setSize(sizeOfStream);
      // upload.setFingerprint("stream");


      System.out.println("Starting upload...");

      // We wrap our uploading code in the TusExecutor class which will automatically catch
      // exceptions and issue retries with small delays between them and take fully
      // advantage of tus' resumability to offer more reliability.
      // This step is optional but highly recommended.
      TusExecutor executor = new TusExecutor() {
        @Override
        protected void makeAttempt() throws ProtocolException, IOException {
          // First try to resume an upload. If that's not possible we will create a new
          // upload and get a TusUploader in return. This class is responsible for opening
          // a connection to the remote server and doing the uploading.
          TusUploader uploader = client.resumeOrCreateUpload(upload);
          // Upload the file in chunks of 1KB sizes.
          uploader.setChunkSize(1024);

          // Upload the file as long as data is available. Once the
          // file has been fully uploaded the method will return -1
          do {
            // Calculate the progress using the total size of the uploading file and
            // the current offset.
            long totalBytes = upload.getSize();
            long bytesUploaded = uploader.getOffset();
            double progress = (double) bytesUploaded / totalBytes * 100;

            System.out.printf("Upload at %06.2f%%.\n", progress);
          } while(uploader.uploadChunk() > -1);

          // Allow the HTTP connection to be closed and cleaned up
          uploader.finish();

          System.out.println("Upload finished.");
          System.out.format("Upload available at: %s", uploader.getUploadURL().toString());
        }
      };
      executor.makeAttempts();
    } catch(Exception e) {
      e.printStackTrace();
    }

log.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions