3

我正在尝试使用Windows Azure 存储客户端库的 v2.0为 blob(并使用它)生成共享访问签名。我从这个示例开始,但它适用于 v1.7,将其转换为 2.0 会产生 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.GetBlobReferenceFromServer(blobWithSharedAccessSignature);

经过

container.GetBlockBlobReference(blobWithSharedAccessSignature);

我也尝试更换

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

经过

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

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

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

更新 - 解决方案:(感谢 Sandrino Di Mattia)

// 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);
}
4

1 回答 1

7

初始化时,CloudBlobClient您需要传递 2 个参数

  • baseUri:没有 SAS 的 Blob url,http://test.blob.core.windows.net/temp/Imports/2012-12-01_20-56-52.zip
  • 凭据:这应该是您的 SAS 没有 url,?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);
}

额外提示:您不需要获取对容器的引用。您可以通过调用GetBlobReferenceFromServer.CloudBlobClient

于 2012-12-01T21:02:46.253 回答