Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
},
"dependencies": {
"depcheck": "^1.4.3",
"typedoc-plugin-missing-exports": "^2.1.0"
"typedoc-plugin-missing-exports": "^2.1.0",
"@web3-storage/w3cli": "^3.0.1",
"ipfs-car": "^0.9.1"
},
"pnpm": {
"peerDependencyRules": {
Expand Down
6 changes: 6 additions & 0 deletions packages/w3up-python-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.env
*.car
/storacha_uploader.egg-info
/myenv
node_modules

96 changes: 96 additions & 0 deletions packages/w3up-python-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Storacha Uploader

A tool to create CAR files and upload content to Storacha decentralized storage.

## Install

```bash
# Requires Python 3.6+, Node.js and npm
pip install .
```

## Configure

Create a `.env` file with your credentials:
```
X_AUTH_SECRET_HEADER=your_auth_secret
AUTHORIZATION_HEADER=your_authorization
SPACE_DID=your_space_did
```

## Usage

```bash
storacha-uploader myfile.txt
```

This converts the file to CAR format, uploads it to Storacha, and provides an access link.


## Instructions to be used over the http bridge

Below instructions can be used over the http bridge by adding them to the list.json file.

```bash
{
"tasks": [
/*
[
"access/delegate",
"did:key:z6Mkabc123",
{ "delegate": "<delegate_address>" }
],
[
"space/info",
"did:key:z6Mkabc123"
],
[
"space/allocate",
"did:key:z6Mkabc123",
{ "allocation": "<allocation_details>" }
],
[
"store/add",
"did:key:z6Mkabc123",
{ "root": { "/": "<cid>" }, "shards": [] }
],
[
"store/get",
"did:key:z6Mkabc123",
{ "root": { "/": "<cid>" } }
],
[
"store/remove",
"did:key:z6Mkabc123",
{ "root": { "/": "<cid>" } }
],
[
"store/list",
"did:key:z6Mkabc123"
],
*/
[
"upload/add",
"did:key:z6Mkabc123",
{ "root": { "/": "<cid>" }, "shards": [] }
]
/*
,
[
"upload/list",
"did:key:z6Mkabc123",
{}
],
[
"upload/remove",
"did:key:z6Mkabc123",
{ "root": { "/": "<cid>" } }
],
[
"usage/report",
"did:key:z6Mkabc123"
]
*/
]
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit confusing, why not expose these as separate methods on the client?

```
9 changes: 9 additions & 0 deletions packages/w3up-python-client/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tasks": [
[
"upload/list",
"did:key:z6MksbhMHBFohHmhhDNkLVSRr9FgSJRU4rHrP74ggr2EiRt7",
{}
]
]
}
1 change: 1 addition & 0 deletions packages/w3up-python-client/myfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test conversion to car file
3 changes: 3 additions & 0 deletions packages/w3up-python-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
requests
python-dotenv
setuptools
54 changes: 54 additions & 0 deletions packages/w3up-python-client/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import subprocess
import sys
import platform
from setuptools import setup, find_packages

def check_w3cli():
print("🔍 Checking for w3cli...")
try:
subprocess.run(["w3", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("✅ w3cli is already installed.")
return True
except FileNotFoundError:
print("❌ w3cli not found.")
print("📦 Installing w3cli globally...")
try:
subprocess.run("npm install -g @web3-storage/w3cli", shell=True, check=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to be global - can you just add it to the package.json here and install and use it locally?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

print("✅ w3cli installed successfully.")
return True
except subprocess.CalledProcessError as e:
print(f"❌ Failed to install w3cli: {e}")
print("Please install it manually with: npm install -g @web3-storage/w3cli")
return False

w3cli_installed = check_w3cli()

with open('requirements.txt') as f:
requirements = f.read().splitlines()

setup(
name="storacha_uploader",
version="0.1.0",
packages=find_packages(),
install_requires=requirements,
entry_points={
"console_scripts": [
"storacha-uploader=storacha_uploader:main",
],
},
author="Amit Pandey",
author_email="a_pandey1@ce.iitr.ac.in",
description="A tool to automate IPFS DAG creation, CAR file generation, and Web3 Storage upload.",
long_description=open("README.md").read() if sys.version_info[0] >= 3 else "",
long_description_content_type="text/markdown",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.6",
)

print("\n✅ Setup completed.")
print("📝 Note: Make sure Node.js and npm are installed on your system.")
print("🚀 You can now use the 'storacha-uploader' command to upload files to Storacha network.")
136 changes: 136 additions & 0 deletions packages/w3up-python-client/storacha_uploader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import os
import sys
import json
import subprocess
import requests
import tempfile
import platform
from dotenv import load_dotenv

def run_command(command):
try:
result = subprocess.check_output(command, shell=True, text=True).strip()
return result
except subprocess.CalledProcessError as e:
print(f"❌ Error: {e}")
return None

def send_request_to_the_bridge(json_payload, auth_secret, authorization, endpoint):
headers = {
"X_AUTH_SECRET": auth_secret,
"Authorization": authorization,
"Content-Type": "application/json"
}

response = requests.post(endpoint, headers=headers, data=json_payload)

if response.status_code == 200:
response_data = response.json()
upload_url = response_data.get('upload_url')
if not upload_url:
raise ValueError("Upload URL not found in the response.")
return upload_url
else:
raise requests.HTTPError(f"Upload registration failed: {response.status_code} {response.text}")

def upload_car_file(upload_url, car_file_path):
with open(car_file_path, 'rb') as f:
response = requests.put(upload_url, data=f)
if response.status_code not in [200, 201]:
raise requests.HTTPError(f"CAR file upload failed: {response.status_code} {response.text}")

def upload_add(cid):
payload = {
"tasks": [
["upload/add", "did:key:z6Mkabc123", {"root": {"/": cid}, "shards": []}]
]
}
return send_request(payload)


def upload_list():
payload = {
"tasks": [
["upload/list", "did:key:z6Mkabc123", {}]
]
}
return send_request(payload)


def upload_remove(cid):
payload = {
"tasks": [
["upload/remove", "did:key:z6Mkabc123", {"root": {"/": cid}}]
]
}
return send_request(payload)


def send_request(payload,endpoint,headers):
response = requests.post(endpoint, headers=headers, json=payload)
if response.status_code == 200:
return response.json()
else:
raise requests.HTTPError(f"Request failed: {response.status_code} {response.text}")

def main():
load_dotenv()

auth_secret = os.getenv("X_AUTH_SECRET_HEADER")
authorization = os.getenv("AUTHORIZATION_HEADER")
endpoint = os.getenv("HTTPS_ENDPOINT", "https://up.storacha.network/bridge")

if len(sys.argv) < 2:
print("❌ Error: Please provide an operation (upload_add, upload_list, upload_remove) and optional CID.")
print("Usage: python storacha_uploader.py <operation> <CID>")
sys.exit(1)

operation = sys.argv[1]
cid = sys.argv[2] if len(sys.argv) > 2 else None

operations = {
"upload_add": upload_add,
"upload_list": upload_list,
"upload_remove": upload_remove,
}

if operation in operations:
result = operations[operation](cid if "list" not in operation else None)
print(json.dumps(result, indent=2))
else:
print(f"Unknown operation: {operation}")


file_path = sys.argv[1]

space_did = os.getenv("SPACE_DID")
if not space_did:
print("❌ Error: SPACE_DID not found in .env file.")
print("Please add SPACE_DID=your_space_did to your .env file.")
sys.exit(1)

print(f"🔑 Using Space DID: {space_did}")

car_file = f"{file_path}.car"
ipfs_car_cmd = ["ipfs-car", "pack", file_path, "--output", car_file]

try:
result = subprocess.run(ipfs_car_cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cid = result.stdout.decode().strip().split('\n')[-1]
print(f"✅ CAR file created: {car_file} with CID: {cid}")
except subprocess.CalledProcessError as e:
print(f"❌ Error creating CAR file: {e.stderr.decode().strip()}")
sys.exit(1)

upload_url = send_request_to_the_bridge(auth_secret, authorization, endpoint)
upload_car_file(upload_url, car_file)
upload_add(cid)
print(f"✅ File uploaded successfully to Storacha network.")
print(f"✅ Access your file at: https://{cid}.ipfs.w3s.link")
print(f"✅ Response details: {json.dumps(result, indent=2)}")

except Exception as e:
print(f"❌ Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()