通过IBM Cloud中的Node.js从Cloudant noSQL数据库下载管道(大)CSV,而不是在生产环境中工作
我在Node.js中开发了一个应用程序,我需要以CSV格式(Cloudant noSQL - IBM Cloud)从云中的noSQL数据库下载原始数据。 Cloudant允许我通过API从数据库下载所有数据。我希望用户能够通过我的Node.js应用程序下载相同的文件。我所做的是将数据库API的响应传递给客户端的响应。当我在本地执行此操作时,这很好用,但是当我将应用程序上传到IBM Cloud并尝试下载相同的文件(40 mb)时,它永远不会下载(但它确实适用于小文件,ej.5 mb)。
我尝试了3种不同的方法,一种是请求模块,另外两种是https模块。
1.请求模块
request({
url: database.credentials.url + path,
method: 'GET'
}).pipe(res);
2. Https模块(第一次尝试)
res.setHeader('content-Type', 'text/csv');
res.setHeader('transfer-encoding','chunked');
res.setHeader('strict-transport-security','max-age=31536000');
https.get(database.credentials.url + path, (csv_res) => {
console.log('Download raw data db headers');
console.log(csv_res.headers);
csv_res.on('data', (d) => {
res.write(d);
process.stdout.write(".");
});
csv_res.on('end', () => {
res.end();
});
}).on('error', (e) => {
console.error(e);
});
3. Https模块(第二次尝试)
var options = {
hostname: database.credentials.host,
port: 443,
path: path,
method: 'GET',
headers: {
'Authorization': 'Basic ' + new Buffer(database.credentials.username + ':' + database.credentials.password).toString('base64')
}
};
var proxy = https.request(options, function (csv_res) {
console.log(csv_res.headers)
res.writeHead(csv_res.statusCode, csv_res.headers)
csv_res.pipe(res, {
end: true
}).on('error', (e) => {
console.log("ERROR piping to res: " + e)
})
});
req.pipe(proxy, {
end: true
}).on('error', (e) => {
console.log("ERROR piping from req" + e)
})
当我在本地运行应用程序时,文件被下载但是当我在云中执行时,文件永远不会被下载,一段时间后浏览器会显示带有网络错误的文件。为什么会这样?
回答如下:执行此操作时,请检查监视仪表板。您可能会受到限制。我还发布了一个实用程序,它具有cloudant,https://github/glynnbird/couchimport的导出功能。
如果您有其他问题或疑虑,请告诉我们IBM Cloud支持,我们可以帮助您!
通过IBM Cloud中的Node.js从Cloudant noSQL数据库下载管道(大)CSV,而不是在生产环境中工作
我在Node.js中开发了一个应用程序,我需要以CSV格式(Cloudant noSQL - IBM Cloud)从云中的noSQL数据库下载原始数据。 Cloudant允许我通过API从数据库下载所有数据。我希望用户能够通过我的Node.js应用程序下载相同的文件。我所做的是将数据库API的响应传递给客户端的响应。当我在本地执行此操作时,这很好用,但是当我将应用程序上传到IBM Cloud并尝试下载相同的文件(40 mb)时,它永远不会下载(但它确实适用于小文件,ej.5 mb)。
我尝试了3种不同的方法,一种是请求模块,另外两种是https模块。
1.请求模块
request({
url: database.credentials.url + path,
method: 'GET'
}).pipe(res);
2. Https模块(第一次尝试)
res.setHeader('content-Type', 'text/csv');
res.setHeader('transfer-encoding','chunked');
res.setHeader('strict-transport-security','max-age=31536000');
https.get(database.credentials.url + path, (csv_res) => {
console.log('Download raw data db headers');
console.log(csv_res.headers);
csv_res.on('data', (d) => {
res.write(d);
process.stdout.write(".");
});
csv_res.on('end', () => {
res.end();
});
}).on('error', (e) => {
console.error(e);
});
3. Https模块(第二次尝试)
var options = {
hostname: database.credentials.host,
port: 443,
path: path,
method: 'GET',
headers: {
'Authorization': 'Basic ' + new Buffer(database.credentials.username + ':' + database.credentials.password).toString('base64')
}
};
var proxy = https.request(options, function (csv_res) {
console.log(csv_res.headers)
res.writeHead(csv_res.statusCode, csv_res.headers)
csv_res.pipe(res, {
end: true
}).on('error', (e) => {
console.log("ERROR piping to res: " + e)
})
});
req.pipe(proxy, {
end: true
}).on('error', (e) => {
console.log("ERROR piping from req" + e)
})
当我在本地运行应用程序时,文件被下载但是当我在云中执行时,文件永远不会被下载,一段时间后浏览器会显示带有网络错误的文件。为什么会这样?
回答如下:执行此操作时,请检查监视仪表板。您可能会受到限制。我还发布了一个实用程序,它具有cloudant,https://github/glynnbird/couchimport的导出功能。
如果您有其他问题或疑虑,请告诉我们IBM Cloud支持,我们可以帮助您!