IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (上篇)-js-ipfs-api
目錄
- 1. 內(nèi)容簡介
- 2. IPFS-HTTP效果圖
- 3. 實現(xiàn)步驟
- 3.1 安裝create-react-app
- 3.2 React項目創(chuàng)建
- 3.3 運行React項目
- 3.4 瀏覽項目
- 3.5 安裝ipfs-api
- 3.6 完成UI邏輯
- 3.7 導(dǎo)入IPFS
- 3.8 編寫上傳大文本字符串到IPFS的Promise函數(shù)
- 3.9 上傳數(shù)據(jù)到IPFS
- 3.10 跨域資源共享CORS配置
- 3.11 再次刷新網(wǎng)頁提交數(shù)據(jù)并在線查看數(shù)據(jù)
- 3.12 從IPFS讀取數(shù)據(jù)
- 3.13 總結(jié)
- 4. 下篇文章預(yù)告
1. 內(nèi)容簡介
- 【IPFS + 區(qū)塊鏈 系列】 入門篇 - IPFS環(huán)境配置
- 【IPFS + 區(qū)塊鏈 系列】 入門篇 - IPFS+IPNS+個人博客搭建
在前面兩篇文章中,第一篇春哥給大家詳細介紹了IPFS環(huán)境配置,第二篇介紹了IPFS如何搭建個人博客,通過這兩篇文章相信大家已經(jīng)對IPFS有所了解,接下來的這篇文章,我們將為大家講解js-ipfs-api的簡單使用,如何將數(shù)據(jù)上傳到IPFS,以及如何從IPFS通過HASH讀取數(shù)據(jù)。
2. IPFS-HTTP效果圖
3. 實現(xiàn)步驟
3.1 安裝create-react-app
參考文檔:https://reactjs.org/tutorial/tutorial.html
localhost:1123 yuechunli$ npm install -g create-react-app3.2 React項目創(chuàng)建
localhost:1123 yuechunli$ create-react-app ipfs-http-demo localhost:ipfs-http-demo yuechunli$ ls README.md package.json src node_modules public yarn.lock localhost:ipfs-http-demo yuechunli$3.3 運行React項目
localhost:ipfs-http-demo yuechunli$ npm start Compiled successfully!You can now view ipfs-http-demo in the browser.Local: http://localhost:3000/On Your Network: http://192.168.0.107:3000/Note that the development build is not optimized. To create a production build, use yarn build.3.4 瀏覽項目
瀏覽器瀏覽http://localhost:3000。
效果如下:
3.5 安裝ipfs-api
??:在這里我就不過多的去介紹React的使用以及開發(fā),如果感興趣的可以去看這套React的視頻,學(xué)完這套視頻你可以直接進企業(yè)找React相關(guān)的前端開發(fā)工作。
- 項目結(jié)構(gòu)
- 安裝ipfs-api
切換到項目根目錄,安裝ipfs-api。
$ npm uninstall --save ipfs-api localhost:ipfs-http-demo yuechunli$ ls README.md package.json src node_modules public yarn.lock localhost:ipfs-http-demo yuechunli$ pwd /Users/liyuechun/Desktop/1123/ipfs-http-demo localhost:ipfs-http-demo yuechunli$ npm uninstall --save ipfs-api
??:ipfs安裝完后,如上圖所示,接下來刷新一下瀏覽器,看看項目是否有問題,正常來講,一切會正常,������,Continue,Continue,Continue……
3.6 完成UI邏輯
拷貝下面的代碼,將src/App.js里面的代碼直接替換掉。
import React, { Component } from 'react'; import './App.css';class App extends Component {constructor(props) {super(props);this.state = {strHash: null,strContent: null}}render() {return (<div className="App"><inputref="ipfsContent"style=/><button onClick={() => {let ipfsContent = this.refs.ipfsContent.value;console.log(ipfsContent);}}>提交到IPFS</button><p>{this.state.strHash}</p><button onClick={() => {console.log('從ipfs讀取數(shù)據(jù)。')}}>讀取數(shù)據(jù)</button><h1>{this.state.strContent}</h1></div>);} }export default App;上面的代碼完成的工作是,當(dāng)我們在輸入框中輸入一個字符串時,點擊提交到IPFS按鈕,將文本框中的內(nèi)容取出來打印,后續(xù)我們需要將這個數(shù)據(jù)上傳到IPFS。點擊讀取數(shù)據(jù)按鈕,我們也只是隨便打印了一個字符串,后面需要從IPFS讀取數(shù)據(jù),然后將讀取的數(shù)據(jù)存儲到狀態(tài)機變量strContent中并且展示出來。
3.7 導(dǎo)入IPFS
const ipfsAPI = require('ipfs-api'); const ipfs = ipfsAPI({host: 'localhost', port: '5001', protocol: 'http'});3.8 編寫上傳大文本字符串到IPFS的Promise函數(shù)
saveTextBlobOnIpfs = (blob) => {return new Promise(function(resolve, reject) {const descBuffer = Buffer.from(blob, 'utf-8');ipfs.add(descBuffer).then((response) => {console.log(response)resolve(response[0].hash);}).catch((err) => {console.error(err)reject(err);})})}response[0].hash返回的是數(shù)據(jù)上傳到IPFS后返回的HASH字符串。
3.9 上傳數(shù)據(jù)到IPFS
this.saveTextBlobOnIpfs(ipfsContent).then((hash) => {console.log(hash);this.setState({strHash: hash}); });ipfsContent是從文本框中取到的數(shù)據(jù),調(diào)用this.saveTextBlobOnIpfs方法將數(shù)據(jù)上傳后,會返回字符串hash,并且將hash存儲到狀態(tài)機變量strHash中。
目前完整的代碼:
import React, {Component} from 'react'; import './App.css';const ipfsAPI = require('ipfs-api'); const ipfs = ipfsAPI({host: 'localhost', port: '5001', protocol: 'http'});class App extends Component {constructor(props) {super(props);this.state = {strHash: null,strContent: null}}saveTextBlobOnIpfs = (blob) => {return new Promise(function(resolve, reject) {const descBuffer = Buffer.from(blob, 'utf-8');ipfs.add(descBuffer).then((response) => {console.log(response)resolve(response[0].hash);}).catch((err) => {console.error(err)reject(err);})})}render() {return (<div className="App"><input ref="ipfsContent" style=/><button onClick={() => {let ipfsContent = this.refs.ipfsContent.value;console.log(ipfsContent);this.saveTextBlobOnIpfs(ipfsContent).then((hash) => {console.log(hash);this.setState({strHash: hash});});}}>提交到IPFS</button><p>{this.state.strHash}</p><button onClick={() => {console.log('從ipfs讀取數(shù)據(jù)。')}}>讀取數(shù)據(jù)</button><h1>{this.state.strContent}</h1></div>);} }export default App;測試:
3.10 跨域資源共享CORS配置
跨域資源共享( CORS )配置,依次在終端執(zhí)行下面的代碼:
localhost:ipfs-http-demo yuechunli$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST", "OPTIONS"]'localhost:ipfs-http-demo yuechunli$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'localhost:ipfs-http-demo yuechunli$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'localhost:ipfs-http-demo yuechunli$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'localhost:ipfs-http-demo yuechunli$ ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'用正確的端口運行daemon:
localhost:ipfs-http-demo yuechunli$ ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001 localhost:ipfs-http-demo yuechunli$ ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001 localhost:ipfs-http-demo yuechunli$ ipfs daemon3.11 再次刷新網(wǎng)頁提交數(shù)據(jù)并在線查看數(shù)據(jù)
- 上傳數(shù)據(jù),并且查看返回hash值
- 在線查看上傳到IPFS的數(shù)據(jù)
3.12 從IPFS讀取數(shù)據(jù)
- ipfs.cat
stream為Uint8Array類型的數(shù)據(jù),下面的方法是將Uint8Array轉(zhuǎn)換為string字符串。
- Utf8ArrayToStr
- 完整源碼
3.13 總結(jié)
這篇文章主要講解如何配置React環(huán)境,如何創(chuàng)建React項目,如何安裝js-ipfs-api,如何上傳數(shù)據(jù),如何設(shè)置開發(fā)環(huán)境,如何下載數(shù)據(jù)等等內(nèi)容。通過這篇文章的系統(tǒng)學(xué)習(xí),你會掌握js-ipfs-api在項目中的使用流程。
這是【IPFS + 區(qū)塊鏈 系列】 入門篇 - IPFS + Ethereum (上篇)-js-ipfs-api,下篇講解如何將IPFS和以太坊智能合約結(jié)合進行數(shù)據(jù)存儲。
4. 下篇文章預(yù)告
這是【IPFS + 區(qū)塊鏈 系列】 入門篇 - IPFS + Ethereum (上篇)-js-ipfs-api,下篇講解如何將IPFS和以太坊智能合約結(jié)合進行數(shù)據(jù)存儲。
http://liyuechun.org/2017/11/22/ipfs-api/
總結(jié)
以上是生活随笔為你收集整理的IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (上篇)-js-ipfs-api的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【IPFS + 区块链 系列】 入门篇
- 下一篇: IPFS + 区块链 系列】 入门篇 -