0

我的目标是创建一个反应应用程序,允许用户使用 PINATA 上传图像并将其固定到 IPFS。我尝试了几次,但我不明白为什么我的请求被拒绝了。我明白了

POST https://api.pinata.cloud/pinning/pinFileToIPFS 400(错误请求)

我按照这个说明(https://github.com/PinataCloud/Pinata-SDK):pinFileToIPFS想要一个“可读流”,我认为我的“streamFile”是一个可读流(streamFile instanceof stream.Readable是TRUE),所以我不知道为什么会被拒绝。

我所做的是

npx create-react-app pinata-test
npm install --save @pinata/sdk
npm i streamifier
npm i stream

然后我编辑了 App.js 文件如下:

import { useState, } from "react";
import './App.css';
import stream from 'stream';


function App() {
  const [img, setImg] = useState("");

  const onSubmitPressed = (e) => {
    e.preventDefault()
    const key = process.env.KEY;
    const secret = process.env.SECRET;
    const pinataSDK = require('@pinata/sdk');
    const pinata = pinataSDK(key,secret);
    var streamifier = require('streamifier');

    pinata.testAuthentication().then((result) => {
      //handle successful authentication here
      console.log(result);
      }).catch((err) => {
          //handle error here
          console.log(err);
      });
    
      var reader = new FileReader();
      reader.readAsDataURL(img); 
      reader.onloadend = function() {
          var base64data = reader.result;
          var buffer = new Buffer.from(base64data, 'base64');
          var streamFile = streamifier.createReadStream(buffer);
          console.log('instanceof: ',streamFile instanceof stream.Readable); 

          pinata.pinFileToIPFS(streamFile).then((result) => {
              //handle results here
              console.log(result);
          }).catch((err) => {
              //handle error here
              console.log(err);
          });
      }
};
  return (
    <div className="App">
      <form>
      <h2>Insert image</h2>
        <input
          type="file"
          accept="image/*"
          onChange={(event) => setImg(event.target.files[0])}
        />
        <button id="pin" onClick={onSubmitPressed}>Press to PIN</button>
      </form>
    </div>
  );
}

export default App;

谢谢你们

4

0 回答 0