I would like to give SAS uris to other applications to allow them to send files to a blob container. However the code fails when trying to upload a file giving me an exception which states:
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
I'm using aspnetcore and WindowsAzure.Storage version 9.1.0.
var acc = CloudStorageAccount.Parse("mysecrets");
CloudBlobClient client = acc.CreateCloudBlobClient();
var container = client.GetContainerReference("myblobcontainer");
await container.CreateIfNotExistsAsync();
var permissions = container.GetPermissionsAsync().Result;
permissions.SharedAccessPolicies.Add("writepolicy",
new SharedAccessBlobPolicy
{
Permissions =
SharedAccessBlobPermissions.Add |
SharedAccessBlobPermissions.Create |
SharedAccessBlobPermissions.List |
SharedAccessBlobPermissions.Write,
});
await container.SetPermissionsAsync(permissions);
var sas = container.GetSharedAccessSignature(null, "writepolicy");
var accessUri = container.Uri.ToString() + sas;
var cloudBlobContainer = new CloudBlobContainer(new Uri(accessUri));
var blob = cloudBlobContainer.GetBlockBlobReference("myfile.txt");
await blob.UploadFromFileAsync("foo.txt");
I have also tried creating the cloudBlobContainer like this:
var cloudBlobContainer =
new CloudBlobContainer(container.Uri, new StorageCredentials(sas));