C# 404 Azure存储客户端库v2的共享访问签名错误
我正在尝试使用WindowsAzure存储客户端库的v2.0为blob生成共享访问签名(并使用它)。 我一开始是,但它是针对v1.7的,将其转换为2.0会产生404错误 这是我的实际代码: 服务器端,要生成SAS:C# 404 Azure存储客户端库v2的共享访问签名错误,c#,azure,azure-storage,azure-storage-blobs,C#,Azure,Azure Storage,Azure Storage Blobs,我正在尝试使用WindowsAzure存储客户端库的v2.0为blob生成共享访问签名(并使用它)。 我一开始是,但它是针对v1.7的,将其转换为2.0会产生404错误 这是我的实际代码: 服务器端,要生成SAS: var myAccount = CloudStorageAccount.Parse( ConfigurationManager.ConnectionStrings[ "AzureStora
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):
我试着换了
container.GetBlobReferenceFromServer(blobWithSharedAccessSignature);
blobClient = new CloudBlobClient(new Uri(blobWithSharedAccessSignature));
借
我也试着换了
container.GetBlobReferenceFromServer(blobWithSharedAccessSignature);
blobClient = new CloudBlobClient(new Uri(blobWithSharedAccessSignature));
借
这将导致“403-禁止”错误
有人能帮我提供一个完整的v2样本吗?或者告诉我是我的错?谢谢
更新-此处的解决方案:(感谢Sandrino Di Mattia)
初始化
CloudBlobClient
时,需要传递2个参数
- baseUri:不带SAS的Blob url,
http://test.blob.core.windows.net/temp/Imports/2012-12-01_20-56-52.zip
- 凭证:这应该是没有url的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,谢谢!但我完全不理解其中的逻辑:为什么CloudBlobClient不能从一个Uri中找到凭证和基本Uri?真奇怪!它只有两行代码(请参阅我的最终解决方案编辑)。顺便说一句,非常感谢你的回答!这样做可能是为了将身份验证与实际的Blob服务实现分离。结果是,您的代码可以与SAS请求和“完全访问”请求(帐户名+密钥)相同,只需提供不同的StorageCredentials
。我不确定您是否可以帮助我,请您看看这个问题并在那里回答我。在我的平台(Android)中我没有得到类似于GetBlobReferenceFromServer的方法。那样的话我该怎么办?我需要你的帮助。。。
// 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);
}
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);
}