2

Based on the below code, im able to add a file**(dataobj.txt --QmUUHKjR5ZdrrYmxpXyPrYP3eMyhv9FnRKo1Duv9stzmmw)** and publish a file and then assign to the key "myIPFSKey".

Step 1: ipfs add -r ipfs/dataobj.txt
Step 2: ipfs name publish QmUUHKjR5ZdrrYmxpXyPrYP3eMyhv9FnRKo1Duv9stzmmw
Result: Published to QmVHUPh6TJKZpXjQ4gL2rZU5YYi7Eb3cUsh4xvSfMrZfhU: /ipfs/QmUUHKjR5ZdrrYmxpXyPrYP3eMyhv9FnRKo1Duv9stzmmw
Step 3: ipfs name publish --key=myIPFSKey QmVHUPh6TJKZpXjQ4gL2rZU5YYi7Eb3cUsh4xvSfMrZfhU
http://localhost:8080/ipns/QmVHUPh6TJKZpXjQ4gL2rZU5YYi7Eb3cUsh4xvSfMrZfhU

Now i want to update some text on the file(dataobj.txt) and uploaded to the IPFS again. it has returned the new IPFS hash**(QmfKtQh2apaM9eQM3ohwgfrr3zfSSXd1wsQhMJtvXTurUg)**. So again i have published the new hash to the IPFS key "myIPFSKey". so far everything goes well.

Here i want to get the list of all the files published to the particular key(myIPFSKey) like versioning. Is it possible to achieve it?

Command:   ipfs key list myIPFSKey
Expected O/P:  QmUUHKjR5ZdrrYmxpXyPrYP3eMyhv9FnRKo1Duv9stzmmw dataObj.txt
               QmfKtQh2apaM9eQM3ohwgfrr3zfSSXd1wsQhMJtvXTurUg dataObj.txt

Here I'm trying to get both versions of the files from IPFS key(myIPFSKey)

Regards Mani

2 Answers 2

6

I don't think you can use IPNS to store more than one CID (hash) but however you can use IPLD (IPFS Linked Data) to generate a JSON bundle that will reference each version and only the CID of the bundle will be store on IPNS.

Let's do an example:

i. Create a file myfile.txt containing first version and store it on IPFS

$ echo "first version" > myfile.txt 
$ ipfs add myfile.txt 
added QmUCr8K6pNZib5TZ2KAPupdjCqEjpgrm94DJ4UtEQQsuqz myfile.txt

ii. Generate an IPFS bundle referencing this file as version 1

Basically IPLD allows you to store a JSON object on IPFS where leaves can contain data or links.

$ echo '{"version1": {"content": {"/": "QmUCr8K6pNZib5TZ2KAPupdjCqEjpgrm94DJ4UtEQQsuqz"}, "author": "greg"}}' | ipfs dag put
zdpuAqidKVQqA36feYLTLyWV9s4eajMtSmVFJxWpa65o5VFPU

Then we can explore the IPLD bundle like this

o Get the entire bundle

$ ipfs dag get zdpuAqidKVQqA36feYLTLyWV9s4eajMtSmVFJxWpa65o5VFPU
{"version1":{"author":"greg","content":{"/":"QmUCr8K6pNZib5TZ2KAPupdjCqEjpgrm94DJ4UtEQQsuqz"}}}

o Get a specific data within the object

$ ipfs dag get zdpuAqidKVQqA36feYLTLyWV9s4eajMtSmVFJxWpa65o5VFPU/version1/author
"greg"

o Get a specific content through a link

$ ipfs cat zdpuAqidKVQqA36feYLTLyWV9s4eajMtSmVFJxWpa65o5VFPU/version1/content
first version

iii. Store the IPLD hash on IPNS

This IPLD CID has a content addressable hash zdpuAqidKVQqA36feYLTLyWV9s4eajMtSmVFJxWpa65o5VFPU like any other objects on IPFS and can be stored on IPNS

o Generate the key key

$ ipfs key gen --type=rsa --size=2048 key
QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a

o Publish the IPLD CID under this name

$ ipfs name publish --key=key zdpuAqidKVQqA36feYLTLyWV9s4eajMtSmVFJxWpa65o5VFPU
Published to QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a: /ipfs/zdpuAqidKVQqA36feYLTLyWV9s4eajMtSmVFJxWpa65o5VFPU

o You can then resolve the name and access the version

$ ipfs dag get $(ipfs name resolve QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a)/version1/author
"greg"

$ ipfs cat $(ipfs name resolve QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a)/version1/content
first version

iv. Let's now create a second version and upload it on IPFS

$ echo "second version" > myfile.txt 
$ ipfs add myfile.txt 
added QmNs9wW2s3fVR3aRJS62Ak8honGdB6PEDjF15FnTWnFeu8 myfile.txt

v. We can recreate a new bundle with the second version

$ echo '{"version1":{"author":"greg","content":{"/":"QmUCr8K6pNZib5TZ2KAPupdjCqEjpgrm94DJ4UtEQQsuqz"}}, "version2":{"author":"mani2020","content":{"/":"QmNs9wW2s3fVR3aRJS62Ak8honGdB6PEDjF15FnTWnFeu8"}}}' | ipfs dag put
zdpuAvKvo6QuvQnwtwYhQrsG82iwgGH4bxZpvKY4hrPuKWxaj

And access the data and links

$ ipfs dag get zdpuAvKvo6QuvQnwtwYhQrsG82iwgGH4bxZpvKY4hrPuKWxaj/version1/author
"greg"
$ ipfs cat zdpuAvKvo6QuvQnwtwYhQrsG82iwgGH4bxZpvKY4hrPuKWxaj/version1/content
first version
$ ipfs dag get zdpuAvKvo6QuvQnwtwYhQrsG82iwgGH4bxZpvKY4hrPuKWxaj/version2/author
"mani2020"
$ ipfs cat zdpuAvKvo6QuvQnwtwYhQrsG82iwgGH4bxZpvKY4hrPuKWxaj/version2/content
second version

vi. Finally we only need to republish the new hash of the second bundle

$ ipfs name publish --key=key zdpuAvKvo6QuvQnwtwYhQrsG82iwgGH4bxZpvKY4hrPuKWxaj
Published to QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a: /ipfs/zdpuAvKvo6QuvQnwtwYhQrsG82iwgGH4bxZpvKY4hrPuKWxaj

$ ipfs dag get $(ipfs name resolve QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a)/version1/author
"greg"
$ ipfs cat $(ipfs name resolve QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a)/version1/content
first version
$ ipfs dag get $(ipfs name resolve QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a)/version2/author
"mani2020"
$ ipfs cat $(ipfs name resolve QmPU6xFfHykL7HBNbckzB9pCCeVFUx2HD4KxvkQmcYAV6a)/version2/content
second version

And so on and so forth !


Notes:

  • Pinning: If you pin the IPLD Bundle, all linked data will be pinned.
2
  • Glad it worked ! Please don't forget to accept the answer if it fully solve your problem. Nov 28, 2018 at 12:47
  • for windows command prompt users: step ii: echo {"version1":{"author":"greg","content":{"/":"QmUCr8K6pNZib5TZ2KAPupdjCqEjpgrm94DJ4UtEQQsuqz"}}} | ipfs dag put step v: echo {"version1":{"author":"greg","content":{"/":"QmUCr8K6pNZib5TZ2KAPupdjCqEjpgrm94DJ4UtEQQsuqz"}}, "version2":{"author":"mani2020","content":{"/":"QmNs9wW2s3fVR3aRJS62Ak8honGdB6PEDjF15FnTWnFeu8"}}} | ipfs dag put Mar 28, 2022 at 3:37
0

An alternative to IPLD (and somewhat simpler) is https://www.npmjs.com/package/ipvfs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.