【问题标题】:IPFS Pinata service not accepting fileIPFS Pinata 服务不接受文件
【发布时间】:2021-04-05 06:32:45
【问题描述】:

我有一个如下所示的代码,它从浏览器上传文件并保存在服务器中,一旦保存到服务器,我希望服务器连接到 Pinata API,以便文件也可以保存到IPFS 节点。

  let data = new FormData();

                      const fileBuffer = Buffer.from(`./public/files/${fileName}`, 'utf-8');
                      data.append('file', fileBuffer, `${fileName}`);

                      axios.post('https://api.pinata.cloud/pinning/pinJSONToIPFS',
                          data,
                          {
                              headers: {
                                  'Content-Type': `multipart/form-data; boundary= ${data._boundary}`,
                                  'pinata_api_key': pinataApiKey,
                                  'pinata_secret_api_key': pinataSecretApiKey
                              }
                          }
                      ).then(function (response) {
                          console.log("FILE UPLOADED TO IPFS NODE", fileName);
                          console.log(response);
                      }).catch(function (error) {
                          console.log("FILE WASNT UPLOADED TO IPFS NODE", fileName);
                          console.log(error);
                      });

我遇到的问题是,在创建文件的缓冲区并将其包装在 formdata 中后,pinata API 返回错误:

   data: {
      error: 'This API endpoint requires valid JSON, and a JSON content-type'
    }

如果我将数据转换为JSON.stringify(data) 之类的字符串并将内容类型更改为application/json,则文件缓冲区将作为字符串成功上传。

我希望能很好地解释它以获得解决方案。谢谢。

【问题讨论】:

    标签: node.js express axios buffer ipfs


    【解决方案1】:

    您似乎正在尝试将文件上传到 pinJSONToIPFS 端点,该端点纯粹用于通过请求正文传递的 JSON。

    在您的情况下,我建议使用 Pinata 的 pinFileToIPFS endpoint

    以下是一些基于他们的文档的示例代码,可能会有所帮助:

    //imports needed for this function
    const axios = require('axios');
    const fs = require('fs');
    const FormData = require('form-data');
    
    export const pinFileToIPFS = (pinataApiKey, pinataSecretApiKey) => {
        const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`;
    
        //we gather a local file for this example, but any valid readStream source will work here.
        let data = new FormData();
        data.append('file', fs.createReadStream('./yourfile.png'));
    
        return axios.post(url,
            data,
            {
                maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large files
                headers: {
                    'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
                    'pinata_api_key': pinataApiKey,
                    'pinata_secret_api_key': pinataSecretApiKey
                }
            }
        ).then(function (response) {
            //handle response here
        }).catch(function (error) {
            //handle error here
        });
    };
    

    【讨论】:

    • 我可以让我的文件发布,但它总是在 Pinata 中显示为“未设置名称”。如何定义名称?使用与上面相同的代码
    • 其实很简单,你只需要将它作为第三个参数添加到data.append 就像这样data.append('file', fs.createReadStream('./yourfile.png'), 'yourFileName.png');
    【解决方案2】:

    将任何文件固定到 IPFS 的正确代码如下。

    显然,即使是 Pinata 支持人员也不知道这一点。 您需要设置一个具有属性名称文件路径的对象作为最后一个参数。名字无所谓,可以是重复的,可以和其他的一样,也可以是唯一的。

    const url = "https://api.pinata.cloud/pinning/pinFileToIPFS";
    const fileContents = Buffer.from(bytes);
    const data = new FormData();
    data.append("file", fileContents, {filepath: "anyname"});
    const result = await axios
        .post(url, data, {
            maxContentLength: -1,
            headers: {
                "Content-Type": `multipart/form-data; boundary=${data._boundary}`,
                "pinata_api_key": userApiKey,
                "pinata_secret_api_key": userApiSecret,
                "path": "somename"
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 2022-11-12
      • 2014-02-17
      • 1970-01-01
      相关资源
      最近更新 更多