Azure File Storage Create File 404 Error

1.7k views Asked by At

I have below code which is attempting to create a blank file on my azure file storage account

CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
var fc = sa.CreateCloudFileClient();
var share = fc.GetShareReference("uploadparking");
share.CreateIfNotExists();
var rootDirectory = share.GetRootDirectoryReference();
var subDirectory = rootDirectory.GetDirectoryReference("valuationrequests");
subDirectory.CreateIfNotExists();
var uri = new Uri(subDirectory.Uri.ToString() + "/file.txt");
var file = new CloudFile(uri);
file.Create(0);

On the final line I am getting the following exception:

Microsoft.WindowsAzure.Storage.StorageException' occurred in Microsoft.WindowsAzure.Storage.dll

Additional information: The remote server returned an error: (404) Not Found.

I'm not sure what it can't find. It shouldn't be trying to find the file as it's creating it. I have confirmed the directories exist.

If anyone knows I can go about creating a file successfully please let me know. I've checked the tutorials and they sadly only show how to download a file not upload.

1

There are 1 answers

0
Gaurav Mantri On BEST ANSWER

I believe the documentation is incorrect. The documentation only mentions that the URI should be absolute. It fails to mention that if you're using absolute URI, then you should also pass storage credentials or the URI should include Shared Access Signature with at least Create permission to create a file.

You should try using the following override of CloudFile to create an instance: https://learn.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.file.cloudfile.-ctor?view=azurestorage-8.1.3#Microsoft_WindowsAzure_Storage_File_CloudFile__ctor_System_Uri_Microsoft_WindowsAzure_Storage_Auth_StorageCredentials_.

So your code would be:

CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
var fc = sa.CreateCloudFileClient();
var share = fc.GetShareReference("uploadparking");
share.CreateIfNotExists();
var rootDirectory = share.GetRootDirectoryReference();
var subDirectory = rootDirectory.GetDirectoryReference("valuationrequests");
subDirectory.CreateIfNotExists();
var uri = new Uri(subDirectory.Uri.ToString() + "/file.txt");
var file = new CloudFile(uri, sa.Credentials);
file.Create(0);

Other alternative would be to create a Shared Access Signature (SAS) token on the share and use a SAS URL when creating an instance of CloudFile. So in this case your code would be:

CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
        var fc = account.CreateCloudFileClient();
        var share = fc.GetShareReference("uploadparking");
        share.CreateIfNotExists();
        var rootDirectory = share.GetRootDirectoryReference();
        var subDirectory = rootDirectory.GetDirectoryReference("valuationrequests");
        subDirectory.CreateIfNotExists();
        SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
        {
            Permissions = SharedAccessFilePermissions.Create,
            SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(15)
        };
        var sasToken = share.GetSharedAccessSignature(policy);
        var uri = new Uri(subDirectory.Uri.ToString() + "/file1.txt" + sasToken);
        var file = new CloudFile(uri);
        file.Create(0);