RPC API Read & Writing Files

Hello,

I would like to build a Python component which can read / write files via RPC API. I have found two different methods

and

I use at the moment the add call, but I cannot read the file content. Can anybody explain why there are two call to read / write files? The structure of the arguments are different, but what is the semantic difference of the mthods and which one should I use for my use-case? Can I mix the two api definition, but at the moment the add is working but if I try to read the file I get the error that the path does not exist(so the file not exists)

My files are text files and are small (some kilobytes).

Thanks for help

A simple solution for you would be to add a file with /add endpoint, which generates a Content Identifier (CID) to the file, which in turn can be used to get the file content using the /get endpoint.

IPFS doesn’t have the concept of writing because every file when added is hashed to generate a CID so when you change a file and add it to IPFS it will be hashed again which will produce a new CID, thus becoming a new file to IPFS.

The second pair of links you sent are from what is called Mutable File System (MFS), which consists in mounting a File System (FS) in your machine, exposing the
FS interface we are used to and in the background turns those to other IPFS calls such as add and get. For example, when you write to a file in MFS, IPFS will automatically do that call to /add, and when you read the file it will also do the calls for /get.

You can learn more about MFS here and here is another explanation of the difference between the two ways of adding files.

Hoped I could help you ^^

1 Like

Great thanks a lot, in my case the file should be “immutable”, so the add / get calls are a better choide, is this correct?

I try to get the file with vo/get or v0/cat and path the path as “arg” value, but in both cases I get “unknown namespace” for the first element

The files will always be immutable, MFS is mostly handy for a human-readable overview of your data. You can add things to MFS at any time though via files cp <cid> <mfs path>. add/cat will often be what you want.

Sorry I’m not completely clear on what you issue is, this is a minimal example of cat using Python, does this help?

# Using the HTTP API, run cat from a kubo node to retrieve a CID's contents

import requests

# Set the CID of the file you want to retrieve
cid = input("CID to retrieve: ")

# Set the API endpoint
endpoint = 'http://127.0.0.1:8080/api/v0/cat?arg=' + cid

# Make the request
r = requests.get(endpoint)

# Print the response
print(r.text)