[英]Generate Presigned Url for Azure Blob throws Null pointer exception
我想生成一個預先簽名的 URL /SAS,以便任何擁有該 URL 的人都可以訪問我的文件。
SDK版本:12.8.0
我寫了以下代碼:
BlockBlobClient blockBlobClient = objectStoreService.getBlobContainerClient().getBlobClient(fileName).getBlockBlobClient();
String blobUrl = Utility.urlDecode(blockBlobClient.getBlobUrl());
BlobSasPermission blobPermission = new BlobSasPermission()
.setReadPermission(true);
BlobServiceSasSignatureValues blobServiceSasSignatureValues = new BlobServiceSasSignatureValues(
OffsetDateTime.now(ZoneOffset.UTC).plusSeconds(1000 * 60 * 60),
blobPermission
);
blobUrl = blobUrl + "?" + blockBlobClient.generateSas(blobServiceSasSignatureValues);
但代碼拋出錯誤為
java.lang.NullPointerException:參數不能為 null 或空字符串。 參數名稱:storageSharedKeyCredentials。
編輯1:嘗試過:
UserDelegationKey userDelegationKey = blobServiceClient.getUserDelegationKey(keyStart,keyExpiry);
String blobUrl = Utility.urlDecode(blobClient.getBlobUrl());
blobUrl = blobUrl + "?" + blobClient.generateUserDelegationSas(blobServiceSasSignatureValues,userDelegationKey);
但現在得到:
“狀態代碼 403,” AuthenticationFailed
服務器未能驗證請求。 確保 Authorization 標頭的值包含簽名正確形成。\\nRequestId:7b0f0d75-d01e-00a8-4d84-7c9aa3000000\\nTime:2021-07-19T09:54:31.1312999Z指定的簽名資源級別不允許用於此簽名資源級別"",
假設您有一個具有有效權限的 SAS 令牌、blob 容器 URL 和 blob 的名稱,您可以使用以下簡單邏輯為 blob 生成 SAS URL:
{blob-container-url}/{blob-name}?{sas-token}
如果要使用SDK,首先您將創建一個BlobContainerClient
使用的blob容器URL和SAS令牌,然后創建一個BlockBlobClient
使用一滴容器的客戶端:
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.endpoint("{blob-container-url}?{sas-token}")
.buildClient();
BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient("{blob-name}").getBlockBlobClient();
然后,您可以使用此blockBlobClient
對您的 blob 執行操作。 您可以使用getUrl()
方法來獲取 blob 的 URL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.