使用Azure Functions中的NodeJS将文件保存到Azure Data Lake Storage的任何示例?

问题描述 投票:-1回答:2

我的用例非常像这个question。但我们正在寻找一个nodejs解决方案。无法在任何地方找到它。希望至少这是可行的。

node.js azure azure-functions azure-data-lake
2个回答
0
投票

它是完全可行的,这是在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 install async

npm install adal-node

npm安装azure-common

npm安装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;
        });
    }
]);

希望能帮助到你。


0
投票

截至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安装--save-exact [email protected],不是最新的3.1.0版本,最新版本不起作用。
  2. 使用new azureDataLakeStore.DataLakeStoreFileSystemClient创建客户端
  3. 使用client.fileSystem.create创建文件
© www.soinside.com 2019 - 2024. All rights reserved.