【问题标题】:Azure FileShare - FileShareClient.UploadAsync(stream) - Resource Not Found ErrorAzure FileShare - FileShareClient.UploadAsync(stream) - 找不到资源错误
【发布时间】:2021-08-22 12:43:47
【问题描述】:

Web API 收到一个 pdf 文档,之后它应该将 pdf 文档存储在 Azure 文件共享(存储)中。但是,当执行 ShareFileClient.UploadAsync(stream) 方法时,会引发以下异常:“ResourceNotFound - 指定的资源不存在”。我正在将 pdf 文档上传到文件共享中,这是一个新文件,因此资源肯定不存在。

知道发生了什么吗?这是什么 ResourceNotFound 问题?该代码在另一个 Azure FileShare 中运行,现在我设置了一个新的存储帐户,从那时起问题就开始了。

预期行为

流应该上传到以下文件夹中的文件共享:uploaded/

实际行为(包括异常或堆栈跟踪)

抛出异常ResourceNotFound - “指定的资源不存在”。

我的代码

       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 对象

destFileCLient {Azure.Storage.Files.Shares.ShareFileClient} Azure.Storage.Files.Shares.ShareFileClient AccountName "unistaddocumentssa" 字符串 CanGenerateSasUri true bool 名称“SC-I60-CAB-ORD-SPE-IT-00015.pdf”字符串 路径“上传/SC-I60-CAB-ORD-SPE-IT-00015.pdf”字符串 ShareName "unistad-files" 字符串 Uri {https://unistaddocumentssa.file.core.windows.net/unistad-files/uploaded/SC-I60-CAB-ORD-SPE-IT-00015.pdf} System.Uri

ex Message = "指定的资源不存在。\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37.6138548Z\r\n状态: 404(指定的资源不存在不存在。)\r\nErrorCode: ResourceNotFound\r\n\r\n内容:\r\nResourceNotFound指定的资源不存在。\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\n时间:2021- 08-22T10:53:37.6138548Z\r\n\r\n标头:\r\n服务器: 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-代码:ResourceNotFound\r\n日期:2021 年 8 月 22 日星期日 10:53:37 GMT\r\n内容长度:223\r\n内容类型:应用程序/xml\r\n" System.Exception {Azure.RequestFailedException} 消息“指定的资源不存在。\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\n时间:2021-08-22T10:53:37.6138548Z\r\n状态:404(指定的资源不存在。)\ r\nErrorCode: ResourceNotFound\r\n\r\n内容:\r\nResourceNotFound指定的资源不存在。\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\n时间:2021-08-22T10:53 :37.6138548Z\r\n\r\n标头:\r\n服务器: 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 \n日期:星期日,2021 年 8 月 22 日 10:53:37 GMT\r\n内容长度:223\r\n内容类型:应用程序/xml\r\n" 字符串

数据 {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} 错误代码“ResourceNotFound”字符串 HResult -2146233088 int 帮助链接空字符串 内部异常 null System.Exception 源“Azure.Storage.Files.Shares”字符串 StackTrace " 在 Azure.Storage.Files.Shares.FileRestClient.d__37.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()\r\n at Azure.Storage.Files.Shares.ShareFileClient.&lt;UploadRangeInternal&gt;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.&lt;UploadInternal&gt;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 在 Azure.Storage .Files.Shares.ShareFileClient.d__103.MoveNext()\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at StorageLibrary.Repositories.FileShare.&lt;SaveFileUploaded&gt;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 在 DocumentUploader.Controllers.DocumentUploaderController.d__7.MoveNext()在 C:\Users\ricar\OneDrive\Documentos\Development\Projects\unista d\Unistad Document Manager Solution\DocumentUploader\Controllers\DocumentUploaderController.cs:line 162" 字符串 状态 404 整数 TargetSite {Void MoveNext()} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo} 静态成员
非公众成员

【问题讨论】:

  • 请检查共享(unistad-files)是否存在于存储帐户中,是否包含目录(已上传)。
  • 是的。文件夹存在。最重要的是,有一个 if 子句,我在其中检查变量“directoryClient”是否不为空。首先,在 if 子句进行检查之后,我得到了对目录的引用。
  • 执行ShareDirectoryClient directoryClient = _shareClient.GetDirectoryClient(directory); 只是创建ShareDirectoryClient 的一个实例。它不会检查目录是否存在。您可能希望将 if (directoryClient != null) 更改为 if (directoryClient.exists()) 以确保目录存在。
  • 代码已审核。我有 ShareDirectoryClient directoryClient = _shareClient.GetDirectoryClient(directory); -> 等待 directoryClient.CreateIfNotExistsAsync(); -> if (await directoryClient.ExistsAsync()) ,目录存在。
  • 调试代码时我的 directoryClient 对象 {Azure.Storage.Files.Shares.ShareDirectoryClient} Azure.Storage.Files.Shares.ShareDirectoryClient / AccountName "unistaddocumentssa" / CanGenerateSasUri true / Name "uploaded" / Path “已上传”/ShareName “unistad-files”/Uri {unistaddocumentssa.file.core.windows.net/unistad-files/uploaded}/System.Uri

标签: c# azure asp.net-core azure-sdk-.net storage-file-share


【解决方案1】:

我应该早点看到这个问题。本质上,在 Azure 文件存储中,您需要先创建一个空文件,然后才能设置文件内容。

请更改以下代码行:

await destFileCLient.UploadAsync(fileStream);

类似:

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

【讨论】:

  • 非常感谢!!!!!!非常感谢 !!!!!!顺便问一下,我在哪里可以找到解释所有这些细节的文档?
  • 不客气。我很高兴看到您的问题得到解决。请参阅 REST API 文档:Create FilePut Range。 HTH。
猜你喜欢
  • 2020-03-18
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2015-11-24
  • 2012-03-06
  • 2018-07-18
相关资源
最近更新 更多