如何在Java中连接Cloud Front?

问题描述 投票:0回答:1

我想使用Java通过CloudFront在Amazon s3中上传文档。

我已经尝试过this class,但不清楚如何使用它。

有人可以尽快帮助我吗?

java amazon-s3 file-upload amazon-cloudfront cdn
1个回答
0
投票

终于得到答案了。我正在使用CloudFront put方法通过Cloudfront在S3存储桶中上传图像。这是我的代码

URL url;
    try {
        url = new URL("**cloudfront URL***"+imagePath);
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            connection.setRequestProperty("charset","UTF-8");
            connection.setRequestProperty("Content-Length",imageByteArray.length+"");
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
            wr.write(imageByteArray);
            wr.flush();
            wr.close();
            connection.disconnect();
            // Check the HTTP response code. To complete the upload and make the object available, 
            // you must interact with the connection object in some way.
            connection.getResponseCode();
            System.out.println("HTTP response code: " + connection.getResponseCode());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
© www.soinside.com 2019 - 2024. All rights reserved.