尝试从 .net 核心 API 将文件上传到 ADLS2 时出现"The request URI is invalid"错误



我正在尝试编写一个api,用于将文件上传到我的ADLS2存储。

在代码段

下面
var credential = new Azure.Identity.DefaultAzureCredential();
var fileSystemClient = new DataLakeFileSystemClient(new Uri("https://mystorageaccount.dfs.core.windows.net"), credential);//_applicationSettings?.ObjectStoreSettings?.Uri
DataLakeFileClient fileClient = fileSystemClient.GetFileClient($@"generallanding{objectPushRequest.File.Name}");
using (Stream fileStream = objectPushRequest.File.OpenReadStream())
{
// Upload the file asynchronously
await fileClient.UploadAsync(fileStream, true);
message = BuildMessage(Status.Success, ErrorCodes.NoError, "Object Push service uploaded file to Blob");
return (message, null);
}

但是当我执行上面的代码时,我得到无效的uri错误

Azure。RequestFailedException:请求URI无效。RequestId: a2504662 - 501 f - 0060 - 0145 - 6 - ccc0c000000Time:2023-04-11T07:17:10.8515000Z Status: 400(请求URI为无效。)错误代码:InvalidUri

Content: {"error"; {"code";InvalidUri";message"无效。 nRequestId: a2504662 - 501 f - 0060 - 0145 - 6 - ccc0c000000 nTime: 2023 - 04 - 11 - t07:17:10.8515000z"}}

但是上面的dfs url是我从Azure门户本身复制的。

所以我不明白我做错了什么!请帮助

查看示例代码片段和错误消息,很明显失败是由于Invalid Request URL

您可以利用下面的示例代码将文件上传到ADLS Gen 2 Storage:

public async Task UploadFile(DataLakeFileSystemClient fileSystemClient)
{
DataLakeDirectoryClient directoryClient =
fileSystemClient.GetDirectoryClient("my-directory");
DataLakeFileClient fileClient = await directoryClient.CreateFileAsync("uploaded-file.txt");
FileStream fileStream =
File.OpenRead("C:\Users\contoso\Temp\file-to-upload.txt");
long fileSize = fileStream.Length;
await fileClient.AppendAsync(fileStream, offset: 0);
await fileClient.FlushAsync(position: fileSize);
}

更多信息在这里。希望对你有帮助。

相关内容

  • 没有找到相关文章

最新更新