Search code examples
c#azureazure-data-lake-gen2

Azure Data Lake Storage Gen2 REST APIs with C#


I am trying to set the ACL permissions for a user using the azure Azure Data Lake Storage Gen2 REST APIs. Yet I cannot find a way to it. I used the following microsoft link to do that: microsoft-link-path-update

Here's the code I came up with using C#:

static async Task SetUpPermissions(string accessToken)
    {
        // Replace with your values
        string accountName = "<account-name>";
        string filesystem = "<container-name>";
        string directoryOrFile = "<file-name-with-path>";
        string userObjectId = "<user-object-id>";

        // URL for setting ACL
        string url = $"https://{accountName}.dfs.core.windows.net/{filesystem}/{directoryOrFile}?action= setAccessControl";

        // Headers
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            client.DefaultRequestHeaders.Add("x-ms-version", "2023-08-03");
            client.DefaultRequestHeaders.Add("x-ms-lease-action", "acquire-release");
            client.DefaultRequestHeaders.Add("x-ms-date", DateTime.UtcNow.ToString("R"));
            client.DefaultRequestHeaders.Add("x-ms-client-request-id", Guid.NewGuid().ToString());

            // Body with ACL settings
            string aclString = $"user:{userObjectId}:rwx,group::r-x,other::r--,mask::rwx,default:user:{userObjectId}:rwx,default:group::r-x,default:other::r--";
            string body = $"[{{\"acl\": \"{aclString}\"}}]";

            // Make the request
            HttpContent content = new StringContent(body, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PatchAsync(url, content);

            // Check the response
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("ACL set successfully");
            }
            else
            {
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"Failed to set ACL: {response.StatusCode} {responseBody}");
            }
        }
    }

I am getting the following error:

"An HTTP header that's mandatory for this request is not specified",

yet only "x-ms-lease-action" should be required.


Solution

  • Set the ACL permissions for a user using the Azure Data Lake Storage Gen2 REST APIs.

    You can use the below code to set the ACL permissions for a user using the Azure Data Lake Storage Gen2 REST APIs.

    Code:

    using System;
    using System;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                string accessToken = "<access token>";
                await SetUpPermissions(accessToken);
            }
    
            static async Task SetUpPermissions(string accessToken)
            {
                // Replace with your values
                string accountName = "Storage account name";
                string filesystem = "filesystem name";
                string directoryOrFile = "path to file";
                string userObjectId = "user object id";
    
                
                string url = $"https://{accountName}.dfs.core.windows.net/{filesystem}/{directoryOrFile}?action=setAccessControl";
    
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    client.DefaultRequestHeaders.Add("x-ms-version", "2023-08-03");
                    client.DefaultRequestHeaders.Add("x-ms-date", DateTime.UtcNow.ToString("R"));
                    client.DefaultRequestHeaders.Add("x-ms-acl", $"user:{userObjectId}:rwx,group::r-x,other::r--,mask::rwx");
    
                    HttpContent content = new StringContent(string.Empty);
                    HttpResponseMessage response = await client.PatchAsync(url, content);
                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("ACL set successfully");
                    }
                    else
                    {
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine($"Failed to set ACL: {response.StatusCode} {responseBody}");
                    }
                }
            }
        }
    }
    

    Output:

    ACL set successfully
    

    enter image description here

    Portal: enter image description here

    Reference:

    Performing simple ADLS Gen2 Storage REST API operations using CURL - Microsoft Community Hub