![](/img/trans.png)
[英]Copy file from one DataLake Gen2 to another Data Lake Gen 2 via C# in Azure Functions
[英]This request is not authorized to perform this operation while trying to read a blob from Data Lake gen2
我想将一个 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 中发布到 function 应用程序,我会收到此异常:
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.");
}
}
}
你可以试试。:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.