簡體   English   中英

Azure 對 blob 存儲的身份訪問

[英]Azure identity access to blob storage

我在訪問 azure blob 存儲時遇到了權限問題。

我的應用程序位於 Azure 之外,並且將訪問 Azure Blob 存儲以獲取文件。

我已經在 Azure AD 中注冊了該應用程序並擁有一個密鑰。 秘密在 1 年內到期。

我已經設置了環境變量 AZURE_CLIENT_ID、AZURE_CLIENT_SECRET 和 AZURE_TENANT_ID。

    BlobServiceClient storageClient = new BlobServiceClientBuilder()
                                     .endpoint("https://myaccount.blob.core.windows.net")
                                     .credential(newDefaultAzureCredentialBuilder().build())
                                     .buildClient();
     
    BlobContainerClient blobContainerClient =  storageClient.getBlobContainerClient("mycontainer");
    BlobClient blobClient = blobContainerClient.getBlobClient("Sample.pdf");
    File destinationDir = new File("/somedir/");
    File downloadedFile = new File(destinationDir, "Sample.pdf");
    
    blobClient.downloadToFile(downloadedFile.getAbsolutePath(),true);

嘗試下載時,我得到:

   <Code>AuthorizationPermissionMismatch</Code><Message>This request is not authorized to perform 
    this operation using this permission.
    RequestId:f0c2de14-401e-0050-0bfd-6f97ad000000
    Time:2020-08-11T16:34:29.1943093Z</Message></Error>"

我承認我現在很困惑。 我需要先獲得令牌嗎? 我以為我擁有一切,因為示例非常明確,但是四處搜索我看到了有關獲取令牌的引用,而有些則沒有。

我也嘗試使用 SAS 並且我遇到了同樣的問題。 我為我的帳戶設置了存儲 Blob 數據貢獻者。

這是使用 SAS 的連接示例

     BlobServiceClient storageClient = new BlobServiceClientBuilder()                        
               .endpoint("https://mystorageaccount.blob.core.windows.net/?sv=2019- 
               12-12&ss=b&srt=c&sp=rlx&se=2020-08-12T22:37:28Z&st=2020-08- 
                12T14:37:28Z&spr=https&sig=<mysig>")    
                 .buildClient();
         
        BlobContainerClient blobContainerClient =  
                    storageClient.getBlobContainerClient("mycontainer");
        BlobClient blobClient = 
                   blobContainerClient.getBlobClient("Sample.pdf");
        File destinationDir = new File("/mydir");
        File downloadedFile = new File(destinationDir, "Sample.pdf");
        
        blobClient.downloadToFile(downloadedFile.getAbsolutePath(),true);

您應該將Blob 存儲參與者角色分配給與您的 Azure AD 應用關聯的服務主體。

在此處輸入圖像描述

更新:

不知道為什么使用 Azure 身份驗證對您不起作用。

但如果您使用 sasToken,請確保您擁有足夠的權限。

在此處輸入圖像描述

請參考我的代碼:

    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
            .endpoint("https://allen3545635.blob.core.windows.net/")
            .sasToken("sv=2019-12-12&ss=bfqt&srt=sco&sp=rwdlacupx&se=2020-08-13T15:18:15Z&st=2020-08-13T07:18:15Z&spr=https&sig=XXX")
            .buildClient();

記得去掉“?” 在 Azure 門戶上生成的 sasToken 的開頭。

所以所有的代碼都是正確的。 我與 MS Azure 支持人員進行了交談。 我錯過了設置應用程序權限。 我錯誤地設置了我的用戶名權限.. 像往常一樣,一個簡單的修復

所以現在同時使用 secretKey 和 SAS 工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM