title: Unable to upload object to S3 with AES server side encryption
body: <p>I'm trying to upload an object to <code>S3</code> bucket through <code>Java</code> API. However no matter what I do it throws <code>Access Denied</code> exception</p>
  
  <pre><code>private static void serverSideEncryption() throws NoSuchAlgorithmException {
      AmazonS3 S3_CLIENT = AmazonS3ClientBuilder.standard()
                                                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                                                .withRegion(Regions.US_EAST_1)
                                                .build();
      PutObjectRequest putRequest = new PutObjectRequest(BUCKET_NAME, "dfsdf.ss",
              new File("/Users/fsdfs/Desktop/test.jpeg"));
      S3_CLIENT.putObject(putRequest);
      System.out.println("Object uploaded");
  }
  </code></pre>
  
  <p>when I run this I get </p>
  
  <pre><code>Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: AD1105A291B609D8; S3 Extended Request ID: Ls5wbbW2Yd43p75MJGSOjex0KvmgPiqNBupxpCcEvdMRkK4iptNPNCEwyOqokA=), S3 Extended Request ID: Ls5wbbW2Yd43p75MJGSOje70iqNBupxpCcEvdMRkK4iptNPNCEwyOqokA=
      at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1632)
      at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1304)
      at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1058)
      at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:743)
      at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:717)
      at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
      at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
      at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
      at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
      at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4365)
      at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4312)
      at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1755)
      at com.xxx.aws.s3.S3Service.serverSideEncryption(S3Service.java:72)
      at com.xxx.aws.s3.S3Service.main(S3Service.java:58)
  </code></pre>
  
  <p>However if I do the same thing from <code>AWS CLI</code> using </p>
  
  <pre><code>aws s3api put-object --bucket zzz-yyy-xxx --key test/testfdf --server-side-encryption AES256
  </code></pre>
  
  <p>It works perfectly fine. </p>
  
  <p>I also tried the code below</p>
  
  <pre><code>private static void serverSideEncryption() throws NoSuchAlgorithmException {
      KEY_GENERATOR = KeyGenerator.getInstance("AES");
      KEY_GENERATOR.init(256, new SecureRandom());
      SSE_KEY = new SSECustomerKey(KEY_GENERATOR.generateKey());
      AmazonS3 S3_CLIENT = AmazonS3ClientBuilder.standard()
                                                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                                                .withRegion(Regions.US_EAST_1)
                                                .build();
      PutObjectRequest putRequest = new PutObjectRequest(BUCKET_NAME, "dfsdf.ss",
              new File("/Users/xx-xx/Desktop/dfdf.jpeg")).withSSECustomerKey(SSE_KEY);
      S3_CLIENT.putObject(putRequest);
      System.out.println("Object uploaded");
  }
  </code></pre>
  
  <p>My bucket policy is set to AES encryption</p>