> For the complete documentation index, see [llms.txt](https://docs.nodecattel.xyz/node-guides/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nodecattel.xyz/node-guides/quick-start/zerogravity-0g/0g-storage-node-setup-guide/0g-storage-cli.md).

# 0G Storage CLI

Storage CLI is a client tool that interact with Storage node

#### Build Storage CLI with source code

```bash
git clone https://github.com/0glabs/0g-storage-client.git
cd 0g-storage-client
go build
```

#### Create a Test file for uploading with storage CLI

```sh
cd $HOME/0g-storage-client
./0g-storage-client gen
# this command will create a test file "tmp123456" in your location
# add --file <file-name> to get a specific file name.filetype
```

#### Prepare your environment for CLI

```bash
STORAGE_PORT=$(grep -oP '(?<=rpc_listen_address = "0.0.0.0:)\d+(?=")' $HOME/0g-storage-node/run/config.toml)
STORAGE_RPC_ENDPOINT=http://$(wget -qO- eth0.me):$STORAGE_PORT
BLOCKCHAIN_RPC_ENDPOINT=$(sed -n 's/blockchain_rpc_endpoint = "\([^"]*\)"/\1/p' $HOME/0g-storage-node/run/config.toml)
LOG_CONTRACT_ADDRESS=$(sed -n 's/log_contract_address = "\([^"]*\)"/\1/p' $HOME/0g-storage-node/run/config.toml)
MINE_CONTRACT_ADDRESS=$(sed -n 's/mine_contract_address = "\([^"]*\)"/\1/p' $HOME/0g-storage-node/run/config.toml)
JSON_PORT=$(sed -n '/\[json-rpc\]/,/^address/ s/address = "0.0.0.0:\([0-9]*\)".*/\1/p' $HOME/.0gchain/config/app.toml)
JSON_RPC_ENDPOINT=http://$(wget -qO- eth0.me):$JSON_PORT
echo -e "STORAGE_RPC_ENDPOINT: $STORAGE_RPC_ENDPOINT\nLOG_CONTRACT_ADDRESS: $LOG_CONTRACT_ADDRESS\nMINE_CONTRACT_ADDRESS: $MINE_CONTRACT_ADDRESS\nBLOCKCHAIN_RPC_ENDPOINT: $BLOCKCHAIN_RPC_ENDPOINT\nJSON_RPC_ENDPOINT: $JSON_RPC_ENDPOINT"
```

#### Extract private key and add to environment temporary

```bash
PRIVATE_KEY=$(0gchaind keys unsafe-export-eth-key $WALLET_NAME)
echo $PRIVATE_KEY
```

#### Upload the test file with storage CLI

Change the file name to upload any other file ( --file \<file-name>)

Your node will submit a the file upload and you will need to `WAIT` until the file complete its process&#x20;

{% code overflow="wrap" %}

```bash
# upload test file generated
cd $HOME/0g-storage-client

./0g-storage-client upload \
--url $BLOCKCHAIN_RPC_ENDPOINT \
--contract $LOG_CONTRACT_ADDRESS \
--key $PRIVATE_KEY \
--node $STORAGE_RPC_ENDPOINT \
--file tmp123456
```

{% endcode %}

Result example

```bash
./0g-storage-client upload --url $BLOCKCHAIN_RPC_ENDPOINT --contract $LOG_CONTRACT_ADDRESS --key $PRIVATE_KEY --node $STORAGE_RPC_ENDPOINT --file tmp123456
INFO[2024-06-07T18:13:07+07:00] Data prepared to upload                       chunks=9342 segments=10 size=2391454
INFO[2024-06-07T18:13:07+07:00] create segment root took                      duration=8.434873ms
INFO[2024-06-07T18:13:07+07:00] Data merkle root calculated                   root=0xac0bd9d83e593ec52dd5fe9d68b779c0bec25e820b501489567a9c5448971cef
INFO[2024-06-07T18:13:07+07:00] create submission nodes took                  duration=10.073086ms
INFO[2024-06-07T18:13:07+07:00] Succeeded to send transaction to append log entry  hash=0x3dcdb69c8265edd868a1d3c48dfed95cb4e8db33b5a6dd99ffabd4d572e2525a
INFO[2024-06-07T18:13:16+07:00] Wait for log entry on storage node            finality=false root=0xac0bd9d83e593ec52dd5fe9d68b779c0bec25e820b501489567a9c5448971cef
WARN[2024-06-07T18:14:17+07:00] Log entry is unavailable yet                  txBlockNumber=471526 zgsNodeSyncHeight=471523
INFO[2024-06-07T18:14:32+07:00] Begin to upload file                          disperse=false nodeNum=1 segIndex=0
INFO[2024-06-07T18:14:32+07:00] Completed to upload file                      duration=254.870242ms segNum=10
INFO[2024-06-07T18:14:32+07:00] Wait for log entry on storage node            finality=true root=0xac0bd9d83e593ec52dd5fe9d68b779c0bec25e820b501489567a9c5448971cef
INFO[2024-06-07T18:14:33+07:00] upload took                                   duration=1m25.932382179s
```

*Tips make note of your root hash received from the upload log*

```bash
root=0xac0bd9d83e593ec52dd5fe9d68b779c0bec25e820b501489567a9c5448971cef
```

#### &#x20;Download the test file with storage CLI

We will download the uploaded file by referring to its `file_root_hash` that we see in our upload log

```bash
# set file_root_hash you want to download to environment
root=0xac0bd9d83e593ec52dd5fe9d68b779c0bec25e820b501489567a9c5448971cef
```

{% code overflow="wrap" %}

```bash
# download test file uploaded
cd $HOME/0g-storage-client

./0g-storage-client download \
--node $STORAGE_RPC_ENDPOINT \
--root $root \
--file tmp123456_dl
```

{% endcode %}

Result example - you will see the file downloaded in your location as `tmp123456_dl`

```bash
INFO[2024-06-08T00:11:47+07:00] Begin to download file from storage node      threads=1
INFO[2024-06-08T00:11:47+07:00] Completed to download file                   
INFO[2024-06-08T00:11:47+07:00] create segment root took                      duration=11.36477ms
INFO[2024-06-08T00:11:47+07:00] Succeeded to validate the downloaded file    
```
