Search code examples
javaamazon-s3file-uploadamazon-cloudfrontcdn

How to connect CloudFront in java?


Currently, I am using java API to upload documents to Amazon s3 using the Amazon s3 SDK but I want to leverage the caching facility of CloudFront to speed up the upload download process so can anyone give any pointers that how to upload file/image in s3 using Cloudfront?


Solution

  • Finally, I got the answer. I'm using the CloudFront Http method for upload Image/Document in S3 bucket via CloudFront. Here is my code

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