|
| 1 | +package io.ipfs.api.demo; |
| 2 | + |
| 3 | +import io.ipfs.api.IPFS; |
| 4 | +import io.ipfs.api.NamedStreamable; |
| 5 | +import io.ipfs.api.WriteFilesArgs; |
| 6 | +import io.ipfs.multiaddr.MultiAddress; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +/* |
| 13 | +From MFS api documentation: https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#the-mutable-files-api |
| 14 | +The Mutable File System (MFS) is a virtual file system on top of IPFS that exposes a Unix like API over a virtual directory. |
| 15 | +It enables users to write and read from paths without having to worry about updating the graph. |
| 16 | +
|
| 17 | +Useful links: |
| 18 | +rpc api - https://docs.ipfs.tech/reference/kubo/rpc/#getting-started |
| 19 | +proto.school - https://proto.school/mutable-file-system/01 |
| 20 | +ipfs.tech - https://docs.ipfs.tech/concepts/file-systems/#mutable-file-system-mfs |
| 21 | +
|
| 22 | + */ |
| 23 | +public class UsageMFSFilesAPI { |
| 24 | + |
| 25 | + public UsageMFSFilesAPI(IPFS ipfsClient) { |
| 26 | + try { |
| 27 | + run(ipfsClient); |
| 28 | + } catch (IOException ioe) { |
| 29 | + ioe.printStackTrace(); |
| 30 | + } |
| 31 | + } |
| 32 | + private void run(IPFS ipfs) throws IOException { |
| 33 | + |
| 34 | + // remove 'my' directory to clean up from a previous run |
| 35 | + ipfs.files.rm("/my", true, true); |
| 36 | + |
| 37 | + // To create a new directory nested under others that don't yet exist, you need to explicitly set the value of parents to true |
| 38 | + ipfs.files.mkdir("/my/directory/example", true); |
| 39 | + |
| 40 | + // Check directory status |
| 41 | + String directoryPath = "/my/directory/example"; |
| 42 | + Map exampleDirectory = ipfs.files.stat(directoryPath); |
| 43 | + //{Hash=QmV1a2QoUnB9fPzjZd1GunGR53isuhcWWNCS5Bg3mJyv8N, Size=0, CumulativeSize=57, Blocks=1, Type=directory} |
| 44 | + |
| 45 | + // Add a file |
| 46 | + String contents = "hello world!"; |
| 47 | + String filename = "hello.txt"; |
| 48 | + String filePath = directoryPath + "/" + filename; |
| 49 | + NamedStreamable ns = new NamedStreamable.ByteArrayWrapper(filename, contents.getBytes()); |
| 50 | + ipfs.files.write(filePath, ns, true, true); |
| 51 | + |
| 52 | + // Read contents of a file |
| 53 | + String fileContents = new String(ipfs.files.read(filePath)); |
| 54 | + System.out.println(fileContents); |
| 55 | + |
| 56 | + // Write a file using builder pattern |
| 57 | + String ipfsFilename = "ipfs.txt"; |
| 58 | + String fullIpfsPath = directoryPath + "/" + ipfsFilename; |
| 59 | + NamedStreamable ipfsFile = new NamedStreamable.ByteArrayWrapper(ipfsFilename, "ipfs says hello".getBytes()); |
| 60 | + WriteFilesArgs args = WriteFilesArgs.Builder.newInstance() |
| 61 | + .setCreate() |
| 62 | + .setParents() |
| 63 | + .build(); |
| 64 | + ipfs.files.write(fullIpfsPath, ipfsFile, args); |
| 65 | + |
| 66 | + // List directory contents |
| 67 | + List<Map> ls = ipfs.files.ls(directoryPath); |
| 68 | + for(Map entry : ls) { |
| 69 | + System.out.println(entry.get("Name")); |
| 70 | + } |
| 71 | + |
| 72 | + // Copy file to another directory |
| 73 | + String copyDirectoryPath = "/my/copy/"; |
| 74 | + ipfs.files.cp(filePath, copyDirectoryPath + filename, true); |
| 75 | + ls = ipfs.files.ls(copyDirectoryPath); |
| 76 | + for(Map entry : ls) { |
| 77 | + System.out.println(entry.get("Name")); |
| 78 | + } |
| 79 | + |
| 80 | + // Move file to another directory |
| 81 | + String duplicateDirectoryPath = "/my/duplicate/"; |
| 82 | + ipfs.files.mkdir(duplicateDirectoryPath, false); |
| 83 | + ipfs.files.mv(copyDirectoryPath + filename, duplicateDirectoryPath + filename); |
| 84 | + ls = ipfs.files.ls(duplicateDirectoryPath); |
| 85 | + for(Map entry : ls) { |
| 86 | + System.out.println(entry.get("Name")); |
| 87 | + } |
| 88 | + |
| 89 | + // Remove a directory |
| 90 | + ipfs.files.rm(copyDirectoryPath, true, true); |
| 91 | + ls = ipfs.files.ls("/my"); |
| 92 | + for(Map entry : ls) { |
| 93 | + System.out.println(entry.get("Name")); |
| 94 | + } |
| 95 | + } |
| 96 | + public static void main(String[] args) { |
| 97 | + IPFS ipfsClient = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001")); |
| 98 | + new UsageMFSFilesAPI(ipfsClient); |
| 99 | + } |
| 100 | +} |
0 commit comments