2016-08-15 66 views
1

我無法獲得共享訪問策略以與blob存儲中的虛擬目錄一起使用。它適用於容器。據我所知,虛擬目錄是容器,所以SAS應該工作?使用共享訪問簽名的Azure Blob存儲虛擬目錄

當我嘗試使用SAS我得到這個響應訪問資源的虛擬目錄:

<?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:XXXXXXXX-000X-00XX-XXXX-XXXXXX000000 Time:2016-08-15T13:28:57.6925768Z</Message> 
    <AuthenticationErrorDetail>Signature did not match. String to sign used was r 2016-08-15T13:29:53Z /blob/xxxxxxxxxx/mycontainer 2015-12-11</AuthenticationErrorDetail> 
</Error> 

示例代碼來演示:

public static async Task<string> GetFilePath() 
{ 
    var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=xxxxxxxxxx;AccountKey=xxxxxxxxxx"); 
    var blobClient = storageAccount.CreateCloudBlobClient(); 

    var containerName = "mycontainer/myvd";  // remove /myvd and SAS will work fine 
    var containerReference = blobClient.GetContainerReference(containerName); 

    var blobName = "readme.txt"; 
    var blobReference = await containerReference.GetBlobReferenceFromServerAsync(blobName); 

    var sasConstraints = new SharedAccessBlobPolicy(); 

    sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(1); 
    sasConstraints.Permissions = SharedAccessBlobPermissions.Read; 

    var sasContainerToken = containerReference.GetSharedAccessSignature(sasConstraints); 

    var path = $"{blobClient.BaseUri.ToString()}{containerName}/{blobName}{sasContainerToken}"; 

    return path; 
} 

回答

1

這個錯誤的原因是因爲Shared Access Signatures是隻支持blob容器級別或blob級別。事實上,在Azure Blob Storage中,不存在如Virtual Directory;它只支持2級層次結構:Blob Container & Blob。 A Virtual Directory只是一個適用於文件(blob)名稱的前綴。

在此基礎上,我建議做如下修改代碼:

var containerName = "mycontainer";  // remove /myvd and SAS will work fine 
var containerReference = blobClient.GetContainerReference(containerName); 

var blobName = "myvd/readme.txt"; //Your blob name is actually "myvd/readme.txt" 
var blobReference = await containerReference.GetBlobReferenceFromServerAsync(blobName); 
+0

謝謝你,完美。 – Paul

相關問題