简体   繁体   中英

Any samples using NodeJS in Azure Functions to save files to Azure Data Lake Storage?

my use case is very much like this question . But we are looking for a nodejs solution. Couldn't find it anywhere. Hope at least this is doable.

It is completely doable , Here is the nodeJs code to create a sample file in azure data lake, you can use something similar in your Azure function for ndoe js

Prerequisite:

1) A service principal with permissions to access the Data Lake Analytics account.

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

2) An Azure Data Lake Store account.

Libraries needed

npm install async

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

Hope it helps.

As of 2019-05-02 here's the code that works for me.

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

NOTE apart from what Mohit's provided,

  1. Have to npm install --save-exact
    azure-arm-datalake-store@3.0.0-preview, not the latest 3.1.0 version, the latest version doesn't work .
  2. Use new azureDataLakeStore.DataLakeStoreFileSystemClient to create client
  3. Use client.fileSystem.create to create file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM