程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了尝试从 Data Lake gen2 读取 blob 时,此请求无权执行此操作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决尝试从 Data Lake gen2 读取 blob 时,此请求无权执行此操作?

开发过程中遇到尝试从 Data Lake gen2 读取 blob 时,此请求无权执行此操作的问题如何解决?下面主要结合日常开发的经验,给出你关于尝试从 Data Lake gen2 读取 blob 时,此请求无权执行此操作的解决方法建议,希望对你解决尝试从 Data Lake gen2 读取 blob 时,此请求无权执行此操作有所启发或帮助;

我想将一个 Blob 从一个 Datalake 复制到另一个,这是我完成这项工作的简单代码:

    DataLakefileSystemClIEnt sourceDataLakefileSystemClIEnt = StorageAccountDataLakeHelper.GetDataLakefileSystemClIEnt(sourceContainer,sourcE_DATALAKE_name,sourcE_DATA_LAKE_ACCESS_KEY);
    DataLakefileSystemClIEnt taregetDataLakefileSystemClIEnt = StorageAccountDataLakeHelper.GetDataLakefileSystemClIEnt(TargetContainer,TARGET_DATALAKE_name,TARTGET_DATA_LAKE_ACCESS_KEY);
    DataLakeDirectoryClIEnt sourcedirectoryClIEnt = sourceDataLakefileSystemClIEnt.GetDirectoryClIEnt("folder1/folder2/");
    DataLakefileClIEnt sourcefileClIEnt = sourcedirectoryClIEnt.GetfileClIEnt("myfile.csv.csv");    
    Stream reader= await sourcefileClIEnt.openReadAsync();
    DataLakeDirectoryClIEnt targetdirectoryClIEnt = taregetDataLakefileSystemClIEnt.GetDirectoryClIEnt("folder1/folder2/");
    DataLakefileClIEnt targetfileClIEnt = await targetdirectoryClIEnt.CreatefileAsync("myfile.csv.csv");

如您所见,基于访问密钥的身份验证。我可以在本地成功运行此代码。但如果我发布到 azure 中的函数应用程序,则会出现此异常:

An unhandled exception of type 'Azure.requestFailedException' occurred in System.Private.Corelib.dll
service request Failed.
Status: 403 (This request is not authorized to perform this operation.)
ErrorCode: AuthorizationFailure

headers:
transfer-encoding: chunked
Server: Microsoft-httpAPI/2.0
x-ms-request-ID: 0d261530-201e-009b-5a39-3c8d7e000000
x-ms-clIEnt-request-ID: 13ec55f9-7a99-4959-bb1c-024e5848a414
x-ms-error-code: AuthorizationFailure
Date: Wed,28 Apr 2021 14:20:59 GMT

此行发生异常:

Stream reader= await sourcefileClIEnt.openReadAsync();

我应该怎么做才能解决这个问题?

解决方法

我使用下面的代码并部署到 azure,它似乎工作正常:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.http;
using Microsoft.AspNetCore.http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Azure.Storage.Files.DataLake;

namespace FunctionApp110
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [httptrigger(AuthorizationLevel.Anonymous,"get","post",Route = null)] httprequest req,ILogger log)
        {
            String str = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net";
            String sourcefs = "source";
            String sinkfs = "sink";
            String sourcedir = "test";
            String sinkdir = "test";
            String sourcefile = "source.txt";
            String sinkfile = "sink.txt";
            DataLakeFileSystemClient sourcefilesystemclient = new DataLakeFileSystemClient(str,sourcefs);
            DataLakeFileSystemClient sinkfilesystemclient = new DataLakeFileSystemClient(str,sinkfs);
            var sourcedirectoryclient = sourcefilesystemclient.GetDirectoryClient(sourcedir);
            var sinkdirectoryclient = sinkfilesystemclient.GetDirectoryClient(sinkdir);
            var sourcefileclient = sourcedirectoryclient.GetFileClient(sourcefilE);
            Stream reader = sourcefileclient.openRead();
            MemoryStream msreader = new MemoryStream();
            reader.CopyTo(msreader);
            msreader.Position = 0;
            DataLakeFileClient sinkfileclient = sinkdirectoryclient.CreateFile(sinkfilE);
            sinkfileclient.Append(msreader,0);
            sinkfileclient.Flush(sourcefileclient.GetProperties().Value.ContentLength);
            return new OkObjectResult("This is a test.");
        }
    }
}

你可以试试。:)

大佬总结

以上是大佬教程为你收集整理的尝试从 Data Lake gen2 读取 blob 时,此请求无权执行此操作全部内容,希望文章能够帮你解决尝试从 Data Lake gen2 读取 blob 时,此请求无权执行此操作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。