目录

  • 1. 系列文章
  • 2. 项目描述及效果展示
  • 3. 阅读本文需要掌握的知识
  • 4. 源码
  • 5. 运行程序
  • 6. 技术交流

1. 系列文章

  • 【IPFS + 区块链 系列】 入门篇 - IPFS环境配置
  • 【IPFS + 区块链 系列】 入门篇 - IPFS+IPNS+个人博客搭建
  • 【IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (上篇)-js-ipfs-api - 数据上传到IPFS
  • 【IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (中篇)-js-ipfs-api - 图片上传到IPFS以及下载

2. 项目描述及效果展示

这篇文章通过truffle unbox react创建项目,安装ipfs-api,将图片存储到ipfs,将图片hash存储到Ethereum区块链,取数据时先从区块链读取图片hash,再通过hashipfs读取数据,解决了区块链大数据存储成本高昂的问题。

3. 阅读本文需要掌握的知识

阅读本文需要将先学习上面的系列文章,由于本文前端使用了大量的React语法,所以建议学习一些React语法,还需要学习truffle framework。

4. 源码

其实这篇文章的内容就是上面几篇文章的综合结合体,所以在这里我将不再对代码做过多的概述。

import React, {Component} from 'react'
import SimpleStorageContract from '../build/contracts/SimpleStorage.json'
import getWeb3 from './utils/getWeb3'import './css/oswald.css'
import './css/open-sans.css'
import './css/pure-min.css'
import './App.css'const ipfsAPI = require('ipfs-api');
const ipfs = ipfsAPI({host: 'localhost', port: '5001', protocol: 'http'});const contract = require('truffle-contract')
const simpleStorage = contract(SimpleStorageContract)
let account;// Declaring this for later so we can chain functions on SimpleStorage.
let contractInstance;let saveImageOnIpfs = (reader) => {return new Promise(function(resolve, reject) {const buffer = Buffer.from(reader.result);ipfs.add(buffer).then((response) => {console.log(response)resolve(response[0].hash);}).catch((err) => {console.error(err)reject(err);})})
}class App extends Component {constructor(props) {super(props)this.state = {blockChainHash: null,web3: null,address: null,imgHash: null,isWriteSuccess: false}}componentWillMount() {ipfs.swarm.peers(function(err, res) {if (err) {console.error(err);} else {// var numPeers = res.Peers === null ? 0 : res.Peers.length;// console.log("IPFS - connected to " + numPeers + " peers");console.log(res);}});getWeb3.then(results => {this.setState({web3: results.web3})// Instantiate contract once web3 provided.this.instantiateContract()}).catch(() => {console.log('Error finding web3.')})}instantiateContract = () => {simpleStorage.setProvider(this.state.web3.currentProvider);this.state.web3.eth.getAccounts((error, accounts) => {account = accounts[0];simpleStorage.at('0x66e9bbfe244799149a9c4eb708a34ea7c9ce67e2').then((contract) => {console.log(contract.address);contractInstance = contract;this.setState({address: contractInstance.address});return;});})}render() {return (<div className="App">{this.state.address? <h1>合约地址:{this.state.address}</h1>: <div/>}<h2>上传图片到IPFS:</h2><div><label id="file">Choose file to upload</label><input type="file" ref="file" id="file" name="file" multiple="multiple"/></div><div><button onClick={() => {var file = this.refs.file.files[0];var reader = new FileReader();// reader.readAsDataURL(file);reader.readAsArrayBuffer(file)reader.onloadend = function(e) {console.log(reader);saveImageOnIpfs(reader).then((hash) => {console.log(hash);this.setState({imgHash: hash})});}.bind(this);}}>将图片上传到IPFS并返回图片HASH</button></div>{this.state.imgHash? <div><h2>imgHash:{this.state.imgHash}</h2><button onClick={() => {contractInstance.set(this.state.imgHash, {from: account}).then(() => {console.log('图片的hash已经写入到区块链!');this.setState({isWriteSuccess: true});})}}>将图片hash写到区块链:contractInstance.set(imgHash)</button></div>: <div/>}{this.state.isWriteSuccess? <div><h1>图片的hash已经写入到区块链!</h1><button onClick={() => {contractInstance.get({from: account}).then((data) => {console.log(data);this.setState({blockChainHash: data});})}}>从区块链读取图片hash:contractInstance.get()</button></div>: <div/>}{this.state.blockChainHash? <div><h3>从区块链读取到的hash值:{this.state.blockChainHash}</h3></div>: <div/>}{this.state.blockChainHash? <div><h2>浏览器访问:{"http://localhost:8080/ipfs/" + this.state.imgHash}</h2><img alt="" style= src={"http://localhost:8080/ipfs/" + this.state.imgHash}/></div>: <img alt=""/>}</div>);}
}export default App

5. 运行程序

  • 项目下载
$ git clone https://github.com/liyuechun/IPFS-Ethereum-Image.git
$ cd IPFS-Ethereum-Image
$ npm install
  • 运行程序
$ ipfs daemon // 终端一
$ npm start   // 终端二

http://liyuechun.org/2017/11/26/ipfs-ethereum-image/

【IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (下篇)-ipfs + Ethereum 大图片存储相关推荐

  1. IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (中篇)-js-ipfs-api - 图片上传到IPFS以及下载

    目录 1. 项目效果图 2. 创建React项目 3. 完成UI逻辑 4. 安装ipfs-api 5. App.js导入IPFS 6. 实现上传图片到IPFS的Promise函数 7. 上传图片到IP ...

  2. IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (上篇)-js-ipfs-api

    目录 1. 内容简介 2. IPFS-HTTP效果图 3. 实现步骤 3.1 安装create-react-app 3.2 React项目创建 3.3 运行React项目 3.4 浏览项目 3.5 安 ...

  3. 【IPFS + 区块链 系列】 入门篇 - IPFS+IPNS+个人博客搭建

    目录 1. 如何在IPFS新增一个文件 1.1 新建file.txt文件 1.2 查看ipfs相关命令 1.3 将file.txt添加到ipfs节点 2. 通过ipfs创建目录存储文件 3. 如何在I ...

  4. ipfs搭建mysql_【IPFS + 区块链 系列】 入门篇 - IPFS环境配置

    孔壹学院:国内区块链职业教育引领品牌. 作者:黎跃春,孔壹学院创始人,区块链.高可用架构师 微信:liyc1215 区块链博客:http://liyuechun.org 目录 1. IPFS简介 IP ...

  5. 【IPFS + 区块链 系列】 入门篇 - IPFS环境配置

    目录 1. IPFS简介 2. IPFS本地环境安装 2.1 下载ipfs压缩包 2.2 安装 3. 项目配置 3.1 创建ipfs节点 3.2 修改节点默认存储空间 3.3 查看节点id 3.4 启 ...

  6. 区块链投资入门篇—骨灰版

    区块链技术未来会大规模地普及,比特币是区块链技术的一种具体应用.能抓住这样的机会是每个人一生中难得的机遇,如同90年代初期的证券市场. 目前很多人普遍的问题是不懂,也懒得看,懒得研究.当你看到这篇教程 ...

  7. 【区块链开发入门】(四) Truffle详解篇2

    由于本篇篇幅较长,因此转为两篇文章.Truffle详解篇篇1请见:link 目录导航页 [区块链开发入门](一) 以太坊的搭建与运行 [区块链开发入门](二) 以太坊的编程接口 [区块链开发入门](三 ...

  8. 【邀请函】Web 3.0中国峰会暨IPFS区块链分布式存储行业大会将于7月15日在成都召开,水滴云邀您一起共话分布式存储未来

    <Web3.0中国峰会暨IPFS区块链分布式存储行业大会>成都站将在7月15-17日成都世纪城新国际会展中心举办,共享分布式存储新机遇!行业盛会,巅峰相聚,助力国家"东数西算&q ...

  9. 《Web3.0中国峰会暨IPFS区块链分布式存储行业大会》开启

    <Web3.0中国PFS区块链分布式存储>开启 Web3.0中国峰会暨IPFS区块链分布式存储行业大会>于7月15日在中国成都,盛大开启!共享分布式存储新机遇,行业盛会,巅峰相聚,助 ...

最新文章

  1. solidworks钣金插件_高效掌握SolidWorks钣金零件加工设计概念
  2. linux内核网络协议栈--网卡报文收发(十六)
  3. JavaScript的DOM操作
  4. 用php编写比赛评奖系统_php编写的抽奖程序中奖概率算法
  5. SQL实战篇:SQL窗口函数及真题
  6. 【转】SharePoint 2010 用户自定义编辑Meta标签的OOB方法
  7. young people can also be a leader
  8. CSS 水平对齐 text-align-last属性
  9. c语言 参数经过运算后还原为输入值,1:编程实现由键盘输入两个整数,将其赋给变量x和y并输出,交换x和y的值后再输出 用函数输出!...
  10. STP中各算法接口开销(COST)计算方式
  11. vb mysql 教程_VB6 数据库 基础 教程
  12. PMP题目与解题思路(第二天)
  13. 【学习—Scrapy1】日常操作
  14. 程序员面试需要带身份证和毕业证原件吗
  15. 英语46级报名考试系统
  16. Oracle中序列的操作以及使用前对序列的初始化
  17. butter滤波器是iir吗_MATLAB IIR滤波器设计函数buttord与butter
  18. Oracle中的wn_concat()函数
  19. 第01节、WEEX是什么?
  20. Virtual Shard

热门文章

  1. 高效CSS的一些建议
  2. 关于 varchar2 的最大长度
  3. 吴恩达 coursera ML 第十一课总结+作业答案
  4. 梯度下降法,牛顿法,高斯-牛顿迭代法,附代码实现
  5. 查看关于yum的配置
  6. 【Python】牛客的输入输出到底怎么整??
  7. 云炬随笔20161117
  8. 一个原来知道却懵懂好久的道理2017-12-08
  9. 基于马克思哲学原理论外在美与内在美2017-12-31
  10. 十三、“词短情长书不尽,桃花潭水是我心。”(2021.2.12)