我使用AWS Java SDK创建签名网址并尝试通过链接到私有S3存储桶的云前端提供图像 - 采取的步骤 -
使用下面的代码通过签署.der密钥来创建URL。
{ 字符串distributionDomain =" distributionDomain&#34 ;;
String keyPairId="keyPairId";
String s3ObjectKey=picName;
Date dateLessThan = DateUtils.parseISO8601Date("2014-01-12T21:20:00.000Z");
InputStream inputStream = ImageServiceImpl.class.getResourceAsStream("/cloudFront.der");
byte[] privateKeyBytes=IOUtils.toByteArray(inputStream);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory;
PrivateKey myPrivKey=null;
try {
keyFactory = KeyFactory.getInstance("RSA");
myPrivKey = keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
System.out.println(myPrivKey);
String domainUrl= "https://" + distributionDomain + "/" + s3ObjectKey;
String url1 = CloudFrontUrlSigner.getSignedURLWithCannedPolicy(domainUrl, keyPairId, myPrivKey, dateLessThan);
System.out.println(url1);
}
当我点击获取URL安全签名的URL时,我被拒绝访问,不知道我在这里缺少什么。如果需要任何其他信息,请告诉我。
答案 0 :(得分:0)
我遵循了本指南(https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html),就像您自己一样,我正在使用Java,因此我不得不将CloudFront密钥转换为der
格式(Java可以读取)。我使用以下openssl
命令执行此操作:-
openssl pkcs8 -topk8 -nocrypt -in MyKey.pem -inform PEM -out MyKey.der -outform DER
一旦转换了密钥,就可以运行以下命令:-
public class AwsSignUrlCreator {
public static void main(String[] args) throws InvalidKeySpecException, IOException {
// The DNS name of your CloudFront distribution, or a registered alias
String distributionDomainName = "xxxx.cloudfront.net";
// the private key you created in the AWS Management Console
File cloudFrontPrivateKeyFile = new File ("C:/mykeys/MyKey.der");
// The unique ID assigned to your CloudFront key pair in the console
String cloudFrontKeyPairId = "xxxx";
Date expirationDate = new Date(System.currentTimeMillis() + 60 * 1000);
String s3ObjectKey = "my-file.txt";
String signedUrl = CloudFrontUrlSigner.getSignedURLWithCannedPolicy(
Protocol.https,
distributionDomainName,
cloudFrontPrivateKeyFile,
s3ObjectKey,
cloudFrontKeyPairId,
expirationDate);
System.out.println(signedUrl);
}
}