[英]Generate Azure SAS Token for File in Java: Signature fields not well formed
我正在尝试在 Java 中为 Azure Data Lake Gen. 2 存储帐户访问错误,但每次生成的令牌都不正确.
已经讨论了类似的问题,但是使用了旧版本的 Azure SDK 并手动创建了签名。 我正在使用最新版本azure-storage-8.6.3
,但也尝试了8.0.0
和7.0.0
,但均未成功。
这是我的代码:
public class SasTokenExample {
public static String generateSasToken(URI fileUri) throws StorageException, URISyntaxException, InvalidKeyException {
StorageCredentials credentials = new StorageCredentialsAccountAndKey("myaccount", "thisisnottheac+tualkeybutonlyanexampletode+monstratewhatsgoingonhere+12+34567890123456==");
CloudFile file = new CloudFile(fileUri, credentials);
SharedAccessFilePolicy policy = new SharedAccessFilePolicy();
policy.setPermissions(EnumSet.of(SharedAccessFilePermissions.READ));
policy.setSharedAccessStartTime(null);
policy.setSharedAccessExpiryTime(getExpirationTime());
return file.generateSharedAccessSignature(policy, null);
}
private static Date getExpirationTime() {
LocalDateTime expLDT = LocalDateTime.now().plusMinutes(30);
Instant expInstant = expLDT.atZone(ZoneId.systemDefault()).toInstant();
return Date.from(expInstant);
}
public static void main(String[] args) throws URISyntaxException, InvalidKeyException, StorageException, MalformedURLException {
URI uri = new URI("http://myaccount.blob.core.windows.net/mycontainer/my.file");
System.out.println(uri.toString() + "?" + generateSasToken(uri));
}
}
从那,我得到这样的网址:
当我在浏览器中打开链接时,我得到以下结果:
<?xml version="1.0" encoding="utf-8"?>
<Error>
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:3dfc7fb8-701e-0057-2054-25add3000000
Time:2020-05-08T16:19:28.4764866Z</Message>
<AuthenticationErrorDetail>Signature fields not well formed.</AuthenticationErrorDetail>
</Error>
我相信您的问题是您正在使用SharedAccessFilePolicy
为blob
资源(Data Lake Gen2 资源本质上是 blob)生成 SAS URL。 您将需要使用SharedAccessBlobPolicy
。 所以你的代码是:
CloudBlob file = new CloudBlob (fileUri, credentials);
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy ();
policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));
policy.setSharedAccessStartTime(null);
policy.setSharedAccessExpiryTime(getExpirationTime());
return file.generateSharedAccessSignature(policy, null);
试一试,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.