如何允许来自我的 firebase 的 public 到 view/download 数据?
How can I allow public to view/download data from my firebase?
我正在 www.openwifinder.com/ 从事一个开放数据项目。我们使用 firebase 来存储我们将使用 javascript 加载到 ESRI 地图的数据。因为我们正在处理开放数据的想法,所以我们希望 public 能够将存储在 firebase 中的整个点云下载为 .json 文件。我的团队最初从数据页面上的 "Export Data" 按钮复制了 link,但我们发现 link 是临时的,因为 url 中的授权令牌已过期.我们还尝试 linking 到 "Instance.firebaseio.com" url,但是当您单击它时(在另一个浏览器中),它说您无权查看 firebase。
理想情况下,我们希望做一些像 Firebase 对它的开放数据集页面所做的事情。当你点击那些打开的数据 urls 时,你会被带到打开的 firebase 的数据页面,你只能读取和导出数据。它不一定必须是 public link,但即使用户必须注册一个 firebase 帐户才能查看数据也没有问题。当然,真正的 public link 将是我们的理想选择。
有什么想法吗?我已经搜索了该网站的帮助页面以寻找线索。
谢谢!!
我想不出一种方法来使用 API 让用户下载数据,所以我使用此处和网络上其他地方找到的示例以编程方式完成。自从我编写以下代码(以及基于原始 OpenWifinder 代码的项目)以来已经有一段时间了,所以我不确定将它归因于何处,但我将我的解决方案发布在这里,以便其他人找到这个质疑他们能否让这个工作。我敢肯定它不如使用 API 优雅,但它确实有效。
解决方案可能来自这里,但无法判断此人是否从其他地方获得:https://gist.github.com/AnirbanKundu/26f2ba229174b1915873
Export.js:
// JavaScript Document
var myFirebase = new Firebase("FIREBASEURL");
var dataJSON;
var jsonDL;
require([
"dojo/dom",
"dojo/on",
"dojo/_base/lang", "dojo/domReady!"
], function(
dom,
on,
lang,
domReady
) {
var exportDataBtn = dom.byId("exportBtn");
on(exportDataBtn, "click", JSONToCSVConvertor);
var exportJSONBtn = dom.byId("dlJSON");
on(exportJSONBtn, "click", DownloadJSON);
function DownloadJSON(){
//console.log(JSON.stringify(dataJSON));
var jsonString = JSON.stringify(dataJSON, null, 4);
//console.log(jsonString);
jsonDL = exportJSONBtn;
jsonDL.download = "BikeThefts.json";
jsonDL.href = "data:application/json;charset=utf-8," + jsonString;
jsonDL.innerHTML = "Download data as JSON";
};
var dataObject;
//this function grabs a 'snapshot' of all the data in Firebase, then navigates down to the 'features' child. It then iterates through all the
//children under 'attributes' and retrieves all attribute data. Then it converts them to strings or numbers and calls addPoint to map them
myFirebase.on("value", function(snapshot) {
dataObject = snapshot;
dataJSON = dataObject.val();
//console.log(JSON.stringify(dataJSON));
});
function JSONToCSVConvertor() {
//If JSONData is not an object then JSON.parse will parse the JSON string in an Object
var arrData = typeof dataJSON != 'object' ? JSON.parse(dataJSON) : dataJSON;
var CSV = '';
//Set Report title in first row or line
var ShowLabel = true;
var ReportTitle = "Data";
CSV += ReportTitle + '\r\n\n';
//This condition will generate the Label/Header
if (ShowLabel) {
var row = "";
//This loop will extract the label from 1st index of on array
for (var index in arrData[0]) {
//Now convert each value to string and comma-seprated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
CSV += row + '\r\n';
}
//1st loop is to extract each row
for (var i = 0; i < arrData.length; i++) {
var row = "";
//2nd loop will extract each column and convert it in string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
//add a line break after each row
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
//Generate a file name
var fileName = "BikeTheftIncidents_";
//this will remove the blank-spaces from the title and replace it with an underscore
fileName += ReportTitle.replace(/ /g,"_");
//Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
// Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
});
您可以轻松地拥有一个 URL,任何人都可以使用它通过 Firebase Cloud Functions 从 firebase 获取您想要的数据:
https://medium.com/@dalenguyen/csv-or-json-export-with-firebase-functions-fa4a4b1416aa
我正在 www.openwifinder.com/ 从事一个开放数据项目。我们使用 firebase 来存储我们将使用 javascript 加载到 ESRI 地图的数据。因为我们正在处理开放数据的想法,所以我们希望 public 能够将存储在 firebase 中的整个点云下载为 .json 文件。我的团队最初从数据页面上的 "Export Data" 按钮复制了 link,但我们发现 link 是临时的,因为 url 中的授权令牌已过期.我们还尝试 linking 到 "Instance.firebaseio.com" url,但是当您单击它时(在另一个浏览器中),它说您无权查看 firebase。
理想情况下,我们希望做一些像 Firebase 对它的开放数据集页面所做的事情。当你点击那些打开的数据 urls 时,你会被带到打开的 firebase 的数据页面,你只能读取和导出数据。它不一定必须是 public link,但即使用户必须注册一个 firebase 帐户才能查看数据也没有问题。当然,真正的 public link 将是我们的理想选择。
有什么想法吗?我已经搜索了该网站的帮助页面以寻找线索。
谢谢!!
我想不出一种方法来使用 API 让用户下载数据,所以我使用此处和网络上其他地方找到的示例以编程方式完成。自从我编写以下代码(以及基于原始 OpenWifinder 代码的项目)以来已经有一段时间了,所以我不确定将它归因于何处,但我将我的解决方案发布在这里,以便其他人找到这个质疑他们能否让这个工作。我敢肯定它不如使用 API 优雅,但它确实有效。
解决方案可能来自这里,但无法判断此人是否从其他地方获得:https://gist.github.com/AnirbanKundu/26f2ba229174b1915873
Export.js:
// JavaScript Document
var myFirebase = new Firebase("FIREBASEURL");
var dataJSON;
var jsonDL;
require([
"dojo/dom",
"dojo/on",
"dojo/_base/lang", "dojo/domReady!"
], function(
dom,
on,
lang,
domReady
) {
var exportDataBtn = dom.byId("exportBtn");
on(exportDataBtn, "click", JSONToCSVConvertor);
var exportJSONBtn = dom.byId("dlJSON");
on(exportJSONBtn, "click", DownloadJSON);
function DownloadJSON(){
//console.log(JSON.stringify(dataJSON));
var jsonString = JSON.stringify(dataJSON, null, 4);
//console.log(jsonString);
jsonDL = exportJSONBtn;
jsonDL.download = "BikeThefts.json";
jsonDL.href = "data:application/json;charset=utf-8," + jsonString;
jsonDL.innerHTML = "Download data as JSON";
};
var dataObject;
//this function grabs a 'snapshot' of all the data in Firebase, then navigates down to the 'features' child. It then iterates through all the
//children under 'attributes' and retrieves all attribute data. Then it converts them to strings or numbers and calls addPoint to map them
myFirebase.on("value", function(snapshot) {
dataObject = snapshot;
dataJSON = dataObject.val();
//console.log(JSON.stringify(dataJSON));
});
function JSONToCSVConvertor() {
//If JSONData is not an object then JSON.parse will parse the JSON string in an Object
var arrData = typeof dataJSON != 'object' ? JSON.parse(dataJSON) : dataJSON;
var CSV = '';
//Set Report title in first row or line
var ShowLabel = true;
var ReportTitle = "Data";
CSV += ReportTitle + '\r\n\n';
//This condition will generate the Label/Header
if (ShowLabel) {
var row = "";
//This loop will extract the label from 1st index of on array
for (var index in arrData[0]) {
//Now convert each value to string and comma-seprated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
CSV += row + '\r\n';
}
//1st loop is to extract each row
for (var i = 0; i < arrData.length; i++) {
var row = "";
//2nd loop will extract each column and convert it in string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
//add a line break after each row
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
//Generate a file name
var fileName = "BikeTheftIncidents_";
//this will remove the blank-spaces from the title and replace it with an underscore
fileName += ReportTitle.replace(/ /g,"_");
//Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
// Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
});
您可以轻松地拥有一个 URL,任何人都可以使用它通过 Firebase Cloud Functions 从 firebase 获取您想要的数据:
https://medium.com/@dalenguyen/csv-or-json-export-with-firebase-functions-fa4a4b1416aa