0

I was trying to download the file from azure data lake storage. it's working on c# side using Rest API. but it's not working in a java script.

My Sample c# code is

//Get Access Token

public DataLakeAccessToken ServiceAuth(string tenatId, string clientid, string clientsecret)
{

        var authtokenurl = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenatId);
        using (var client = new HttpClient())
        {
            var model = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("grant_type","client_credentials"),
                new KeyValuePair<string, string>("resource","https://management.core.windows.net/"),//Bearer
                new KeyValuePair<string, string>("client_id",clientid),
                new KeyValuePair<string, string>("client_secret",clientsecret),

            };
            var content = new FormUrlEncodedContent(model);
            HttpResponseMessage response = client.PostAsync(authtokenurl, content).Result;
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var accessToken = JsonConvert.DeserializeObject<DataLakeAccessToken>(response.Content.ReadAsStringAsync().Result);
                return accessToken;
            }
            else
            {
                return null;
            }


        }
    }

File Download Code is

 public void DownloadFile(string srcFilePath, ref string destFilePath)
    {

        int i = 0;
        var folderpath = Path.GetDirectoryName(destFilePath);
        var filename = Path.GetFileNameWithoutExtension(destFilePath);
        var extenstion = Path.GetExtension(destFilePath);
        Increment:
        var isfileExist = File.Exists(destFilePath);
        if (isfileExist)
        {
            i++;
            destFilePath = folderpath+filename + "_" + i + "_" + extenstion;
            goto Increment;
         }
            string DownloadUrl = "https://{0}.azuredatalakestore.net/webhdfs/v1/{1}?op=OPEN&read=true";
        var fullurl = string.Format(DownloadUrl, _datalakeAccountName, srcFilePath);
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesstoken.access_token);
            using (var formData = new MultipartFormDataContent())
            {
                var response = client.GetAsync(fullurl).Result;
                using (var fs = new FileStream(destFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                   response.Content.CopyToAsync(fs).Wait();
                }
            }
        }
    }

first i was Generate the token using client credentials and the token based download file using path example https://mydatalaksestore.azuredatalaksestore.net/myfolder/myfile i pass myfolder/myfile in source path and destFilePath file name based download the file

in javascript i was get the accesstoken from my api server and send the request for mydatalakestore it's throw error for cross orgin for localhost:8085 like this

enter image description here

Any one know how to download the datalake store file using Javascript from Client Side using Access Token without cross orgin error

Thanks in Advance

umasankar
  • 599
  • 1
  • 9
  • 28
  • 2
    Based on https://stackoverflow.com/questions/40693452/azure-lake-store-missing-access-control-allow-origin-from-response-header, it seems CORS is not supported for Azure Data Lake. – Gaurav Mantri Dec 20 '17 at 07:10
  • Hi umasankar..i have a question related to Download file code. I am trying to download the file to local drive and I am getting the error "The access token is not provided in the 'Authorization' header". However I am passing the Access token. Not sure what I am missing here. – code_blue Apr 25 '18 at 04:06
  • @code_blue If you are trying to download you pass the access token in request header. In above code "ServiceAuth" method.i was generated the access token and "DownloadFile" method i was passed the access token in my web request. this way you should pass the access token download your file to local drive using the way of client credential flow. – umasankar Apr 27 '18 at 05:27

0 Answers0