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

在阅读这篇文章之前,你需要先学习【IPFS + 区块链 系列】 入门篇 – IPFS环境配置这篇文章。

目录

1. 如何在IPFS新增一个文件1.1 新建file.txt文件1.2 查看ipfs相关命令1.3 将file.txt添加到ipfs节点2. 通过ipfs创建目录存储文件3. 如何在IPFS新增一个目录3.1 使用ipfs add -r可以上传一整个目录3.2 通过路径访问contactme.txt文件数据3.3 通过Hash查看数据IPFS网络数据4. 创建简易的网页发布到IPFS4.1 创建一个index.html文件4.2 创建一个style.css文件4.3 添加到ipfs4.4 网络同步4.5 访问网站4.6 发布到IPNS5. 发布个人博客5.1 搭建静态博客5.2 节点ID替换5.3 浏览博客6. 下篇预报6.1 ipfs + ethereumDapp开发入门

1. 如何在IPFS新增一个文件

1.1 新建file.txt文件

打开终端,切换到桌面,新建一个文件夹1121,切换到1121中,通过vi新建一个文件file.txt,文件里面输入春哥微信号liyc1215保存并且退出。

localhost:Desktop yuechunli$ pwd/Users/liyuechun/Desktoplocalhost:Desktop yuechunli$ mkdir 1121localhost:Desktop yuechunli$ cd 1121/localhost:1121 yuechunli$ vi file.txtlocalhost:1121 yuechunli$ cat file.txt liyc1215localhost:1121 yuechunli$ 

1.2 查看ipfs相关命令

localhost:1121 yuechunli$ ipfs helpUSAGE  ipfs - Global p2p merkle-dag filesystem.  ipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...SUBCOMMANDS  BASIC COMMANDS    init          Initialize ipfs local configuration    add <path>    Add a file to IPFS    cat <ref>     Show IPFS object data    get <ref>     Download IPFS objects    ls <ref>      List links from an object    refs <ref>    List hashes of links from an object  DATA STRUCTURE COMMANDS    block         Interact with raw blocks in the datastore    object        Interact with raw dag nodes    files         Interact with objects as if they were a unix filesystem    dag           Interact with IPLD documents (experimental)  ADVANCED COMMANDS    daemon        Start a long-running daemon process    mount         Mount an IPFS read-only mountpoint    resolve       Resolve any type of name    name          Publish and resolve IPNS names    key           Create and list IPNS name keypairs    dns           Resolve DNS links    pin           Pin objects to local storage    repo          Manipulate the IPFS repository    stats         Various operational stats    p2p           Libp2p stream mounting    filestore     Manage the filestore (experimental)  NETWORK COMMANDS    id            Show info about IPFS peers    bootstrap     Add or remove bootstrap peers    swarm         Manage connections to the p2p network    dht           Query the DHT for values or peers    ping          Measure the latency of a connection    diag          Print diagnostics  TOOL COMMANDS    config        Manage configuration    version       Show ipfs version information    update        Download and apply go-ipfs updates    commands      List all available commands

1.3 将file.txt添加到ipfs节点

localhost:1121 yuechunli$ lsfile.txtlocalhost:1121 yuechunli$ ipfs add file.txt added QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T file.txtlocalhost:1121 yuechunli$ cat file.txt liyc1215localhost:1121 yuechunli$ ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7Tliyc1215localhost:1121 yuechunli$ 

当执行完ipfs add file.txt这个命令以后,会将file.txt添加到ipfs当前的节点中,并且会对file.txt文件生成一个唯一的hashQmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T,如果想查看本地ipfs节点的数据,可以通过ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T进行查看。

⚠️:当我试图通过http://ipfs.io/ipfs/QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T进行数据访问时,无法访问,如下图所示:

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

⚠️:虽然数据已经添加到当前的你自己的IPFS节点中,但是并没有同步到IPFS网络,所以暂时在网络上无法访问。

⚠️:重要:接下来执行下面的命令同步节点数据到IPFS网络,再试图在网络上查看数据。

同步节点

新建一个终端,执行ipfs daemon

localhost:.ipfs yuechunli$ ipfs daemonInitializing daemon...Adjusting current ulimit to 2048...Successfully raised file descriptor limit to 2048.Swarm listening on /ip4/111.196.246.151/tcp/3637Swarm listening on /ip4/127.0.0.1/tcp/4001Swarm listening on /ip4/169.254.170.167/tcp/4001Swarm listening on /ip4/192.168.0.107/tcp/4001Swarm listening on /ip6/::1/tcp/4001API server listening on /ip4/127.0.0.1/tcp/5001Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080Daemon is ready
IPFS网络查看数据

浏览器访问https://ipfs.io/ipfs/QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T

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

2. 通过ipfs创建目录存储文件

在着上面的步骤走,我们可以通过ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7Tliyc1215查看添加到ipfs网络的file.txt文件的内容,如下:

localhost:1121 yuechunli$ ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7Tliyc1215localhost:1121 yuechunli$ 

当然,我们也可以通过ipfs的相关命令在ipfs的根目录下面创建文件夹,并且将file.txt文件移动或者拷贝到我们创建的文件夹中。

⚠️:cp不会改变文件hash,mv会改变hash寻址。

localhost:1121 yuechunli$ ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7Tliyc1215localhost:1121 yuechunli$ ipfs files mkdir /LiYueChunlocalhost:1121 yuechunli$ ipfs files cp /ipfs/QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T /LiYueChun/file.txtlocalhost:1121 yuechunli$ ipfs files ls /LiYueChunlocalhost:1121 yuechunli$ ipfs files ls /LiYueChun/file.txtlocalhost:1121 yuechunli$ ipfs files read /LiYueChun/file.txtliyc1215localhost:1121 yuechunli$ 

3. 如何在IPFS新增一个目录

3.1 使用ipfs add -r可以上传一整个目录

localhost:1121 yuechunli$ ipfs add -r ipfs-tutorial/added QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc ipfs-tutorial/contactme.txtadded QmfKdWsguobA3aDPvSxLB3Bq4HMKyqKSgFr2NFUuVH8n31 ipfs-tutorial/eth-fabric.pngadded QmXe8jTxTh5MZP6BK5cnj19mXNTKVMzNyUJZUHuYyr5dk1 ipfs-tutorial/gongzhonghao.pngadded QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek ipfs-tutorial

3.2 通过路径访问contactme.txt文件数据

如果我们上传的是目录,那么可以通过下面几种方式访问到contactme.txt文件的数据。

localhost:1121 yuechunli$ ipfs cat QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc微信:liyc1215区块链技术交流群:348924182公众号:区块链部落localhost:1121 yuechunli$ ipfs cat /ipfs/QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc微信:liyc1215区块链技术交流群:348924182公众号:区块链部落localhost:1121 yuechunli$ ipfs cat /ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek/contactme.txt微信:liyc1215区块链技术交流群:348924182公众号:区块链部落localhost:1121 yuechunli$ 

3.3 通过Hash查看数据IPFS网络数据

访问目录:https://ipfs.io/ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek

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

通过目录访问文件:https://ipfs.io/ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek/contactme.txt【IPFS + 区块链 系列】 入门篇 – IPFS+IPNS+个人博客搭建通过文件hash直接访问:https://ipfs.io/ipfs/QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc

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

4. 创建简易的网页发布到IPFS

在这里我先自己写一个简单的网页给大家演示,先在桌面新建一个site文件夹,然后按照下面的步骤在site文件夹中建立index.htmlstyle.css文件。

4.1 创建一个index.html文件

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Hello IPFS!</title>  <link rel="stylesheet" href="./style.css" /></head><body>  <h1>Hello IPFS!</h1></body></html>

4.2 创建一个style.css文件

h1 {  color: green;}

4.3 添加到ipfs

localhost:Desktop yuechunli$ ipfs add -r site/added QmWG5rbgT9H77TGq49RXNoqN8M7DNKMnMX425nkmCB6BjS site/index.htmladded QmfGLJ3mryLvicQqzdsghq4QRhptKJtBAPe7yDJxsBGSuy site/style.cssadded QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp site

最后一行是项目根目录的hash,你先通过ipfs daemon同步网络,然后可以通过https://ipfs.io/ipfs/<你的项目根目录hash>,即https://ipfs.io/ipfs/QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp访问项目。

4.4 网络同步

localhost:Desktop yuechunli$ ipfs daemon

4.5 访问网站

浏览器打开https://ipfs.io/ipfs/QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRp,效果图如下:

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

4.6 发布到IPNS

当我们修改网站内容重新添加到ipfs时,hash会发生变化,当我们网站更新时,我们可以将网站发布到IPNS,在IPNS中,允许我们节点的域名空间中引用一个IPFS hash,也就是说我们可以通过节点ID对项目根目录的IPFS HASH进行绑定,以后我们访问网站时直接通过节点·ID访问即可,当我们更新博客时,重新发布到IPNS`即可。

localhost:~ yuechunli$ ipfs name publish QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRpPublished to QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP: /ipfs/QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRplocalhost:~ yuechunli$ ipfs id{    "ID": "QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP"}

当我们执行ipfs name publish命令时,会返回我们的节点ID,你可以通过ipfs id进行查看验证是否是你的节点ID

⚠️:验证

$ ipfs name resolve <peerId>
localhost:~ yuechunli$ ipfs name resolve QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP/ipfs/QmdVEGkT5u7LtzzatTrn8JGNEF3fpuMPVs2rPCfvqRykRplocalhost:~ yuechunli$ 

⚠️:当然我们现在就可以通过IPNS进行访问了。

https://ipfs.io/ipns/QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP

⚠️⚠️⚠️:注意上面是ipns而不是ipfs。

⚠️:如果你网站数据修改,需要重新发布到IPNS。

5. 发布个人博客

你可以通过Hugo按照官方文档创建一个漂亮的静态博客Hugo官方网站,当然你也可以自己编写,或者使用其他开源项目搭建。

5.1 搭建静态博客

大家可以自己搭建,也可以直接下载我的博客源码直接搭建。

源码地址:http://github.com/liyuechun/ipfs_blogger

5.2 节点ID替换

查看你的节点ID
localhost:ipfs_pin yuechunli$ ipfs id{    "ID": "《your peer id》"}localhost:ipfs_pin yuechunli$ 

在上面的源码中全局搜索将源码里面的QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP替换成你自己的ID

接下来重复4. 创建简易的网页发布到IPFS的操作步骤即可。

5.3 浏览博客

浏览器打开https://ipfs.io/ipns/QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP/查看项目效果。

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

6. 下篇预报

6.1 ipfs + ethereumDapp开发入门

7. 技术交流

区块链技术交流QQ群:348924182进微信群请加微信:liyc1215「区块链部落」官方公众号


企业会员

免责声明:本网站内容主要来自原创、合作伙伴供稿和第三方自媒体作者投稿,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证有关资料的准确性及可靠性,读者在使用前请进一步核实,并对任何自主决定的行为负责。本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。任何单位或个人认为本网站中的网页或链接内容可能涉嫌侵犯其知识产权或存在不实内容时,应及时向本网站提出书面权利通知或不实情况说明,并提供身份证明、权属证明及详细侵权或不实情况证明。本网站在收到上述法律文件后,将会依法尽快联系相关文章源头核实,沟通删除相关内容或断开相关链接。

2017-11-22
【IPFS + 区块链 系列】 入门篇 &#8211; IPFS+IPNS+个人博客搭建
在阅读这篇文章之前,你需要先学习【IPFS + 区块链 系列】 入门篇 - IPFS环境配置这篇文章。

长按扫码 阅读全文

Baidu
map