【问题标题】:Any samples using NodeJS in Azure Functions to save files to Azure Data Lake Storage?在 Azure Functions 中使用 NodeJS 将文件保存到 Azure Data Lake Storage 的任何示例?
【发布时间】:2019-04-19 09:02:48
【问题描述】:

我的用例与question 非常相似。但我们正在寻找一个 nodejs 解决方案。到处都找不到。希望至少这是可行的。

【问题讨论】:

标签: node.js azure azure-functions azure-data-lake


【解决方案1】:

这是完全可行的,这是在 azure 数据湖中创建示例文件的 nodeJs 代码,你可以在你的 Azure 函数中使用类似的东西来处理 ndoe js

先决条件:

1) 有权访问 Data Lake Analytics 帐户的服务主体。

https://github.com/Huachao/azure-content/blob/master/articles/data-lake-store/resource-group-authenticate-service-principal.md

2) Azure Data Lake Store 帐户。

需要的库

npm 安装异步

npm install adal-node

npm install azure-common

npm install azure-arm-datalake-store

var async = require('async');
var adalNode = require('adal-node');
var azureCommon = require('azure-common');
var azureDataLakeStore = require('azure-arm-datalake-store');

var resourceUri = 'https://management.core.windows.net/';
var loginUri = 'https://login.windows.net/'

var clientId = 'application_id_(guid)';
var clientSecret = 'application_password';

var tenantId = 'aad_tenant_id';
var subscriptionId = 'azure_subscription_id';
var resourceGroup = 'adls_resourcegroup_name';

var accountName = 'adls_account_name';

var context = new adalNode.AuthenticationContext(loginUri+tenantId);

var client;
var response;

var destinationFilePath = '/newFileName.txt';
var content = 'desired file contents';

async.series([
    function (next) {
        context.acquireTokenWithClientCredentials(resourceUri, clientId, clientSecret, function(err, result){
            if (err) throw err;
            response = result;
            next();
        });
    },
    function (next) {
        var credentials = new azureCommon.TokenCloudCredentials({
            subscriptionId : subscriptionId,
            authorizationScheme : response.tokenType,
            token : response.accessToken
        });

        client = azureDataLakeStore.createDataLakeStoreFileSystemManagementClient(credentials, 'azuredatalakestore.net');

        next();
    },
    function (next) {
        client.fileSystem.directCreate(destinationFilePath, accountName, content, function(err, result){
            if (err) throw err;
        });
    }
]);

希望对你有帮助。

【讨论】:

  • 感谢 Mohit,它有帮助。但是该代码无法使用新版本的 azure-arm-datalake-store lib 运行,我发布了另一个答案,其中包含目前对我有用的内容。顺便说一句,维护这个库的人应该更及时地更新文档和示例。目前真的很混乱。
  • 很高兴知道问题已解决。我鼓励你在相关的 repo 中创建一个问题,以便相关的工程师可以处理它。
【解决方案2】:

截至 2019-05-02 这是适用于我的代码。

const resourceUri = 'https://management.core.windows.net/';
const loginUri = 'https://login.windows.net/'

const clientId = 'your client id';
const clientSecret = 'your client secret';

const tenantId = 'your tenant id';
const subscriptionId = 'your subscription id';
const resourceGroup = 'your resource group name';
const accountName = 'your adls account name';
const context = new adalNode.AuthenticationContext(loginUri+tenantId);

       const getClient = () => {
            return new Promise((resolve, reject) => {
                context.acquireTokenWithClientCredentials(resourceUri, clientId, clientSecret, function(err, result){
                    if (err) reject('adal error --' + err.stack)

                    const credentials = new azureCommon.TokenCloudCredentials({
                        subscriptionId : subscriptionId,
                        authorizationScheme : result.tokenType,
                        token : result.accessToken
                    });      
                    // console.log('result token' + result.accessToken)
                    client = new azureDataLakeStore.DataLakeStoreFileSystemClient(credentials);      
                    resolve(client)
                });
            })
        }

    const save = async () => {
        const result = await getResultFromRest() // get json response from 3rd party Rest API
        var options = {
            streamContents: new Buffer(JSON.stringify(result.data))
          }
        const client = await getClient()
        client.fileSystem.create(accountName, '/test/result.json', options, function (err, result, request, response) {
            if (err) {
              console.log(err);
            } else {
              console.log('response is: ' + response);
            }
          })
    }
save()

注意,除了 Mohit 提供的内容,

  1. 必须 npm install --save-exact
    azure-arm-datalake-store@3.0.0-preview,不是最新的 3.1.0 版本,最新版本不起作用。
  2. 使用new azureDataLakeStore.DataLakeStoreFileSystemClient创建客户端
  3. 使用client.fileSystem.create创建文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-03
    • 2020-11-22
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多