使用 Java 建立URL簽章 - Amazon CloudFront

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 Java 建立URL簽章

除了下列程式碼範例之外,您還可以使用 AWS SDK for Java (第 1 版) 中的CloudFrontUrlSigner公用程式類別來建立CloudFront 已簽署的 URLs

如需更多範例,請參閱AWS SDK程式碼範例程式碼庫 中的使用 建立已簽署URLs和 Cookie AWS SDK

注意

建立已簽署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 = "https://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); } }

另請參閱: