当你在使用AWS CloudFront时,预签名URL的错误“无此键”通常是由以下几个原因引起的:
无效的密钥对:确保你使用的是有效的密钥对来生成预签名URL。密钥对由Access Key和Secret Access Key组成,并且必须与你的AWS账户关联。
错误的密钥:检查你在生成预签名URL时使用的密钥是否正确。确保Access Key和Secret Access Key是正确的,并且没有任何额外的空格或其他字符。
非法字符:如果Access Key或Secret Access Key中包含非法字符(如空格、特殊符号等),则可能导致“无此键”错误。确保在生成预签名URL时不包含任何非法字符。
以下是一个使用AWS SDK for Java生成CloudFront预签名URL的示例代码:
import com.amazonaws.services.cloudfront.CloudFrontUrlSigner;
public class CloudFrontUrlGenerator {
public static void main(String[] args) {
String privateKeyPath = "/path/to/private/key.pem";
String cloudFrontDomain = "http://xxxxxxxxxxxx.cloudfront.net";
String resourcePath = "/path/to/resource.jpg";
String keyPairId = "APKAxxxxxxxxxxxxxx";
String signedUrl = CloudFrontUrlSigner.getSignedURLWithCannedPolicy(
cloudFrontDomain + resourcePath,
keyPairId,
privateKeyPath,
null, // Optional: you can specify a token to prevent URL tampering
null); // Optional: you can specify the expiration time for the URL
System.out.println("Signed URL: " + signedUrl);
}
}
确保你已经替换了示例代码中的私钥路径、CloudFront域名、资源路径和密钥ID。此代码将使用指定的私钥和密钥ID生成预签名URL,并打印到控制台。
如果你仍然遇到“无此键”错误,你可以按照以下步骤进行排查:
确保访问密钥对具有适当的权限:检查你的访问密钥对是否具有足够的权限来生成预签名URL。确保密钥对具有CloudFront相关操作的权限。
检查CloudFront配置:确保你的CloudFront配置正确,并且资源路径与预签名URL中的路径匹配。检查你的CloudFront分发配置、Origin配置和缓存行为。
检查密钥对ID:确保密钥对ID与你的CloudFront分发配置中的密钥对ID匹配。密钥对ID应该与你在生成预签名URL时使用的密钥对ID一致。
如果你仍然无法解决问题,建议你查看AWS CloudFront文档或联系AWS支持获取更多帮助。