azure 尝试从.net core API将文件上载到ADLS2时出现“请求URI无效”错误

ngynwnxp  于 2023-04-12  发布在  .NET
关注(0)|答案(1)|浏览(176)

我正试图写一个API上传文件到我的adls 2存储。
下面的代码片段

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($@"general\landing\{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- 6ccc 0 c 000000时间:2023-04- 11 T07:17:10.8515000Z状态:400(请求的URI无效。)ErrorCode:InvalidUri
内容:{“error”:{“code”:“InvalidUri”,“message”:“请求URI无效。\nRequestId:a2504662- 501 f-0060-0145-6ccc0c000000\nTime:2023-04- 11 T07:17:10.8515000Z”}}
但是上面的dfs URL是从Azure门户本身复制的。
所以我不明白我做错了什么!请帮助

gab6jxml

gab6jxml1#

查看示例代码片段和错误消息,很明显失败是由于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);

}

More Info here . Hope this helps.

相关问题