亚马逊 Java SDK - 上传到 S3

Amazon Java SDK - Upload to S3

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

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

 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 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!!!"); } }

看起来你是 运行 一些已弃用的代码。

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

我更新了我的 S3 库版本,生成了新的访问密钥和一个新的存储桶

现在一切正常