ipfs

How do I get a list of CIDs from IPFS


I have uploaded a list of image to the IPFS, and now I need to get their CIDs to generate json files, upload them to IPFS and get the list of CIDs of the json files again for minting NFTs. Is there a way to batch fetch CIDs of a list of items through IPFS?


Solution

  • List CIDs of files in a folder

    You can see the CIDs of files in a folder by running this: ipfs ls [folder_cid]. If the folder contains many files, you may want to run ipfs ls -s --size=false [folder_cid]. If the folder is more complex and does not only contain files (like it contains other folders which also contain files/folder), you might want to run ipfs refs --recursive; read the docs: ipfs refs --help. Get JSON output of the contents of a folder: ipfs dag get [folder_cid] (can also show info on chunks, if there are multiple, for a specified file CID).

    List all of your pinned CIDs

    By default, when you add data to IPFS by running ipfs add file or ipfs add -r folder, it becomes pinned (more on that later). Make sure to record the CIDs of all of the files that you add, and at least record the final CID if you are adding a folder. Record the CIDs to a text file and/or some places that you will not lose the record, and have the organization to find it and update it. It is important to index your CIDs, because if certain files/folders in your repo become corrupted or inaccessible or I/O-errored (looking at you NTFS) then previously pinned CIDs can show up as not pinned, but they can be repinned from blockstore if you have a text file containing the CIDs.

    Pinned CIDs are protected against garbage collection (gc). Pin local or remote CIDs: ipfs pin add --progress [cid]. Other than looking at a text file which contains your CIDs ("best method"), you can use this command to see pinned CIDs: ipfs pin ls --type=recursive. The --type is set to recursive, which shows the root CIDs of files or folders that you recursively pinned (what stuff is pinned as by default). --type could otherwise be set to direct, only lists directly pinned CIDs where none of its descendant(s) were pinned, "advanced" usage, ignore. --type could otherwise be set to indirect, only lists indirectly pinned CIDs; e.g., if only multiple folders were added, it would only show the CIDs of the files therein and not the CIDs of the folders. It can be otherwise set to all. I've only used --type=recursive to list my pinned CIDs, and I think that is how the other values for --type work.

    Extra info: list non-pinned CIDs

    I know that running ipfs repo gc removes all non-pinned blocks in a repo (BTW, change gc settings in the config file at $IPFS_PATH/config). ipfs repo gc shows a list of the CIDs of the removed blocks. I guess if you ran ipfs repo ls it would show the CIDs of all blocks/objects in a local repo, pinned or not pinned, which should be great! It should show raw block CIDs for everything, which should be helpful in getting IDs for all of the data in your repo.