前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IPFS星际文件系统之-- 环境配置

IPFS星际文件系统之-- 环境配置

作者头像
若与
发布2018-10-18 11:13:28
1K0
发布2018-10-18 11:13:28
举报

1. IPFS简介

IPFS(InterPlanetary File System)是一个点对点的分布式超媒体分发协议,它整合了过去几年最好的分布式系统思路,为所有人提供全球统一的可寻址空间,包括Git、自证明文件系统SFSBitTorrentDHT,同时也被认为是最有可能取代HTTP的新一代互联网协议。

IPFS用基于内容的寻址替代传统的基于域名的寻址,用户不需要关心服务器的位置,不用考虑文件存储的名字和路径。我们将一个文件放到IPFS节点中,将会得到基于其内容计算出的唯一加密哈希值。哈希值直接反映文件的内容,哪怕只修改1比特,哈希值也会完全不同。当IPFS被请求一个文件哈希时,它会使用一个分布式哈希表找到文件所在的节点,取回文件并验证文件数据。

IPFS是通用目的的基础架构,基本没有存储上的限制。大文件会被切分成小的分块,下载的时候可以从多个服务器同时获取。IPFS的网络是不固定的、细粒度的、分布式的网络,可以很好的适应内容分发网络的要求。这样的设计可以很好的共享各类数据,包括图像、视频流、分布式数据库、整个操作系统、模块链、8英寸软盘的备份,还有静态网站。

IPFS提供了一个友好的WEB访问接口,用户可通过http://ipfs.io/hash 获取IPFS网络中的内容,也许在不久的将来,IPFS协议将会彻底替代传统的HTTP协议。

2. IPFS本地环境安装

2.1 下载ipfs压缩包

2.2 安装

代码语言:javascript
复制
 youdi > tar xvfz go-ipfs_v0.4.17_darwin-amd64.tar.gz            
x go-ipfs/build-log
x go-ipfs/install.sh
x go-ipfs/ipfs
x go-ipfs/LICENSE
x go-ipfs/README.md
 youdi > cd go-ipfs                                                                                                             
 youdi       ~/go-ipfs  ./install.sh                                              
Moved ./ipfs to /usr/local/bin

tar xvfz go-ipfs_v0.4.10_darwin-amd64.tar.gz文件解压。

mv ipfs /usr/local/bin/ipfs,将已解压的文件夹中的ipfs文件移动到/usr/local/bin/文件夹中。

我们可以看一下安装脚本的内容:

代码语言:javascript
复制
#!/bin/sh
#
# Installation script for ipfs. It tries to move $bin in one of the
# directories stored in $binpaths.

INSTALL_DIR=$(dirname $0)
# 我的地址是./


bin="$INSTALL_DIR/ipfs"
binpaths="/usr/local/bin /usr/bin"

# This variable contains a nonzero length string in case the script fails
# because of missing write permissions.
is_write_perm_missing=""

for binpath in $binpaths; do
  if mv "$bin" "$binpath/$bin" 2> /dev/null; then
    echo "Moved $bin to $binpath"
    exit 0
  else
    if [ -d "$binpath" -a ! -w "$binpath" ]; then
      is_write_perm_missing=1
    fi
  fi
done

echo "We cannot install $bin in one of the directories $binpaths"

if [ -n "$is_write_perm_missing" ]; then
  echo "It seems that we do not have the necessary write permissions."
  echo "Perhaps try running this script as a privileged user:"
  echo
  echo "    sudo $0"
  echo
fi

exit 1

上面脚本就是执行将ipfs的执行文件mv到/usr/local/bin /usr/bin,后面就是一些权限,其他的等。

3. 项目配置

3.1 创建ipfs节点

为了运行项目,我们需要通过ipfs init在本地计算机建立一个IPFS节点。

代码语言:javascript
复制
 youdi       ~  ipfs init      // init如果没有接参数就是 在 ~/目录下                                                                                                                     0.05   
initializing IPFS node at /Users/youdi/.ipfs
Error: ipfs configuration file already exists!
Reinitializing would overwrite your keys.

如果想自定义目录:

代码语言:javascript
复制
 youdi       ~  ipfs init --help                                                                                                                    0.05   
USAGE
  ipfs init [<default-config>] - Initializes ipfs config file.

SYNOPSIS
  ipfs init [--bits=<bits> | -b] [--empty-repo | -e] [--profile=<profile> | -p] [--] [<default-config>]

ARGUMENTS

  [<default-config>] - Initialize with the given configuration.

OPTIONS

  -b, --bits       int    - Number of bits to use in the generated RSA private key. Default: 2048.
  -e, --empty-repo bool   - Don't add and pin help files to the local storage.
  -p, --profile    string - Apply profile settings to config. Multiple profiles can be separated by ','.

DESCRIPTION

  Initializes ipfs configuration files and generates a new keypair.

  If you are going to run IPFS in server environment, you may want to
  initialize it using 'server' profile.

  For the list of available profiles see 'ipfs config profile --help'

  ipfs uses a repository in the local file system. By default, the repo is
  located at ~/.ipfs. To change the repo location, set the $IPFS_PATH
  environment variable:

      export IPFS_PATH=/path/to/ipfsrepo

只需要在 ipfs init命令之前执行,声明一个IPFS_PATH地址就可以了

代码语言:javascript
复制
$ tree  -L 1
.
├── api
├── blocks
├── config
├── datastore
├── datastore_spec
├── keystore
├── repo.lock
└── version

3 directories, 5 files

我加入了git ,可以直观的看到哪些数据发生了变化。

3.2 修改节点默认存储空间

执行完ipfs init命令后,会在根目录生成一个.ipfs的文件夹存储节点数据。.ipfs节点默认存储空间为10个G

如果你自己想修改节点默认存储空间,可打开终端执行下面的命令。

代码语言:javascript
复制
$ export EDITOR=/usr/local/bin/vim  // 不执行这个会报错  which vim可以获取路径
$ ipfs config edit

执行完ipfs config edit命令后会打开一个文件,将10GB修改成你自己想要的存储空间。修改完毕,保存退出。 保存后,使用 git status 可以看到修改的文件是 config,所以我们也可以直接vim config进行编辑。

3.3 查看节点id

代码语言:javascript
复制
$ ipfs id 
{
    "ID": "QmZcCwaajszaMjHsYTGXmTrdFFmrMxNJHaMrhAYFXqFLuN",
    "PublicKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDQuECmgjSqKRQRqKGpVs0LYTmGoZkGbEryipPmukaIP4Ker4gCOWkOgafx7WO5YVS0wscmVkupGj6an0oH1frI8De7GqR4K/WAp8TjOGTgIf8Ec87KLKXj3KtYlatTpZ/Lur9U8XGSTYHjs0yscXoaEBb1E8qhHGI9jb3l16rB36s4HuTe5ZI4J5o3UuWgXpNr5l4i2e1bN1Hqv3yymSewdOKDFFpglXK/40GdMEMUnsRJb3l1zle8AJ/tSsYndL4A8BFuzNZHoonXQD4sQs9iZjVtO4xPP/K5kKGWB+oYN8j65rgTTw08R4YhpRbeQUVX2gV2S9+SMMbckxoPHKs1AgMBAAE=",
    "Addresses": [
        "/ip4/127.0.0.1/tcp/4001/ipfs/QmZcCwaajszaMjHsYTGXmTrdFFmrMxNJHaMrhAYFXqFLuN",
        "/ip4/192.168.1.101/tcp/4001/ipfs/QmZcCwaajszaMjHsYTGXmTrdFFmrMxNJHaMrhAYFXqFLuN",
        "/ip6/::1/tcp/4001/ipfs/QmZcCwaajszaMjHsYTGXmTrdFFmrMxNJHaMrhAYFXqFLuN",
        "/ip4/10.0.0.14/tcp/43831/ipfs/QmZcCwaajszaMjHsYTGXmTrdFFmrMxNJHaMrhAYFXqFLuN"
    ],
    "AgentVersion": "go-ipfs/0.4.17/",
    "ProtocolVersion": "ipfs/0.1.0"
}

QmdKXkeEWcuRw9oqBwopKUa8CgK1iBktPGYaMoJ4UNt1MP为你的节点ID每个节点都会有一个唯一的ID

3.4 启动节点服务器

代码语言:javascript
复制
$ ipfs daemon
Initializing daemon...
Adjusting current ulimit to 2048...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/111.196.241.208/tcp/7723
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/192.168.0.107/tcp/4001
Swarm listening on /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

3.5 跨域资源共享CORS配置

为了后续的开发方便,我们还需要对跨域资源共享( CORS )进行配置,ctrl- c退出ipfs,然后按照下面的步骤进行跨域配置。

  • ctrl- c退出ipfs
  • ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST", "OPTIONS"]'
  • ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'

3.6 验证

  • 启动服务器
代码语言:javascript
复制
$ ipfs daemon
  • 新建终端执行下面的命令
代码语言:javascript
复制
ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
localhost:~ yuechunli$ ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
Hello and Welcome to IPFS!

██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗  ███████╗
██║██╔═══╝ ██╔══╝  ╚════██║
██║██║     ██║     ███████║
╚═╝╚═╝     ╚═╝     ╚══════╝

If you're seeing this, you have successfully installed
IPFS and are now interfacing with the ipfs merkledag!

 -------------------------------------------------------
| Warning:                                              |
|   This is alpha software. Use at your own discretion! |
|   Much is missing or lacking polish. There are bugs.  |
|   Not yet secure. Read the security notes for more.   |
 -------------------------------------------------------

Check out some of the other files in this directory:

  ./about
  ./help
  ./quick-start     <-- usage examples
  ./readme          <-- this file
  ./security-notes
$ 
  • 浏览器输入下面的网址

打开http://localhost:5001/webui会看到一个漂亮的UI界面。

image

生成了哪些文件

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.10.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. IPFS简介
  • 2. IPFS本地环境安装
    • 2.1 下载ipfs压缩包
      • 2.2 安装
      • 3. 项目配置
        • 3.1 创建ipfs节点
          • 3.2 修改节点默认存储空间
            • 3.3 查看节点id
              • 3.4 启动节点服务器
                • 3.5 跨域资源共享CORS配置
                  • 3.6 验证
                    • 生成了哪些文件
                    相关产品与服务
                    内容分发网络 CDN
                    内容分发网络(Content Delivery Network,CDN)通过将站点内容发布至遍布全球的海量加速节点,使其用户可就近获取所需内容,避免因网络拥堵、跨运营商、跨地域、跨境等因素带来的网络不稳定、访问延迟高等问题,有效提升下载速度、降低响应时间,提供流畅的用户体验。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档