2012-12-01 44 views
3

我想为Windows Azure存储客户端库的一个blob(并使用它)与v2.0生成共享访问签名。 我以this sample开头,但它是v1.7,并将其转换为2.0会产生404错误。共享访问签名与Azure存储客户端库v2的404错误

这里是我的实际代码: 服务器端,生成SAS:

var myAccount = CloudStorageAccount.Parse(
        ConfigurationManager.ConnectionStrings[ 
           "AzureStorageConnectionString"].ConnectionString); 
var myContainer = myAccount.CreateCloudBlobClient() 
              .GetContainerReference(containerName); 
myContainer.CreateIfNotExists(); 

string blobName = "Imports/" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss") 
          + ".zip"; 
var myBlob = myContainer.GetBlockBlobReference(blobName); 
using (var stream = 
new MemoryStream(System.Text.Encoding.UTF8.GetBytes("Hey, I'm empty for now."))) 
{ 
    myBlob.UploadFromStream(stream); 
} 

var sharedAccesSignature = myBlob.GetSharedAccessSignature(
       new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy() 
{ 
    Permissions = 
     Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Write 
     | Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Read, 
     SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1), 
}); 

return myBlob.Uri.AbsoluteUri + sharedAccesSignature; 

它尝试了很多东西在客户端,在404有时会造成或403服务器错误。 对于为例我尝试这样做(结果:404):

var blobClient = new CloudBlobClient(new Uri(blobWithSharedAccessSignature)); 
// blobWithSharedAccessSignature here is : https://azertyuiop.blob.core.windows.net/container1/Imports/2012-12-01_19-43-54.zip?sv=2012-02-12&se=2012-12-01T20%3A43%3A54Z&sr=b&sp=rw&sig=h0bTUk[...]3D 
// calling blobWithSharedAccessSignature from a webBrowser works. 
// result here is valid for containerName : container1 
var container = blobClient.GetContainerReference(containerName); 
ICloudBlob blobRef = container.GetBlobReferenceFromServer(blobWithSharedAccessSignature); ==> error 404 
using (var fileStream = File.OpenRead(fileFullPath)) 
{ 
    blobRef.UploadFromStream(fileStream); 
} 

我试图通过

container.GetBlockBlobReference(blobWithSharedAccessSignature); 

替换

container.GetBlobReferenceFromServer(blobWithSharedAccessSignature); 

我还试图通过替换

blobClient = new CloudBlobClient(new Uri(blobWithSharedAccessSignature)); 
blobClient = new CloudBlobClient(new Uri(blobWithSharedAccessSignature), new StorageCredentials(blobWithSharedAccessSignature)); 

这会导致“403-禁止”错误。

有人可以通过在V2中提供完整样本来帮助我吗?或者告诉我是我的错误?谢谢 !

更新 - 这里的解决方案:(感谢Sandrino迪马蒂亚)

// Assuming that blobWithSharedAccessSignature is : 
// "https://azertyuiop.blob.core.windows.net/container1/Imports/2012-12-01_19-43-54.zip?sv=2012-02-12&se=2012-12-01T20%3A43%3A54Z&sr=b&sp=rw&sig=h0bTUkTR%2FdTF%2BVZgDUuBPHqG%2BiTtFeXK4kepBpDR2AU%3D" 
Uri blobUriWithoutCredentials = new Uri(new Uri(blobWithSharedAccessSignature).GetLeftPart(UriPartial.Path)); 
// here blobUriWithoutCredentials is https://azertyuiop.blob.core.windows.net/container1/Imports/2012-12-01_19-43-54.zip 
string credentials = blobWithSharedAccessSignature.Substring(blobWithSharedAccessSignature.IndexOf('?')); 
// here credentials is "?sv=2012-02-12&se=2012-12-01T22%3A26%3A55Z&sr=b&sp=rw&sig=Lsk8kLyJ8TFoGNVLbFLftCIXUNlIIRPZalkhoPdUfh8%3D" 
var blobClient = new CloudBlobClient(blobUriWithoutCredentials, new StorageCredentials(credentials)); 
ICloudBlob blobRef = blobClient.GetBlobReferenceFromServer(blobUriWithoutCredentials); 
using (var fileStream = File.OpenRead(fileFullPath)) 
{ 
    blobRef.UploadFromStream(fileStream); 
} 
+0

我需要你的帮助在这... http://stackoverflow.com/questions/31847102/upload-file-with-azure-storage-using-sas-shared-access-signature –

回答

7

初始化当CloudBlobClient需要传递2个参数

  • 基本URI:斑点网址没有SAS, http://test.blob.core.windows.net/temp/Imports/2012-12-01_20-56-52.zip
  • 凭证:This应该是你的SAS没有网址,?sv=2012-02-12&se=2012-12-01T21%3A57%3A56Z&sr=b&sp=rw&sig=5JboXXM1Yeo%2BuI6mb18VbURluo%3D

工作样本:

var blobClient = new CloudBlobClient(new Uri(blob.Uri.AbsoluteUri), new StorageCredentials(sharedAccesSignature)); 
using (var fileStream = File.OpenRead(fileFullPath)) 
{ 

    blobClient.GetBlobReferenceFromServer(new Uri(blob.Uri.AbsoluteUri)) 
       .UploadFromStream(fileStream); 
} 

额外提示:你并不需要去的容器的引用。您可以通过CloudBlobClient上的GetBlobReferenceFromServer立即访问该blob。

+0

它的工作原理,谢谢!但是我完全不理解这个逻辑:为什么CloudBlobClient不能从一个URI中找到自己的证书和基础Uri?这很奇怪!这只是两行代码(参见我的编辑和最终解决方案)。 顺便说一句,非常感谢你的回答! – JYL

+1

这可能是为了将认证与实际的Blob服务实现分离开来。结果是,通过简单地提供不同的'StorageCredentials',您的代码可以与SAS请求和“完全访问”请求(帐户名+密钥)相同。 –

+0

谢谢,很有帮助。 –

相关问题