Search code examples
c#azureasp.net-coreazure-sdk-.netstorage-file-share

Azure FileShare - FileShareClient.UploadAsync(stream) - Resource Not Found Error


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


Solution

  • 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