本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Java 建立 URL 簽章
除了下列程式碼範例之外,您還可以使用 適用於 Java 的 AWS SDK (第 1 版) 中的CloudFrontUrlSigner
公用程式類別來建立 CloudFront 簽章URLs。
如需更多範例,請參閱 AWS SDK 程式碼範例程式碼庫中的使用 AWS SDK 建立簽章URLs 和 Cookie。
注意
建立簽署 URL 只是透過 CloudFront 提供私有內容程序的一個環節。如需有關整個程序的詳細資訊,請參閱 使用簽章URLs。
下列範例示範如何建立 CloudFront 簽署的 URL。
範例 Java 政策和簽章加密方法
package org.example; import java.time.Instant; import java.time.temporal.ChronoUnit; import software.amazon.awssdk.services.cloudfront.CloudFrontUtilities; import software.amazon.awssdk.services.cloudfront.model.CannedSignerRequest; import software.amazon.awssdk.services.cloudfront.url.SignedUrl; public class Main { public static void main(String[] args) throws Exception { CloudFrontUtilities cloudFrontUtilities = CloudFrontUtilities.create(); Instant expirationDate = Instant.now().plus(7, ChronoUnit.DAYS); String resourceUrl = "http://a1b2c3d4e5f6g7.cloudfront.net"; String keyPairId = "K1UA3WV15I7JSD"; CannedSignerRequest cannedRequest = CannedSignerRequest.builder() .resourceUrl(resourceUrl) .privateKey(new java.io.File("/path/to/private_key.pem").toPath()) .keyPairId(keyPairId) .expirationDate(expirationDate) .build(); SignedUrl signedUrl = cloudFrontUtilities.getSignedUrlWithCannedPolicy(cannedRequest); String url = signedUrl.url(); System.out.println(url); } }
另請參閱: