【问题标题】:function node to upload file to ipfs and to return the file hash函数节点将文件上传到 ipfs 并返回文件哈希
【发布时间】:2019-10-30 10:34:00
【问题描述】:

我对 JavaScript 了解不多,但我真的很喜欢 Node-Red 框架来连接物联网设备。我构建了一个小流程,将 http-request 节点(获取图片)连接到功能节点,并将功能节点连接到 msg-debug 节点(查看发生了什么)。

图片被请求节点成功抓取,我在函数节点中使用这个javascript函数将有效负载(图像)发送到IPFS上传并取回上传文件的哈希,但它没有工作。我做错了什么?

注意,IPFS 守护程序已在我的机器上启动并运行:http://localhost:5001/

我尝试了不同的脚本(来自 node.js 或其他在线示例),但都没有奏效。

这里是函数节点中的代码:

//const ipfsAPI = require('ipfs-api');

var payload=msg.payload;

function toIPFS(file) {
    return new Promise(resolve => {
        const reader = new FileReader();
        reader.onloadend = function() {
        const ipfs = IpfsApi('ipfs', 5001,{protocol : "http"}) // Connect to IPFS
        const buf = buffer.Buffer(reader.result) // Convert data into buffer
        ipfs.files.add(buf, (err, result) => { // Upload buffer to IPFS
            if(err) {
              return "error";
            }
              let url = `https://ipfs.io/ipfs/${result[0].hash}`
              resolve('resolved url');
          })
        }
        reader.readAsArrayBuffer(file); // Read Provided File
        //console.log(`done!!!!`)
   });
  }
//var test = toIPFS(payload);
toIPFS(payload);

return msg;

最后我从 msg-debug 节点得到的消息对象是这样的:

object
_msgid: "80561394.e873e"
topic: ""
payload: ""
statusCode: 200
headers: object
accept-ranges: "bytes"
access-control-allow-origin: "*"
access-control-expose-headers: "Content-Length"
cache-control: "max-age=604800, must-revalidate"
content-type: "image/jpeg"
date: "Sun, 16 Jun 2019 16:39:46 GMT"
last-modified: "Thu, 28 Jun 2018 04:32:21 GMT"
server: "ECS (lcy/1D6A)"
strict-transport-security: "max-age=631138519"
surrogate-key: "media media/bucket/2 media/1012192454752301056"
x-cache: "HIT"
x-connection-hash: "4d2ce5d9ed372dccc93b26cf6e8ce8c4"
x-content-type-options: "nosniff"
x-response-time: "460"
content-length: "127686"
connection: "close"
x-node-red-request-node: "6f2e7990"
responseUrl: "https://upload.wikimedia.org/wikipedia/commons/1/12/K2_2006b.jpg"
redirectList: array[0]

【问题讨论】:

    标签: javascript node-red ipfs


    【解决方案1】:

    应该不需要任何文件处理,因为您已经在缓冲区(msg.payload)中拥有文件的内容。

    所以你应该可以调用:

    const ipfs = IpfsApi('ipfs', 5001,{protocol : "http"})
    ipfs.files.add(buf, (err, result) => {
        msg.payload = result[0].hash;
        node.send(msg)
    })
    

    (假设您已将ipfs 模块添加到global context,因此它在范围内。)

    【讨论】:

    • 您好,谢谢,但是如何取回文件哈希?它不是由文件处理处理的吗?我尝试做类似msg.topic="object?"; 的事情,因为主题是我可以在调试消息输出中看到的字符串变量。
    • 老实说,您最好编写一个适当的 IPFS Node-RED 节点,而不是尝试使用功能节点。
    • const ipfs = IpfsApi('ipfs', 5001,{protocol : "http"}) ipfs.files.add(buf, (err, result) => { msg.payload = result[0].hash; node.send(msg) }) 有效,但我没有在有效负载中看到哈希。它的对象输出和以前一样吗?
    • 去掉函数节点末尾的return msg
    • 好的,结果是:"TypeError: Cannot read property '0' of undefined"
    猜你喜欢
    • 2021-12-31
    • 2018-10-20
    • 2023-03-03
    • 2011-11-12
    • 2021-12-02
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2022-11-12
    相关资源
    最近更新 更多