IPFS 是一个新鲜事物,虽然早在 2014 年就已经发布,也一直有关注它。直到最近 CloudFlare 建设了 IPFS 的节点后觉得应该写一篇关于 IPFS 的文章了,不然博客咕了 2 个月没更新也不太好。

IPFS 的官方网站在 https://ipfs.io/,在其官方网站上有介绍到其主要特性,其中一个最吸引我的特性是“IPFS removes duplications across the network.”,也就是去重功能。

试想一下 Torrent 下载也是属于 P2P 网络下载,但是其有一个不好的地方就在于文件的任意一个字节被修改,就需要重新做种,这就造成了极大的资源浪费,而 IPFS 天生就可以避免重复的资源浪费。


首先,IPFS 并不存储文件,而是将文件打散后分为一个个“区块”(block),再存储这些区块的内容。而文件则是一个个 block 的 reference,将这些 references 重新拼接在一起就成为了一个可以读取的文件。

而根据这篇问题,IPFS 的区块大小是 256 KB,也如同问题所说,如果将一个文件切割成一半,加些东西进去,IPFS 只会存储另一半没有在网络上出现的部分,已存在的部分不需要重新上传,所以去重成功完成了。


可以到 IPFS 的官方网站下载并安装,Linux 用户可以直接用包管理器安装,能打开本站点的应该都有能力完成这一步,就不多说了。


安装好 IPFS 之后,需要对其进行初始化,然后创建节点私钥之类的,使用 ipfs init 命令即可完成创建。在默认情况下,IPFS 会创建一个 2048-bit RSA 钥匙对,不过你也可以选择自定义长度,在这里我将使用 4096。所以初始化的命令为 ipfs init --bits 4096


ipfs init --bits 4096
initializing IPFS node at /home/yanderemoe/.ipfs
generating 4096-bit RSA keypair...done
peer identity: QmYu**********************************MTLA

其中,“peer identity”为你的节点地址。

运行 IPFS,输入 ipfs daemon 命令来运行一个 IPFS 节点。

$ ipfs daemon
Initializing daemon...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/**.*.*.*/tcp/4001
Swarm listening on /ip4/
Swarm listening on /ip4/**.***.**.***/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit/ipfs/Qm*******************************************MTLA
Swarm announcing /ip4/**.*.*.*/tcp/4001
Swarm announcing /ip4/
Swarm announcing /ip4/**.***.**.***/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/
Gateway (readonly) server listening on /ip4/
Daemon is ready

Daemon is ready 说明了节点运行成功,现在你可以使用 IPFS 获取网络上的资源了。

假设你知道一个 IPFS 资源,想获取它,那么可以使用 ipfs cat 命令。

比如我知道 QmebCzFGo6TnYj8hvexCc2D9xHeFcQXdBu31c5qL5ASJbw 这个 IPFS 资源,就可以使用

ipfs cat QmebCzFGo6TnYj8hvexCc2D9xHeFcQXdBu31c5qL5ASJbw > output.jpg 来下载它,下载的结果会被保存在 output.jpg 中。现在,试试吧。

如果成功了,你应该能看到 output.jpg 是一只可爱的二哈。

如果想上传一个文件,就可以使用 ipfs add 来运行,比如我想上传一个名为“Frustrated Husky.mp4”的文件,就需要执行 ipfs add "Frustrated Husky.mp4"

ipfs add "Frustrated Husky.mp4"
added QmYZo6yQH1bYYYtQ1wvtyfinXufpnKYED18eG4QrFr3DGi Frustrated Husky.mp4
1.86 MiB / 1.86 MiB [=================================================] 100.00%

其中 QmYZo6yQH1bYYYtQ1wvtyfinXufpnKYED18eG4QrFr3DGi 就是这个资源的 IPFS 地址啦,其他人也可以通过这个来访问它。


和添加文件一样,只是需要 ipfs -r folder_name 来 recursive add 资源。

如:ipfs -r moe-site

ipfs add -r moe-site/
added QmYZo6yQH1bYYYtQ1wvtyfinXufpnKYED18eG4QrFr3DGi moe-site/Frustrated Husky.mp4
added QmQeKrEHr9DcCvEJynd3zDzbVAcTmzQFJ9jBrmLDBD9HgT moe-site/Golden Retriever and Cat.mkv
added QmYeie5dNtfsYprQwcMX7H7deH97QUKZQx8PRmSLdwk4WW moe-site
3.35 MiB / 3.35 MiB [=================================================] 99.88%



当你上传了一个资源之后希望别人也能下载,就需要在公共节点或一个服务器上来缓存它,其中比较有名的是 CloudFlare 的 IPFS 节点和 IPFS.io 的节点。






由于每个文件都有唯一的 hash,每次更新都会导致文件 hash 改变,从而 IPFS 的资源地址也会改变。那么该如何动态更新一个资源呢?很不幸,资源没办法被动态更新,但是,你可以更新指向资源的指针。这里就要介绍下 IPNS 的使用了。

回到上面添加目录的例子,从标粗的黑体字可以得知目录的 IPFS 资源名为“QmYeie5dNtfsYprQwcMX7H7deH97QUKZQx8PRmSLdwk4WW”,这就是你的 IPNS 不变的地址了,当你想要更新内容,只需要修改好内容,然后执行

ipfs name publish QmYeie5dNtfsYprQwcMX7H7deH97QUKZQx8PRmSLdwk4WW


是不是很像 git?



  1. 博主,你好。
    我是一个RSS使用者,刚刚发现本博客的RSS Feed( //blog.yandere.moe/feed )由于Cloudflare DDoS保护,RSS阅读器无法抓取。
    博主能否在Cloudflare Page Rule 设置中排除 /feed 这个路由。

  2. 哇塞无意间找到有中文的了,这下就不用翻译机了,看英文好痛苦。这是我第一次在深网留言那。

  3. Hi would you mind sharing which blog platform you’re using?
    I’m planning to start my own blog in the near future but I’m having a tough time making
    a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and
    I’m looking for something unique. P.S My apologies for getting off-topic but I had to ask!

  4. The other day, while I was at work, my sister stole my iphone and tested to see
    if it can survive a 25 foot drop, just so she can be a youtube sensation.
    My iPad is now broken and she has 83 views. I know this is entirely off topic but
    I had to share it with someone!

  5. My spouse and I stumbled over here different web page and thought
    I might as well check things out. I like what
    I see so now i am following you. Look forward to going over your web page
    yet again.

  6. This is the right website for anyone who really wants to find
    out about this topic. You know so much its almost tough to argue with you (not
    that I actually will need to…HaHa). You certainly put a brand new spin on a topic which
    has been discussed for ages. Excellent stuff, just great!

  7. Wow that was strange. I just wrote an incredibly long comment
    but after I clicked submit my comment didn’t appear. Grrrr…
    well I’m not writing all that over again. Anyhow, just wanted to say wonderful

  8. Greetings, I do believe your website could possibly be having browser compatibility issues.
    Whenever I look at your blog in Safari, it looks fine however, when opening in Internet Explorer, it has some overlapping issues.

    I just wanted to provide
    you with a quick heads up! Besides that, excellent site! https://entreprisesmst.com

  9. When I initially commented I clicked the “Notify me when new comments are added” checkbox and
    now each time a comment is added I get several emails with the same comment.
    Is there any way you can remove me from that service?
    Bless you!

  10. Greetings! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest authoring
    a blog post or vice-versa? My website covers a lot of the same subjects as yours and I think we
    could greatly benefit from each other. If you might be interested feel free to
    shoot me an e-mail. I look forward to hearing from you! Excellent blog by
    the way!

  11. Excellent post. I used to be checking continuously
    this blog and I’m impressed! Very useful info particularly the final part 🙂 I maintain such information much.
    I used to be looking for this particular info for a very lengthy time.
    Thanks and good luck.


Leave a Reply

Your email address will not be published.