How to Upload Files on Arweave With Python

Throughout history, the need to store information has been crucial, both for individuals and organizations. Traditionally, paper documents and photographs were kept in physical folders and secured at home. The invention of computers revolutionized storage, transitioning from floppy disks to CDs, USBs, hard drives, and finally, solid-state drives. Recognizing the growing demand for data storage, the internet embraced cloud services, which emerged rapidly like mushrooms. Massive data centers, leveraging their hardware capabilities, began offering seemingly limitless storage for a fee.

Cloud storage has become increasingly popular, seemingly offering greater convenience and ease of use compared to traditional methods like hard drives. While the convenience is undeniable, the assumption of inherent safety in cloud drives is not always accurate, even with reputable providers like Google. I personally experienced data loss when a cloud service I used shut down. Thankfully, the data wasn't critical, but it serves as a reminder that some level of caution is necessary.

Centralized cloud storage faced a critical challenge: ensuring permanent data availability. Decentralized storage solutions emerged to address this concern. Platforms like IPFS, for example, create a network of interconnected devices (nodes) that share the responsibility of storing data. Similar to file-sharing torrents, IPFS distributes information across these nodes, resembling the principles of blockchain technology. This distributed architecture guarantees the permanence and accessibility of data, making it a crucial element for storing NFTs. Without such permanence, the value of an NFT as a unique digital asset would be significantly undermined.

Recently, Arweave emerged as a competitor to IPFS in the permaweb space, offering an alternative approach to permanent data storage. Unlike IPFS, Arweave leverages a "blockchain-like architecture" to ensure data persistence. Through its unique SPoRA (Succinct Proofs of Random Access) protocol, it incentivizes miners with the native $AR token. Additionally, Arweave provides developers with an API and SDK, enabling them to build decentralized applications (dApps) and cloud platforms like Akord and Ardrive.

In the following topics, we’ll learn how to create an Arweave wallet, get some $AR native token on it to pay for permanent storage, and navigate a Python script to seamlessly upload files and retrieve them.

1 - Create an Arweave wallet

Creating an Arweave wallet is straightforward. You just need to head to the following website:

You can import a keyfile, or create a new wallet with the option to store it in the device you’re using as a vault.

Wallet creation steps
Wallet creation steps

Once created, you’ll be able to trace any transaction and file upload on this platform.

Transaction traceability in Arweave wallet
Transaction traceability in Arweave wallet

We’ll now take a look at how to acquire $AR tokens and fund your wallet.

2 - Get $AR tokens in your wallet

The $AR native token is the fuel to make transactions and power Dapps in the Arweave ecosystem. To acquire it, most of the options rely on using centralized exchanges and KYC. Fortunately, I was able to find an exchange that allows one to swap $ETH for $AR and send it to your Arweave wallet.

The exchange is called change now, and there’s no need for registration. You can simply connect your Ethereum or Bitcoin wallet and swap for $AR.

Exchange $ETH for $AR in at change now
Exchange $ETH for $AR in at change now

In the example above, I’m swapping $ETH for $AR but you can select among many other tokens. As for the recipient's wallet, put the wallet address of the Arweave wallet created previously.

3 - Upload files to Arweave and retrieve them

As mentioned before there are several storage solutions out there, that allow you to upload files permanently in a way that feels very similar to what Cloud Drives do.

For developers seeking to integrate Arweave into their projects or upload files programmatically, extensive documentation is available. However, it primarily utilizes JavaScript and Command Line Interface (CLI) tools. For Pythoneers, I was able to find a GitHub repository by Mike Hibbert who created a Python client for Arweave operations.

I won’t cover all the documentation here, only the commands that allow one to upload and retrieve data. Let’s start by installing the package:

pip install arweave-python-client

Before getting into the script, we need to download our wallet credentials as a JSON file and use them in our script. For that, in your Arweave wallet app settings you’ll find the option to backup the keyfile:

Backup keyfile in settings
Backup keyfile in settings

This will download a CUSTOMIZATION file, but you can easily change it for JSON to be read by the script. Once done, let’s create a Python script and start by importing the following dependencies:

import os
import arweave
from arweave.arweave_lib import Transaction
from arweave.transaction_uploader import get_uploader

Now let’s make a simple function that allows you to choose the type of format you want to upload to Arweave, display the Transaction IDs that are being created and control your wallet balance every time you run it.

def upload_arweave_files(credentials_path, files_path, logger, format='png'):
    """This function uses the arweave package to upload files."""
    wallet = arweave.Wallet(credentials_path)
    logger.info("Balance of $AR: %s", wallet.balance)
    files = os.listdir(files_path)
    tx_ids = {}
    for file in files:
        with open(f"{files_path}/{file}", "rb", buffering=0) as file_handler:
            tx = Transaction(
                wallet,
                file_handler=file_handler,
                file_path=f"{files_path}/{file}")
            tx.add_tag('Content-Type', f"application/{format}")
            tx.sign()

            uploader = get_uploader(tx, file_handler)

            while not uploader.is_complete:
                uploader.upload_chunk()

            logger.info("FILE: %s", file)
            logger.info("TRANSACTION: %s\n", f"https://arweave.net/{tx.id}")
            tx_ids[tx.id] = file

    logger.info("All transaction IDs: %s", tx_ids)
    return tx_ids

Briefly, this function takes the wallet credentials downloaded previously, the path of the files you want to upload, a logger in case you want to save the information in a log file and finally the format as inputs. The function returns a dictionary mapping transaction IDs to corresponding file names. This allows you to utilize the data for further actions or store it in a database for future reference.

Conclusion

Storing an image of 250 KB cost only about $0.01 USD (equivalent to 0.0002 $AR at the time of writing). However, it's important to remember that larger files can be significantly more expensive. This cost factor is crucial when deciding which files are suitable for permanent storage on Arweave. Videos, for example, can quickly become expensive due to their size. Additionally, as Arweave's adoption grows, the price of the $AR token is likely to increase, potentially impacting future storage costs.

Despite the cost considerations, Arweave remains an innovative project with the potential to revolutionize internet operations. Many web3 projects already utilize IPFS or Arweave for storage, and broader adoption across web2 seems likely in the future.

Subscribe to Marco Rodrigues
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.