异步HTTP使用presignedURL上传到S3(带进度指示器)

时间:2017-09-09 17:55:44

标签: amazon-web-services amazon-s3 aws-sdk apache-httpclient-4.x asynchttpclient

以下代码提供了将zip存档上传到AWS s3预先签名的URL的功能。我使用HttpURLConnection进行上传,但它似乎阻止了getResponse。我希望实现一个反馈循环,以便我可以打印上传完成的进度。 我如何才能捕获可以打印到stdout的上传进度?

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.amazonaws.services.s3.AmazonS3;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;




public class App {

  private static String bucketName = "XXXXX"; 
  private static String objectKey  = "XXXX/file.zip";

  public static void main(String[] args) throws Exception  {

    URL url = getPresignedUrl();
    uploadToUrlHTTP(url,new File("/XXX/XXXX/file.zip"));

  }



  public static URL getPresignedUrl() {
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
        .withCredentials(new ProfileCredentialsProvider("default")).build();
    try {
      System.out.println("Generating pre-signed URL.");
      java.util.Date expiration = new java.util.Date();
      long milliSeconds = expiration.getTime();
      milliSeconds += 1000 * 60 * 60; // Add 1 hour.
      expiration.setTime(milliSeconds);

      GeneratePresignedUrlRequest generatePresignedUrlRequest = 
            new GeneratePresignedUrlRequest(bucketName, objectKey);
      generatePresignedUrlRequest.setMethod(HttpMethod.PUT); 
      generatePresignedUrlRequest.setExpiration(expiration);


      URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); 

      System.out.println("Pre-Signed URL = " + url.toString());

      return url;

    } catch (AmazonServiceException exception) {

      System.out.println("Caught an AmazonServiceException, " +
          "which means your request made it " +
          "to Amazon S3, but was rejected with an error response " +
      "for some reason.");
      System.out.println("Error Message: " + exception.getMessage());
      System.out.println("HTTP  Code: "    + exception.getStatusCode());
      System.out.println("AWS Error Code:" + exception.getErrorCode());
      System.out.println("Error Type:    " + exception.getErrorType());
      System.out.println("Request ID:    " + exception.getRequestId());
      return null;
    } catch (AmazonClientException ace) {

      System.out.println("Caught an AmazonClientException, " +
          "which means the client encountered " +
          "an internal error while trying to communicate" +
          " with S3, " +
      "such as not being able to access the network.");
      System.out.println("Error Message: " + ace.getMessage());
      return null;

    }
  }


  public static void uploadToUrlHTTP(URL url, File file) {

    HttpURLConnection connection;
    try {
        InputStream inputStream = new FileInputStream(file);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");
        OutputStream out =
                connection.getOutputStream();

        byte[] buf = new byte[1024];
        int count;
        int total = 0;
        long fileSize = file.length();

        while ((count =inputStream.read(buf)) != -1)
        {
            if (Thread.interrupted())
            {
                throw new InterruptedException();
            }
            out.write(buf, 0, count);
            total += count;
            int pctComplete = new Double(new Double(total) / new Double(fileSize) * 100).intValue();

            System.out.print("\r");
            System.out.print(String.format("PCT Complete: %d", pctComplete));
        }
        System.out.println();
        out.close();
        inputStream.close();

        System.out.println("Finishing...");
        int responseCode = connection.getResponseCode();

        if (responseCode == 200) {
            System.out.printf("Successfully uploaded.");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

0 个答案:

没有答案