I want to use Azure Blob Storage to save some files.
For each file I have a submission date, which I also want to use to create a Container. So I can organise the files by submission date.
I can access the Blob Storage and create the Container.
But I cannot set permissions on the Container, test if a particular Blob exists within it, or add a Blob to the Container.
What am I missing?
The innerException I'm getting in every case is:
{Microsoft.WindowsAzure.Storage.StorageException: This request is not authorized to perform this operation.
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.<ExecuteAsyncInternal>d__4`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.<>c__DisplayClass64_0.<<SetPermissionsAsync>b__0>d.MoveNext()
Request Information
RequestID:XXXXXX
RequestDate:XXXXXX
StatusMessage:This request is not authorized to perform this operation.
ErrorCode:AuthorizationFailure
ErrorMessage:This request is not authorized to perform this operation.
RequestId:XXXXXX
Time:XXXXXX
}
Obviously an AuthorizationFailure as it says. But I can access the Blob Storage and create a Container. So what gives?
Here's the complete .Net Core 2 test program (minus a little redaction)
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
namespace AzureStorageTestApp
{
public class Program
{
public static void Main(string[] args)
{
var storageAccount = GetStorageAccount();
var policy = new SharedAccessAccountPolicy()
{
Permissions = SharedAccessAccountPermissions.Read | SharedAccessAccountPermissions.Write | SharedAccessAccountPermissions.List,
Services = SharedAccessAccountServices.Blob | SharedAccessAccountServices.File,
ResourceTypes = SharedAccessAccountResourceTypes.Service | SharedAccessAccountResourceTypes.Container,
SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
Protocols = SharedAccessProtocol.HttpsOnly
};
var sasToken = storageAccount.GetSharedAccessSignature(policy);
var creds = new StorageCredentials(sasToken);
var accountWithSAS = new CloudStorageAccount(creds, storageAccount.Credentials.AccountName, null, true);
var blobClient = accountWithSAS.CreateCloudBlobClient();
var containerName = DateTime.UtcNow.ToString("yyyy-MM-dd");
var container = blobClient.GetContainerReference(containerName);
if (container.CreateIfNotExistsAsync().Result)
{
var perms = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
// Blows up here
container.SetPermissionsAsync(perms).Wait();
}
var fileName = "testfile.txt";
var fileData = "some file data";
var blob = container.GetBlockBlobReference(fileName);
bool shouldUpload = true;
// Blows up here
var blobExists = blob.ExistsAsync().Result;
if (blobExists)
{
blob.FetchAttributesAsync().Wait();
if (blob.Properties.Length == fileData.Length)
{
shouldUpload = false;
}
}
if (shouldUpload)
{
// Blows up here
blob.UploadTextAsync(fileData).Wait();
}
}
private static CloudStorageAccount GetStorageAccount()
{
var connectionString = "DefaultEndpointsProtocol=https;AccountName=<Redacted>;AccountKey=<Redacted>;EndpointSuffix=core.windows.net";
return CloudStorageAccount.Parse(connectionString);
}
}
}