gpt4 book ai didi

java - Amazon Java SDK - 上传到 S3

转载 作者:行者123 更新时间:2023-11-30 02:48:44 24 4
gpt4 key购买 nike

我正在使用 Amazon Java SDK 将文件上传到 Amazon s3

虽然使用工件 aws-java-sdk 版本 1.10.62 - 以下代码完美运行 - 请注意幕后的所有接线都有效

 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;
}

自从我迁移到 aws-java-sdk 版本 1.11.31 后 - 此代码停止工作所有类都保持不变,并且我的 IDE 中没有任何警告

但是 - 我确实看到以下内容记录到我的控制台

 [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=

上传仍在继续,但从进度监听器中 - 事件代码为 8,代表传输失败

有人知道我需要做什么才能让这段代码再次工作吗?

谢谢达米恩

最佳答案

尝试将其更改为:
公共(public)无效progressChanged(ProgressEvent进度事件){
LOG.info(myUpload.getProgress().getPercentTransferred() + "%");
LOG.info("progressEvent.getEventCode():"+ ProgressEvent.getEventType());
if (progressEvent.getEventType() == ProgressEventType.TRANSFER_COMPLETED_EVENT) {
LOG.info("上传完成!!!");
}
}

您似乎正在运行一些已弃用的代码。

com.amazonaws.event.ProgressEventType 中,值 8 指的是 HTTP_REQUEST_COMPLETED_EVENT

  • COMPLETED_EVENT_CODE 已弃用
  • getEventCode 已弃用

请参阅此-> https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/event/ProgressEvent.java

关于java - Amazon Java SDK - 上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39358139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com