简体   繁体   English

Azure Data Lake存储通过C#脚本创建文件夹

[英]Azure Data Lake store create folder via C# script

I am trying to create new folder inside my datalake store , there is no error in code however nothing is being reflected in datalake store . 我正在尝试在datalake store创建新文件夹,代码中没有错误,但是datalake store没有任何反映。

Code: 码:

using System;
using System.Collections.Generic;

using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

using Microsoft.Azure.Management.DataLake.Store;
using Microsoft.Azure.Management.DataLake.Store.Models;
using Microsoft.Rest.Azure.Authentication;
namespace test_dlstore
{
    class Program
    {
        private static DataLakeStoreAccountManagementClient _adlsClient;
        private static DataLakeStoreFileSystemManagementClient _adlsFileSystemClient;

        private static string _adlsAccountName;
        private static string _subId;
        private static void Main(string[] args)
        {
            _adlsAccountName = "mycreds@mysite.com"; 

            _subId = "2342342-97ce-a54b2-ba6e-234234234234234";

            string localFolderPath = @"C:\myfolder\"; // TODO: Make sure this exists and can be overwritten.
            string localFilePath = Path.Combine(localFolderPath, "try.txt");
            string remoteFolderPath = "adl://mystore.azuredatalakestore.net/myfolder";
            string remoteFilePath = Path.Combine(remoteFolderPath, "try.txt");

            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            var tenant_id = "my-tenant-id"; 
            var nativeClientApp_clientId = "1950a258-227b-4e31-a9cf-717495945fc2";


            var activeDirectoryClientSettings = ActiveDirectoryClientSettings.UsePromptOnly(nativeClientApp_clientId, new Uri("urn:ietf:wg:oauth:2.0:oob"));
            var creds = UserTokenProvider.LoginWithPromptAsync(tenant_id, activeDirectoryClientSettings).Result;

            _adlsClient = new DataLakeStoreAccountManagementClient(creds) { SubscriptionId = _subId };
            _adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);
            Console.WriteLine(ListAdlStoreAccounts());
            Console.WriteLine(AppendToFile("adl://mystore.azuredatalakestore.net/myfolder/stage/testfile.txt", "abcdefghijklmnopqrstuvwxyz"));
            CreateDirectory("adl://mystore.azuredatalakestore.net/myfolder/newdir");

            Console.ReadLine();

        }
        // Append to file
        public static string AppendToFile(string path, string content)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
            {
                Console.WriteLine(_adlsAccountName, path, content);
                Console.WriteLine(path);
                Console.WriteLine(content);
                _adlsFileSystemClient.FileSystem.AppendAsync(_adlsAccountName, path, stream);
                return "Tried";
            }
        }
        public static string CreateDirectory(string path)
        {
            _adlsFileSystemClient.FileSystem.MkdirsAsync(_adlsAccountName, path);
            return "Tried Creating directory.";
        }
}
}

After execution of above code, program exits with no error. 执行完上述代码后,程序将正确退出。 The connection is being made. 正在建立连接。

Also it is displaying datalake stores that are present but not able to do anything at all over the data lake stores. 它还正在显示存在的datalake存储,但是在整个Lake Lake存储上根本无法执行任何操作。

I am new to azure please help me out. 我是天蓝色,请帮帮我。

I do a demo test on my side,it works correctly on side. 我一边进行演示测试,一边可以正常工作。 The following is my detail steps: 以下是我的详细步骤:

Preparation: 制备:

Registry an AD application and assign role to applcation , more details please refer to Azure official tutorials . 注册一个AD应用程序并应用分配角色 ,更多详细信息请参考Azure官方教程 After that we can get tenantId , appId , secretKey from the Azure Portal. 之后,我们可以得到tenantId,APPID,从Azure的门户秘密密钥

Steps: 脚步:

1.Create an C# console project 1.创建一个C#控制台项目

2.Referece the Microsoft.Azure.Management.DataLake.Store SDK, more details please refer to packages.config file section. 2.请参考Microsoft.Azure.Management.DataLake.Store SDK,有关更多详细信息,请参阅packages.config文件部分。

3.Add the follow code 3.添加以下代码

 var applicationId = "appid";
 var secretKey = "secretkey";
 var tenantId = "tenantid";
 var adlsAccountName = "adlsAccount Name";
 var creds = ApplicationTokenProvider.LoginSilentAsync(tenantId, applicationId, secretKey).Result;
 var adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds,clientTimeoutInMinutes:60); 
 adlsFileSystemClient.FileSystem.Mkdirs(adlsAccountName, "/tomtest/newfolder");

4.Run the test code 4,运行测试代码

在此处输入图片说明

5.Check from the azure portal. 5.从天蓝色门户检查。

在此处输入图片说明

Packages.config Packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.Management.DataLake.Store" version="2.2.0" targetFramework="net452" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.7" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.2.0-preview" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="9.0.2-beta1" targetFramework="net452" />
</packages>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# 中使用服务主体(clientId 和 clientSecret)为 Azure Data Lake Store(Gen-2)创建 SAS 令牌? - How to create SAS token for Azure Data Lake Store (Gen-2) using service principals (clientId and clientSecret) in C#? 如何使用C#复制Azure Data Lake存储中的文件 - how to Copy files inside Azure Data lake store using C# 通过 Azure 函数中的 C# 将文件从一个 DataLake Gen2 复制到另一个 Data Lake Gen 2 - Copy file from one DataLake Gen2 to another Data Lake Gen 2 via C# in Azure Functions Azure Data Lake Store基准测试 - Azure Data Lake Store Benchmarks Azure Data Lake Gen2 - 如何使用 C# 将文件从文件夹移动到另一个文件夹 - Azure Data Lake Gen2 - How do I move files from folder to another folder using C# 从 C# 中的 Azure 数据湖读取数据 - Reading data from Azure Data Lake in C# C#并行写入Azure Data Lake File - c# parallel writes to Azure Data Lake File Azure Data Lake Store中的目录大小 - Directory size in Azure Data Lake Store 我们如何在 azure 文件客户端中为 C# 中的 Azure 数据湖设置 setMetadata - How do we set setMetadata in azure fileclient for Azure Data Lake in C# 从 Azure 数据湖中读取和查询 Parquet 文件 - Read and Query Parquet files from Azure Data Lake Using Azure Function without downloading locally C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM