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/127.0.0.1/tcp/4001
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/127.0.0.1/tcp/4001
Swarm announcing /ip4/**.***.**.***/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
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 的节点。

通过:

https://cloudflare-ipfs.com/ipfs/QmYZo6yQH1bYYYtQ1wvtyfinXufpnKYED18eG4QrFr3DGi

https://ipfs.io/ipfs/QmYZo6yQH1bYYYtQ1wvtyfinXufpnKYED18eG4QrFr3DGi

可以使文件被传送至公共节点,这样即使你下线了别人也可以访问到这个文件。

IPNS:

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

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

ipfs name publish QmYeie5dNtfsYprQwcMX7H7deH97QUKZQx8PRmSLdwk4WW

重新发布,这样其他用户就可以看到当前版本的目录了。

是不是很像 git?

试试它吧。

102 Comments

  1. 博主,你好。
    我是一个RSS使用者,刚刚发现本博客的RSS Feed( https://blog.yandere.moe/feed )由于Cloudflare DDoS保护,RSS阅读器无法抓取。
    博主能否在Cloudflare Page Rule 设置中排除 /feed 这个路由。
    另外,非常感谢博主提供这些优秀的文章。

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

    Reply
  3. My partner and I absolutely love your blog and
    find almost all of your post’s to be exactly I’m looking for.

    Would you offer guest writers to write content available for
    you? I wouldn’t mind composing a post or elaborating on some of the subjects you write
    about here. Again, awesome web site!

    Reply
  4. Hey very cool site!! Man .. Excellent .. Wonderful ..
    I will bookmark your blog and take the feeds also…I’m
    happy to search out a lot of helpful information right here in the submit, we want work out extra strategies in this regard, thanks for
    sharing.

    Reply
  5. Hey I am so glad I found your webpage, I really found you by accident, while I
    was researching on Digg for something else, Anyways I am here now and
    would just like to say many thanks for a fantastic post
    and a all round entertaining blog (I also love the theme/design), I don’t have time to
    go through it all at the minute but I have saved
    it and also included your RSS feeds, so when I
    have time I will be back to read a lot more, Please do
    keep up the great job.

    Reply
  6. I simply wanted to thank you yet again for your amazing web page
    you have developed here. It’s full of useful tips for those who are seriously interested in this specific subject, primarily
    this very post. You’re really all so sweet along with thoughtful of others as
    well as reading your website posts is a good delight with me.
    And such a generous gift! Mary and I usually have enjoyment
    making use of your points in what we should do in the future.

    Our collection of ideas is a distance long and tips might be put to great use.

    Reply
  7. We wish to thank you all over again for the wonderful ideas you gave Jesse
    when preparing her post-graduate research and, most importantly, pertaining to providing
    each of the ideas in a blog post. Provided that we had been aware of your web site
    a year ago, we might have been rescued from the nonessential measures we were employing.
    Thank you very much.

    Reply
  8. We are a bunch of volunteers and opening a new scheme in our community.
    Your website offered us with useful info to work on. You’ve
    done a formidable task and our entire community will be thankful to you.

    Reply
  9. I would like to take the opportunity of saying thanks to you for that professional instruction I have constantly enjoyed visiting your site.
    I will be looking forward to the particular commencement of my school research and the overall groundwork
    would never have been complete without consulting this site.
    If I can be of any assistance to others, I will be
    glad to help by means of what I have learned from here.

    Reply
  10. Hey I am so happy I found your blog, I really found you by accident, while I was looking
    on Yahoo for something else, Regardless I am here now and would
    just like to say thank you for a remarkable post and a all round interesting
    blog (I also love the theme/design), I don’t have time to go through it all at the moment but I have book-marked it and also included your RSS feeds, so when I have
    time I will be back to read much more, Please do keep up the awesome work.

    Reply
  11. Howdy very cool blog!! Man .. Beautiful .. Wonderful .. I will bookmark your website and take
    the feeds also? I’m happy to search out a
    lot of helpful information right here within the submit,
    we need develop extra techniques on this regard, thanks for
    sharing. . . . . .

    Reply
  12. Link exchange is nothing else except it is just placing the other person’s
    webpage link on your page at suitable place and other
    person will also do similar in support of you.

    Reply
  13. I intended to compose you this very small remark to help say thanks as before about the
    spectacular suggestions you’ve discussed in this article.

    This is simply generous with you to provide publicly precisely what a few
    people might have supplied as an electronic book to end up making some money for their own end, even more so considering that you might
    have done it in case you decided. These good tips likewise served to be the great way to fully grasp some people have similar desire like my very own to
    see more and more on the subject of this condition. I’m
    certain there are numerous more fun sessions in the future for those who see your website.

    Reply
  14. My brother suggested I might like this website. He was
    totally right. This post truly made my day. You can not imagine just how much
    time I had spent for this info! Thanks!

    Reply
  15. I don’t know whether it’s just me or if perhaps everybody else encountering issues with your blog.
    It appears as if some of the text in your posts are running
    off the screen. Can somebody else please provide feedback and let me know if
    this is happening to them as well? This may be a problem with my web browser because I’ve
    had this happen before. Kudos

    Reply
  16. You can definitely see your skills within the article you write.
    The world hopes for more passionate writers such as you who are not afraid to say how they believe.
    Always follow your heart.

    Reply
  17. Good – I should definitely pronounce, impressed with your site.

    I had no trouble navigating through all tabs as well as related information ended up being truly simple to do to access.
    I recently found what I hoped for before you know it in the least.
    Quite unusual. Is likely to appreciate it for those who add
    forums or anything, web site theme . a tones way for your client
    to communicate. Nice task.

    Reply
  18. Nice post. I learn something totally new and challenging on sites I stumbleupon everyday.
    It will always be interesting to read articles from other authors and
    use something from their websites.

    Reply
  19. With havin so much content and articles do you ever
    run into any issues of plagorism or copyright violation? My site has a
    lot of exclusive content I’ve either created myself or outsourced but it appears a lot of it is popping it up all over the internet without my authorization. Do you know any ways to help reduce content from being ripped off?
    I’d genuinely appreciate it.

    Reply
  20. I truly love your website.. Pleasant colors & theme.
    Did you build this amazing site yourself? Please reply back
    as I’m wanting to create my very own site and want to learn where you got this from or exactly what the theme is
    named. Thanks!

    Reply
  21. Hmm it seems like your blog ate my first comment (it was super long) so I guess I’ll just
    sum it up what I wrote and say, I’m thoroughly
    enjoying your blog. I as well am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any helpful hints for beginner blog writers? I’d certainly appreciate it.

    Reply
  22. I was recommended this blog by means of my cousin.
    I am no longer positive whether this post is written by means of him as nobody else understand such
    distinctive approximately my difficulty.
    You’re amazing! Thank you!

    Reply
  23. Hello very nice website!! Guy .. Beautiful .. Amazing
    .. I will bookmark your website and take the feeds also?
    I am happy to search out a lot of useful info here within the put
    up, we need develop more strategies on this regard, thanks for
    sharing. . . . . .

    Reply
  24. I seldom comment, however i did a few searching and
    wound up here 扫盲IPFS的使用 – Yandere@萌.
    And I actually do have some questions for you if you usually do not mind.
    Could it be just me or does it look like some of the responses
    appear as if they are coming from brain dead visitors? :
    -P And, if you are writing at other social sites, I would like to follow
    anything fresh you have to post. Would you list of every one of all your communal pages like
    your Facebook page, twitter feed, or linkedin profile?

    Reply
  25. hi!,I love your writing very a lot! proportion we keep up a correspondence
    more about your post on AOL? I need an expert on this space to solve my problem.
    May be that’s you! Taking a look forward to look you.

    Reply
  26. Great blog right here! Additionally your website
    quite a bit up very fast! What web host are you the usage of?

    Can I get your associate hyperlink in your host? I wish my web site
    loaded up as fast as yours lol

    Reply
  27. I was curious if you ever thought of changing the page layout of your blog?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so
    people could connect with it better. Youve got an awful lot of text for only having 1 or 2 images.
    Maybe you could space it out better?

    Reply
  28. I think this is among the most important info for me. And i
    am glad reading your article. But want to remark on few general things, The website style is
    great, the articles is really nice : D. Good job, cheers

    Reply
  29. Thanks for the sensible critique. Me and my neighbor were just preparing to do
    a little research on this. We got a grab a book from our local
    library but I think I learned more from this post.
    I’m very glad to see such magnificent info being
    shared freely out there.

    Reply
  30. Greetings! I know this is somewhat off topic but I was
    wondering if you knew where I could get a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

    Reply
  31. I would like to thank you for the efforts you have put in writing this site.
    I’m hoping the same high-grade web site post from you
    in the upcoming as well. In fact your creative writing
    skills has inspired me to get my own blog now. Really the
    blogging is spreading its wings fast. Your write up is a good example of it.

    Reply
  32. Good – I should certainly pronounce, impressed with your web site.
    I had no trouble navigating through all tabs as well as related info ended up being truly
    easy to do to access. I recently found what I hoped for before you know it in the least.
    Quite unusual. Is likely to appreciate it for those who add forums or anything, site theme .
    a tones way for your client to communicate. Excellent task.

    Reply
  33. I was just searching for this info for some time.
    After 6 hours of continuous Googleing, at last I got it in your site.

    I wonder what is the lack of Google strategy that do not rank this kind of informative web sites in top of the list.
    Usually the top web sites are full of garbage.

    Reply

Leave a Reply

Your email address will not be published.

*

code