LoginSignup
4
0

More than 1 year has passed since last update.

【IPFS】ファイルの CID をオフラインで取得する【daemon 起動させずに】

Last updated at Posted at 2021-12-03

CICD などでドキュメントを自動更新する際に、IPFS で公開予定のコンテンツの CID だけを取得したい。できればサーバ負荷の軽減のため daemon の起動はせずに取得したい。

TL; DR (今北産業)

  • --offline --only-hash --quieter オプションを使う(確認: ipfs v0.10.0)
ハッシュ値の計算のみ行う
ipfs add --offline --only-hash --quieter <file path>
実行例(CIDの値のみ表示)
$ ipfs add --offline --only-hash --quieter /path/to/myFile.txt
QmWBf2hYCzFWtQp5w5NLD3oVSdV6nmdvFAdG4KGgsoBMNh

TS; DR

以下は Docker(docker-compose)を使った例です。直接 ipfs add ... を叩いて取得してもいいのですが、GitHub Actions など Docker の入っている環境でゴニョゴニョしたい場合の参考にしてください。

go-ipfs の Docker イメージは 83 MB 程度です。CID 取得のためだけに、ローカルに Go や Kubo(旧 go-ipfs) をインストールするより楽だと思います。

ディレクトリ構成
$ tree
.
├── data
│   └── myFile.txt
├── docker-compose.yml
└── entrypoint.sh
/data/myFile.txt
Hello, Qiita!
docker-compose.yml
version: "3.9"
services:
  getcid:
    image: ipfs/go-ipfs:latest
    volumes:
      - .:/workspaces
    working_dir: /workspaces
    entrypoint: [ "/workspaces/entrypoint.sh" ]
entrypoint.sh(要実行権限)
#!/bin/sh

set -eu

ipfs init 2>/dev/null 1>/dev/null
ipfs add --offline --only-hash --quieter "$1"
実行例
$ docker-compose run getcid ./data/myFile.txt
QmeYx6fTDLj63JWRwYMEAPs9DqGkXKKUDuYeZMuh7SRXj3

参考文献

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0