从Javascript下载Azure DataLake文件

时间:2017-12-20 06:47:11

标签: javascript azure cross-domain azure-data-lake

我试图从azure数据湖存储中下载文件。它使用Rest API在c#端工作。但是它没有在java脚本中工作。

我的示例c#代码是

//获取访问令牌

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;
            }


        }
    }

文件下载代码

 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();
                }
            }
        }
    }

首先我使用客户端凭据生成令牌,使用路径示例https://mydatalaksestore.azuredatalaksestore.net/myfolder/myfile生成基于令牌的下载文件我在源路径中传递myfolder / myfile,并基于destFilePath文件名下载文件

在javascript中我从我的api服务器获取了accessstoken并发送了mydatalakestore的请求它为localhost:8085的交叉orgin抛出错误

enter image description here

任何人都知道如何使用访问令牌从客户端使用Javascript下载datalake商店文件而不会出现交叉错误

先谢谢

0 个答案:

没有答案