Using TransferManager we handle TRANSFER_COMPLETED_EVENT to detect when an upload has finished. However if the upload's state is queried after receiving the event, it still says it is in progress.
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.event.ProgressEvent;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.PersistableTransfer;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import com.amazonaws.services.s3.transfer.internal.S3ProgressListener;
import org.junit.Test;
import java.io.File;
public class S3AccessServiceTest {
@Test
public void test() throws InterruptedException {
// File having some content to upload
File file = new File("/tmp/somefile"); //SET YOUR FILE
String accessKey = ""; //SET YOUR ACCESS KEY
String secretKey = ""; //SET YOUR SECRET KEY
String bucket = "mybucket"; //SET YOUR BUCKET
final TransferManager transferManager = new TransferManager(new BasicAWSCredentials(accessKey, secretKey));
class MyListener implements S3ProgressListener {
private Upload upload;
public synchronized Upload getUpload() {
return upload;
}
public synchronized void setUpload(Upload upload) {
this.upload = upload;
}
public void onPersistableTransfer(PersistableTransfer persistableTransfer) {}
public void progressChanged(ProgressEvent progressEvent) {
System.out.println(progressEvent);
Upload upload = getUpload();
if (upload != null) {
System.out.println(upload.getState());
} else {
System.out.println("No upload yet");
}
}
}
MyListener listener = new MyListener();
Upload upload = transferManager.upload(new PutObjectRequest(bucket, "some_key", file), listener);
listener.setUpload(upload);
upload.waitForUploadResult();
}
}
TRANSFER_STARTED_EVENT, bytes: 0
InProgress
REQUEST_CONTENT_LENGTH_EVENT, bytes: 97082
InProgress
CLIENT_REQUEST_STARTED_EVENT, bytes: 0
InProgress
HTTP_REQUEST_STARTED_EVENT, bytes: 0
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 8192
InProgress
HTTP_REQUEST_COMPLETED_EVENT, bytes: 0
InProgress
HTTP_RESPONSE_STARTED_EVENT, bytes: 0
InProgress
HTTP_RESPONSE_COMPLETED_EVENT, bytes: 0
InProgress
CLIENT_REQUEST_SUCCESS_EVENT, bytes: 0
InProgress
REQUEST_BYTE_TRANSFER_EVENT, bytes: 6970
InProgress
TRANSFER_COMPLETED_EVENT, bytes: 0
InProgress
Using TransferManager we handle TRANSFER_COMPLETED_EVENT to detect when an upload has finished. However if the upload's state is queried after receiving the event, it still says it is in progress.
The following code reproduces the issue:
This is the ouput: