[英]Any samples using NodeJS in Azure Functions to save files to Azure Data Lake Storage?
这是完全可行的,这是在Azure数据湖中创建示例文件的nodeJs代码,您可以在ndoe js的Azure函数中使用类似的功能
先决条件:
1)有权访问Data Lake Analytics帐户的服务主体。
2)一个Azure Data Lake Store帐户。
需要图书馆
npm安装异步
npm安装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;
});
}
]);
希望能帮助到你。
截至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提供的功能外,
new azureDataLakeStore.DataLakeStoreFileSystemClient
创建客户端 client.fileSystem.create
创建文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.