前提:
1.使用 node2011 版本的镜像。(https://docs.pd.mingdao.com/faq/codeextensionversion)
2.扩展 ssh2-sftp-client 库。(https://docs.pd.mingdao.com/faq/codeextension)
代码块内容:
1.api 获取数据,超过 1000 条需要自己循环。可以获取指定字段。
2.将 JSON 转换为 CSV 格式。
3.sftp 推送数据到指定服务器。
var url='http://【ip】/api/v2/open/worksheet/getFilterRows';
var result={};
var data={
"appKey": input.appKey,
"sign": input.sign,
"worksheetId": input.worksheetId,
"viewId": input.viewId,
"pageSize": 1000,
"pageIndex": 2,
"notGetTotal":true,
"useControlId":false,
"getSystemControl":false
}
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const res = await fetch(url,{
method:"post",
headers:{
'Content-Type':'application/json; charset=UTF-8'
},
body:JSON.stringify(data)
}).then(function(response){
if(response.ok){
return response.json();
}else{
return response.json();
return {"error":"无数据"};
}
}).catch(function(err){
return {"error":"Fetch错误:"+err};
});
result = await res;
var csvdata="";
let csv = '';
let jsonData=result.data.rows
//要提取的字段
let fieldsToExtract = ['date','logtype2','name','staffid'];
if(result && result.success == true && result.error_code==1 && result.data.rows){
//json2csv
//提取字段
let extractedData= jsonData.map(obj => {
return fieldsToExtract.map(field => {
return typeof obj[field] === 'string' ? `"${obj[field]}"` : obj[field];
});
});
//csvheaders = Object.keys(result.data.rows[0]);
//csvHeaders = extractedData
//生成csv头部
let csvHeaders = fieldsToExtract.join(',');
//将提取的数据转换为csv格式
let csvContent=extractedData.map(row=>row.join(',')).join('\n');
//完整的csv字符串
csvdata=csvHeaders + '\n' + csvContent;
}
//output={result:result,accountId:accountId,strdata:JSON.stringify(data)}
let catcherror="";
let debuglog ="nodebug";
let retlog="";
var body="body";
try{
let sftpcl = require('ssh2-sftp-client');
const {Readable}=require('stream');
//let fs = require('fs');
const path = require('path');
let readStream = new Readable({
read(size){
//const data = 'AAestetsfs撒打tsfasdfAA';
this.push(csvdata,'utf8');
this.push(null);
//if(data===undefined)this.push(null);
}
});
//retlog+="1"+readStream.read(3);
const config = {
host: '【ip】',
port: '22',
username: '【username】',
password: '【password】',
//debug:(msg)=>debuglog+=msg // 输出debug日志
};
const BASIC_URL = '/tmp/';
remotePath = BASIC_URL + 'sftptestdir/';
let sftp = new sftpcl();
await sftp.connect(config);
await sftp.put(readStream, remotePath + 'csvoutput.txt');
await sftp.end();
}catch(error){
catcherror+="catch:"+error;
}
output={result_success:result.success,result_error_code:result.error_code,result_error_msg:result.error_msg,strdata:JSON.stringify(data),csvdata:csvdata.substr(0,100),resultdatarows:JSON.stringify(result.data.rows[0]),catcherror:catcherror,debuglog:debuglog}