upload files to ipfs from command line (using chainsafe)
no metamask needed, no local ipfs daemon needed

bash

1
2
3
4
5
6
pacman -S kubo
pip install pytest-playwright
playwright install
git clone https://github.com/sdushantha/tmpmail.git
cd tmpmail
cp tmpmail ~/.local/bin

chainsafe.py

# python chainsafe.py init
# python chainsafe.py upload <file_path>

from playwright.sync_api import sync_playwright
import subprocess
import time
import sys

def upload(file_path):
  f = open(".chainsafe_key", "r")
  secret_key = f.read()
  f = open(".chainsafe_bucket_id", "r")
  bucket_id = f.read()

  print("uploading to bucket " + bucket_id)

  cid = subprocess.check_output('curl --location --request POST "https://api.chainsafe.io/api/v1/bucket/' + bucket_id + '/upload" --header "Authorization: Bearer ' + secret_key + '" --form \'file=@"' + file_path + '"\' --form \'path="/"\' | jq -r .files_details[0].cid | ipfs cid base32', shell=True).strip().decode('UTF-8')
  print("https://" + cid + ".ipfs.4everland.io")

  return cid

def create_bucket(name):
  f = open(".chainsafe_key", "r")
  secret_key = f.read()
  cmd = 'curl --request POST "https://api.chainsafe.io/api/v1/buckets" --header "Authorization: Bearer ' + secret_key + '" --header "Content-Type: application/json" --data \'{"type": "fps", "name": "' + name + '"}\' | jq -r .id'
  print(cmd)
  bucket_id = subprocess.check_output(cmd, shell=True).strip().decode('UTF-8')

  f = open(".chainsafe_bucket_id", "w")
  f.write(bucket_id)
  f.close()

  print("bucket id " + bucket_id + " written to file .chainsafe_bucket_id")

  return bucket_id

def init():
  email = subprocess.check_output('tmpmail -g', shell=True).strip().decode('UTF-8')
  print("email", email)

  with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://app.storage.chainsafe.io")
    page.locator("form input[name=email]").fill(email)
    page.locator("button[type=submit]").click()

    while True:
      try:
        email_id = subprocess.check_output('tmpmail | grep -oP "^\d+"', shell=True).strip().decode('UTF-8')
      except:
        print("waiting for confirmation email")
        time.sleep(3)
        continue
      break

    verification_code = subprocess.check_output('tmpmail ' + email_id + ' | grep -P "^[a-zA-Z0-9]{7}$"', shell=True).strip().decode('UTF-8')
    print("verification code", verification_code, "\n")

    page.locator("form input").fill(verification_code)
    page.locator("button[type=submit]").click()
    page.locator('[data-cy="api-keys-nav"]').click()

    page.locator('button[data-cy="button-add-api-key"]').click()
    secret_key = page.locator('[data-cy="label-new-key-modal-secret"]').inner_text()

    browser.close()

  f = open(".chainsafe_key", "w")
  f.write(secret_key)
  f.close()

  create_bucket("bucket_1")

  print("secret key saved to .chainsafe_key\n")

  print("create bucket")
  print('python chainsafe.py create_bucket <bucket_name>\n')

  print("upload file")
  print('python chainsafe.py upload <file_path>')

if len(sys.argv) < 2:
  print("usage:")
  print("python chainsafe.py init")
  print("python chainsafe.py upload <file_path>")
elif sys.argv[1] == "upload":
  upload(sys.argv[2])
elif sys.argv[1] == "init":
  init()
elif sys.argv[1] == "create_bucket":
  create_bucket(sys.argv[2])

run

python chainsafe.py init
python chainsafe.py upload <file_path>
Edit
Pub: 04 Jan 2024 01:22 UTC
Edit: 04 Jan 2024 02:41 UTC
Views: 95