1

Web API receives a pdf document, afterwards it should store the pdf document in the Azure File Share (Storage). However when the method ShareFileClient.UploadAsync(stream) is executed, the following exception is raised : "ResourceNotFound - The specified resource does not exist". I am uploading the pdf document into the file share, it's a new file so the resource does not exist for sure.

Any idea what is going on? What is this ResourceNotFound issue? The code was working in another Azure FileShare, now I settle a new storage account, the problem has started since then.

Expected Behavior

The stream should be uploaded to the file share in the following folder : uploaded/

Actual behavior (include Exception or Stack Trace)

The exception ResourceNotFound - " The specified resource does not exist" is thrown.

My Code

       public async Task<bool> SaveFileUploaded(string directory, string file, Stream fileStream)

    {

        // Get a reference to the directory
        ShareDirectoryClient directoryClient = _shareClient.GetDirectoryClient(directory);

        // Check the client exists
        if (directoryClient != null)
        {
            // Get a reference to a file object
            ShareFileClient destFileCLient = directoryClient.GetFileClient(file);

            fileStream.Position = 0;

            // Start the copy operation
            await destFileCLient.UploadAsync(fileStream);
            

            // Ensure that the file was uploaded
            if (await destFileCLient.ExistsAsync())
            {
                return true;
            }
            else
            {
                _log.LogError($"File {file} wasn't saved into the uploaded directory : {directory}.", file, directory);
                return false;
            }
        }
        else
        {
            // Directory wasn't initiated.
            return false;
        }                                   

    }

FileShareClient Object

destFileCLient {Azure.Storage.Files.Shares.ShareFileClient} Azure.Storage.Files.Shares.ShareFileClient AccountName "unistaddocumentssa" string CanGenerateSasUri true bool Name "SC-I60-CAB-ORD-SPE-IT-00015.pdf" string Path "uploaded/SC-I60-CAB-ORD-SPE-IT-00015.pdf" string ShareName "unistad-files" string Uri {https://unistaddocumentssa.file.core.windows.net/unistad-files/uploaded/SC-I60-CAB-ORD-SPE-IT-00015.pdf} System.Uri

ex Message = "The specified resource does not exist.\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37.6138548Z\r\nStatus: 404 (The specified resource does not exist.)\r\nErrorCode: ResourceNotFound\r\n\r\nContent:\r\nResourceNotFoundThe specified resource does not exist.\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37.6138548Z\r\n\r\nHeaders:\r\nServer: Windows-Azure-File/1.0,Microsoft-HTTPAPI/2.0\r\nx-ms-request-id: f3cf9c55-a01a-0055-1d43-9755fc000000\r\nx-ms-client-request-id: 5d8ed1ff-237d-4edf-a12b-89bb8aaaa85a\r\nx-ms-version: 2020-08-04\r\nx-ms-error-code: ResourceNotFound\r\nDate: Sun, 22 Aug 2021 10:53:37 GMT\r\nContent-Length: 223\r\nContent-Type: application/xml\r\n" System.Exception {Azure.RequestFailedException} Message "The specified resource does not exist.\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37.6138548Z\r\nStatus: 404 (The specified resource does not exist.)\r\nErrorCode: ResourceNotFound\r\n\r\nContent:\r\nResourceNotFoundThe specified resource does not exist.\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37.6138548Z\r\n\r\nHeaders:\r\nServer: Windows-Azure-File/1.0,Microsoft-HTTPAPI/2.0\r\nx-ms-request-id: f3cf9c55-a01a-0055-1d43-9755fc000000\r\nx-ms-client-request-id: 5d8ed1ff-237d-4edf-a12b-89bb8aaaa85a\r\nx-ms-version: 2020-08-04\r\nx-ms-error-code: ResourceNotFound\r\nDate: Sun, 22 Aug 2021 10:53:37 GMT\r\nContent-Length: 223\r\nContent-Type: application/xml\r\n" string

Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} ErrorCode "ResourceNotFound" string HResult -2146233088 int HelpLink null string InnerException null System.Exception Source "Azure.Storage.Files.Shares" string StackTrace " at Azure.Storage.Files.Shares.FileRestClient.d__37.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()\r\n at Azure.Storage.Files.Shares.ShareFileClient.<UploadRangeInternal>d__95.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Azure.Storage.Files.Shares.ShareFileClient.<UploadInternal>d__105.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()\r\n at Azure.Storage.Files.Shares.ShareFileClient.d__103.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at StorageLibrary.Repositories.FileShare.<SaveFileUploaded>d__8.MoveNext() in C:\\Users\\ricar\\OneDrive\\Documentos\\Development\\Projects\\unistad\\Unistad Document Manager Solution\\StorageLibrary\\Repositories\\FileShare.cs:line 264\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at DocumentUploader.Controllers.DocumentUploaderController.d__7.MoveNext() in C:\Users\ricar\OneDrive\Documentos\Development\Projects\unistad\Unistad Document Manager Solution\DocumentUploader\Controllers\DocumentUploaderController.cs:line 162" string Status 404 int TargetSite {Void MoveNext()} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo} Static members
Non-Public members

Ricardo
  • 88
  • 1
  • 8
  • 1
    Please check if the share (unistad-files) exists in the storage account and it contains the directory (uploaded). – Gaurav Mantri Aug 22 '21 at 13:00
  • Yes. The folder exists. On top of that, there is an if clause, where I check if the variable "directoryClient" is not null. First, I get a reference to the directory, just after the if clause does the check. – Ricardo Aug 23 '21 at 05:52
  • Doing `ShareDirectoryClient directoryClient = _shareClient.GetDirectoryClient(directory);` simply creates an instance of `ShareDirectoryClient`. It will not check if the directory exists. You probably want to change `if (directoryClient != null)` to `if (directoryClient.exists())` to ensure directory exists. – Gaurav Mantri Aug 23 '21 at 06:10
  • The code was reviewed. I have ShareDirectoryClient directoryClient = _shareClient.GetDirectoryClient(directory); -> await directoryClient.CreateIfNotExistsAsync(); -> if (await directoryClient.ExistsAsync()) , the directory exists. – Ricardo Aug 23 '21 at 12:23
  • My directoryClient object when debugging the code {Azure.Storage.Files.Shares.ShareDirectoryClient} Azure.Storage.Files.Shares.ShareDirectoryClient / AccountName "unistaddocumentssa" / CanGenerateSasUri true / Name "uploaded" / Path "uploaded" / ShareName "unistad-files" / Uri {https://unistaddocumentssa.file.core.windows.net/unistad-files/uploaded} / System.Uri – Ricardo Aug 23 '21 at 12:24
  • I found this list of errors [link](https://learn.microsoft.com/en-us/rest/api/storageservices/common-rest-api-error-codes), but I don't know which resource is not found, there are no reference to the type of resource in my exception. – Ricardo Aug 23 '21 at 12:32
  • Please see my answer below. HTH. – Gaurav Mantri Aug 23 '21 at 12:35
  • By the way, I created a new file share without any special character such as 'myfiles' (the problem persists). – Ricardo Aug 23 '21 at 13:01
  • Can you try my answer below? – Gaurav Mantri Aug 23 '21 at 13:05

1 Answers1

1

I should have seen the issue sooner. Essentially in Azure File Storage, you would need to create an empty file first and then only you can set the file content.

Please change the following line of code:

await destFileCLient.UploadAsync(fileStream);

with something like:

if (!destFileCLient.exists())//Check if the file already exists.
{
    destFileCLient.create(fileStream.Length);//Create an empty file if the file doesn't exist.
}
await destFileCLient.UploadAsync(fileStream);//Upload the content
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • MANY THANKS !!!!!! MANY THANKS !!!!!! By the way, where can I found a documentation explaining all these details? – Ricardo Aug 23 '21 at 13:10
  • You're welcome. I am glad to see that your problem is resolved. Please refer to the REST API documentation: [`Create File`](https://learn.microsoft.com/en-us/rest/api/storageservices/create-file) and [`Put Range`](https://learn.microsoft.com/en-us/rest/api/storageservices/put-range). HTH. – Gaurav Mantri Aug 23 '21 at 13:13