I am using the Amazon Java SDK to upload files to Amazon s3
Whilst using version 1.10.62 of the artifact aws-java-sdk - the following code worked perfectly - Note all the wiring behind the scenes works
public boolean uploadInputStream(String destinationBucketName, InputStream inputStream, Integer numberOfBytes, String destinationFileKey, Boolean isPublic){
try {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(numberOfBytes);
PutObjectRequest putObjectRequest = new PutObjectRequest(destinationBucketName, destinationFileKey, inputStream, metadata);
if (isPublic) {
putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead);
} else {
putObjectRequest.withCannedAcl(CannedAccessControlList.AuthenticatedRead);
}
final Upload myUpload = amazonTransferManager.upload(putObjectRequest);
myUpload.addProgressListener(new ProgressListener() {
// This method is called periodically as your transfer progresses
public void progressChanged(ProgressEvent progressEvent) {
LOG.info(myUpload.getProgress().getPercentTransferred() + "%");
LOG.info("progressEvent.getEventCode():" + progressEvent.getEventCode());
if (progressEvent.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
LOG.info("Upload complete!!!");
}
}
});
long uploadStartTime = System.currentTimeMillis();
long startTimeInMillis = System.currentTimeMillis();
long logGap = 1000 * loggingIntervalInSeconds;
while (!myUpload.isDone()) {
if (System.currentTimeMillis() - startTimeInMillis >= logGap) {
logUploadStatistics(myUpload, Long.valueOf(numberOfBytes));
startTimeInMillis = System.currentTimeMillis();
}
}
long totalUploadDuration = System.currentTimeMillis() - uploadStartTime;
float totalUploadDurationSeconds = Float.valueOf(totalUploadDuration) / 1000;
String uploadedPercentageStr = getFormattedUploadPercentage(myUpload);
boolean isUploadDone = myUpload.isDone();
if (isUploadDone) {
Object[] params = new Object[]{destinationFileKey, totalUploadDuration, totalUploadDurationSeconds};
LOG.info("Successfully uploaded file {} to Amazon. The upload took {} milliseconds ({} seconds)", params);
result = true;
}
LOG.debug("Post put the inputStream to th location {}", destinationFileKey);
} catch (AmazonServiceException e) {
LOG.error("AmazonServiceException:{}", e);
result = false;
} catch (AmazonClientException e) {
LOG.error("AmazonServiceException:{}", e);
result = false;
}
LOG.debug("Exiting uploadInputStream - result:{}", result);
return result;
}
Since I migrated to version 1.11.31 of the aws-java-sdk - this code stopped working All classes remain intact and there were no warnings in my IDE
However - I do see the following logged to my console
[2016-09-06 22:21:58,920] [s3-transfer-manager-worker-1] [DEBUG] com.amazonaws.requestId - x-amzn-RequestId: not available
[2016-09-06 22:21:58,931] [s3-transfer-manager-worker-1] [DEBUG] com.amazonaws.request - Received error response: com.amazonaws.services.s3.model.AmazonS3Exception: Moved Permanently (Service: null; Status Code: 301; Error Code: 301 Moved Permanently; Request ID: D67813C8A11842AE), S3 Extended Request ID: 3CBHeq6fWSzwoLSt3J7D4AUlOaoi1JhfxAfcN1vF8I4tO1aiOAjqB63sac9Oyrq3VZ4x3koEC5I=
The upload still continues but from the progress listener - the event code is 8 which stands for transfer failed
Does anyone have any idea what I need to do to get this chunk of code working again?
Thank you Damien
try changing it to this: public void progressChanged(ProgressEvent progressEvent) { LOG.info(myUpload.getProgress().getPercentTransferred() + "%"); LOG.info("progressEvent.getEventCode():" + progressEvent.getEventType()); if (progressEvent.getEventType() == ProgressEventType.TRANSFER_COMPLETED_EVENT) { LOG.info("Upload complete!!!"); } }
public void progressChanged(ProgressEvent progressEvent) { LOG.info(myUpload.getProgress().getPercentTransferred() + "%"); LOG.info("progressEvent.getEventCode():" + progressEvent.getEventType()); if (progressEvent.getEventType() == ProgressEventType.TRANSFER_COMPLETED_EVENT) { LOG.info("Upload complete!!!"); } }
It looks like you are running some deprecated code.
In com.amazonaws.event.ProgressEventType
, value 8 refers to HTTP_REQUEST_COMPLETED_EVENT
COMPLETED_EVENT_CODE
is deprecated getEventCode
is deprecated refer to this -> https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/event/ProgressEvent.java
I updated my versions of the S3 library, generated new Access Keys and also a new bucket
Now everything works as expected
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.