-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGetSignedURLWithCustomPolicy.java
More file actions
74 lines (59 loc) · 2.86 KB
/
GetSignedURLWithCustomPolicy.java
File metadata and controls
74 lines (59 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* An example showing the use of the getSignedURLWithCustomPolicy(...) method
* for generating signed CloudFront URLs. The above method uses a custom policy
* defined by the user to generate the eventual URL.
*
* Related:
* http://amzn.to/1ThvsUc
* http://amzn.to/1ThvwDh
*
*/
package com.amazonaws.examples.cloudfront;
import java.io.File;
import java.util.Date;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.cloudfront.CloudFrontUrlSigner;
import com.amazonaws.services.cloudfront.CloudFrontUrlSigner.Protocol;
import com.amazonaws.util.DateUtils;
public class GetSignedURLWithCustomPolicy {
public static void main(String[] args) {
// Build the parameters for generation of the signed URL
Protocol protocol = Protocol.https;
String distributionDomain = "d260vhn3o2h8ng.cloudfront.net";
String s3ObjectKey = "images/Chrysanthemum.jpg";
// Each of the AWS accounts that you use to create CloudFront signed
// URLs or signed cookies—your trusted signers—must have its own
// CloudFront key pair, and the key pair must be active.
// More about CF certs and keys at: http://amzn.to/1ThvsUc
File privateKeyFile = new File("c:\\keys\\pk-APKAI46CMRRTBEJHMIRA.pem");
String keyPairId = "APKAI46CMRRTBEJHMIRA";
Date dateLessThan = DateUtils.parseISO8601Date("2015-12-05T23:59:59.000Z");
Date dateGreaterThan = DateUtils.parseISO8601Date("2015-12-01T00:00:00.000Z");
String ipRange = "0.0.0.0/0";
try {
//
String urlString = CloudFrontUrlSigner.getSignedURLWithCustomPolicy(
protocol, distributionDomain, privateKeyFile, s3ObjectKey,
keyPairId, dateLessThan, dateGreaterThan, ipRange);
System.out.println("Signed URL:\n");
System.out.println(urlString);
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which means your request made it " +
"to Amazon SQS, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means the client encountered " +
"a serious internal problem while trying to communicate with SQS, such as not " +
"being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
} catch (Exception ex) {
System.out.println("The following exception was raised: ");
System.out.println(ex);
}
}
}