Azure FileShare - FileShareClient.UploadAsync(stream) - 找不到资源错误
Azure FileShare - FileShareClient.UploadAsync(stream) - Resource Not Found Error
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 Object
destFileCLient {Azure.Storage.Files.Shares.ShareFileClient}
Azure.Storage.Files.Shares.ShareFileClient
AccountName "unistaddocumentssa" 字符串
CanGenerateSasUri 真布尔
命名为“SC-I60-CAB-ORD-SPE-IT-00015.pdf”字符串
路径“uploaded/SC-I60-CAB-ORD-SPE-IT-00015.pdf”字符串
ShareName "unistad-files" 字符串
乌里 {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\nStatus: 404(指定的资源不存在.)\r\nErrorCode:ResourceNotFound\r\n\r\nContent:\r\nResourceNotFound
指定资源不存在\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37 .6138548Z\r\n\r\nHeaders:\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\nContent-Length:223\r\nContent-Type:application/xml\r\n" System.Exception {Azure.RequestFailedException}
消息“指定的资源不存在。\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37。6138548Z\r\nStatus: 404(指定的资源不存在。)\r\nErrorCode : ResourceNotFound\r\n\r\nContent:\r\nResourceNotFound
指定的资源不存在。\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37.6138548Z\r\n \r\nHeaders:\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\nContent-Length:223\r\nContent-Type:application/xml\r\n" 字符串
数据{System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
错误代码“ResourceNotFound”字符串
HResult -2146233088 整数
HelpLink 空字符串
InnerException 空 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(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 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.ConfiguredTaskAwaitable
1.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.<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.TaskAwaiter
1.GetResult()\r\n 在 DocumentUploader.Controllers.DocumentUploaderController.d__7.MoveNext() 在 C:\Users\ricar\OneDrive\Documentos\Development\Projects\unistad\Unistad 文档管理器 Solution\DocumentUploader\Controllers\DocumentUploaderController.cs:第 162 行"字符串
状态 404 整数
TargetSite {Void MoveNext()} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
静态成员
Non-Public 成员
我应该早点看到这个问题。基本上在 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
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 Object
destFileCLient {Azure.Storage.Files.Shares.ShareFileClient} Azure.Storage.Files.Shares.ShareFileClient AccountName "unistaddocumentssa" 字符串 CanGenerateSasUri 真布尔 命名为“SC-I60-CAB-ORD-SPE-IT-00015.pdf”字符串 路径“uploaded/SC-I60-CAB-ORD-SPE-IT-00015.pdf”字符串 ShareName "unistad-files" 字符串 乌里 {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\nStatus: 404(指定的资源不存在.)\r\nErrorCode:ResourceNotFound\r\n\r\nContent:\r\nResourceNotFound
指定资源不存在\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37 .6138548Z\r\n\r\nHeaders:\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\nContent-Length:223\r\nContent-Type:application/xml\r\n" System.Exception {Azure.RequestFailedException}
消息“指定的资源不存在。\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37。6138548Z\r\nStatus: 404(指定的资源不存在。)\r\nErrorCode : ResourceNotFound\r\n\r\nContent:\r\nResourceNotFound
指定的资源不存在。\nRequestId:f3cf9c55-a01a-0055-1d43-9755fc000000\nTime:2021-08-22T10:53:37.6138548Z\r\n \r\nHeaders:\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\nContent-Length:223\r\nContent-Type:application/xml\r\n" 字符串
数据{System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
错误代码“ResourceNotFound”字符串
HResult -2146233088 整数
HelpLink 空字符串
InnerException 空 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(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 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.ConfiguredTaskAwaitable
1.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.<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.TaskAwaiter
1.GetResult()\r\n 在 DocumentUploader.Controllers.DocumentUploaderController.d__7.MoveNext() 在 C:\Users\ricar\OneDrive\Documentos\Development\Projects\unistad\Unistad 文档管理器 Solution\DocumentUploader\Controllers\DocumentUploaderController.cs:第 162 行"字符串
状态 404 整数
TargetSite {Void MoveNext()} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
静态成员
Non-Public 成员
我应该早点看到这个问题。基本上在 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