whysoseriousson
whysoseriousson

Reputation: 306

How to know the error / exception when file upload download transfer state is FAILED

I am using transfer manager available in AWS SDK for file upload and download.Upload and download method returns Upload , Download Object respectively. I am using the isDone() method to check if the upload/download is finished.Now isDone() method returns true even when TransferState is FAILED. I need to know the error or exception that has occurred which has caused this failure. How can i do that.

Upvotes: 3

Views: 1510

Answers (2)

rodolk
rodolk

Reputation: 5907

For file upload you can check errors in this way, I'm copying code in the documentation and adding exception handling:

public String uploadFile(S3TransferManager transferManager, String bucketName,
          String key, String filePath) {
    UploadFileRequest uploadFileRequest = UploadFileRequest.builder()
              .putObjectRequest(b -> b.bucket(bucketName).key(key))
              .addTransferListener(LoggingTransferListener.create())
              .source(Paths.get(filePath))
              .build();

    FileUpload fileUpload = transferManager.uploadFile(uploadFileRequest);

    try {
        CompletedFileUpload uploadResult = fileUpload.completionFuture().join();
        System.out.println("Successfully uploaded file");
        return uploadResult.response().eTag();
    } catch(CancellationException e) {
        System.out.println("File upload was cancelled for file");
        e.printStackTrace();
    } catch(CompletionException e) {
        System.out.println("File upload completed with error for file");
        e.printStackTrace();
    }
    return ""; //some error
}

For download you can use the same exception handling. I responded a question here Stackoverflow question

Upvotes: 0

Ersoy
Ersoy

Reputation: 9624

By the definition and documentation;

isDone Returns true if this transfer is finished (i.e. completed successfully, failed, or was canceled). Returns false if otherwise.

What you may use;

waitForCompletion: Waits for this transfer to complete. This is a blocking call; the current thread is suspended until this transfer completes. details here

Maybe this one

waitForException: Waits for this transfer to finish and returns any error that occurred, or returns null if no errors occurred. This is a blocking call; the current thread will be suspended until this transfer either fails or completes successfully. details here

Both are blocking calls but throws exceptions which includes details.

Upvotes: 1

Related Questions