Ipfs

通過埠 5001 訪問 IPFS

  • May 3, 2017

我正在使用 IPFS 將帶有地址的媒體文件儲存在區塊鏈上。我在埠和訪問方面遇到了很多問題。在下面的程式碼中,我有一些程式碼部分執行,但我遇到了訪問問題。我認為這與 API 有關,因為 IP 設置為 0.0.0.0。

錯誤 -

POST http://162.243.237.41:5001/api/v0/add?stream-channels=true 403   (Forbidden)
ClientRequest._onFinish @ request.js:130
(anonymous) @ request.js:62
EventEmitter.emit @ events.js:96
finishMaybe @ _stream_writable.js:504
endWritable @ _stream_writable.js:514
Writable.end @ _stream_writable.js:484
ClientRequest.end @ request.js:273
onend @ _stream_readable.js:499
g @ events.js:165
EventEmitter.emit @ events.js:78
endReadableNT @ _stream_readable.js:920
afterTickTwo @ index.js:27
Item.run @ browser.js:153
drainQueue @ browser.js:123
jenbil.com/:1 Fetch API cannot load http://162.243.237.41:5001/api/v0/add?stream-channels=true. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://jenbil.com:3000' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

App 類中的程式碼 (ReactJS) 擴展了 Component { -

this.IpfsAPI = IpfsAPI('162.243.237.41', '5001')

渲染中的程式碼 -

var zstr = 'hello world from Zillerium2'
  this.IpfsAPI.add(new Buffer(zstr), function (err, res){
         console.log("hello");
         if(err || !res) return console.error("ipfs add error", err, res);
         else{
           console.log("no issue");
           console.log(res);
           res.forEach(function(text) {
                  console.log('successfully stored', text.hash);
                //  console.log('successfully stored', text.path);
                //  display(file.Hash);
                   var textaddress=text.hash;
                   console.log(textaddress);
           });
         }
       });

我有 IPFS 守護程序如下 -

root@ubuntu-2gb-nyc2-01:/home/zipfs# ipfs daemon
Initializing daemon...
Adjusting current ulimit to 2048...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/10.13.0.5/tcp/4001
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/162.243.237.41/tcp/4001
Swarm listening on /ip4/162.243.237.41/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
API server listening on /ip4/0.0.0.0/tcp/5001
Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8180
Daemon is ready

分配給 this.IpsfAPI 的 API 呼叫是否錯誤?IP 地址看起來沒問題。

需要啟用 IPFS CORS 才能通過 JavaScript Api 連接和訪問

$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://example.com\"]"
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\", \"GET\"]"

以供參考

https://github.com/ipfs/js-ipfs-api#cors

而你的遠端機器,需要為請求埠打開防火牆。

如果仍然沒有連接意味著嘗試連接同一個 ipfs 守護程序的 multiaddr。

引用自:https://ethereum.stackexchange.com/questions/15778