|
| 1 | +package io.ipfs.api.demo; |
| 2 | + |
| 3 | +import io.ipfs.api.IPFS; |
| 4 | +import io.ipfs.api.MerkleNode; |
| 5 | +import io.ipfs.api.NamedStreamable; |
| 6 | +import io.ipfs.multiaddr.MultiAddress; |
| 7 | +import io.ipfs.multihash.Multihash; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +/* |
| 15 | +This sample program demonstrates how to use the remote pinning API methods |
| 16 | +
|
| 17 | +rpc api - https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-pin-remote-add |
| 18 | +
|
| 19 | +setup: |
| 20 | +For demonstration purposes it uses a mock pinning service: |
| 21 | +- https://github.com/ipfs-shipyard/js-mock-ipfs-pinning-service |
| 22 | +Follow the instructions in the README.md file of the above repository for installation |
| 23 | +
|
| 24 | +Sample command to execute before running this program: |
| 25 | +npx mock-ipfs-pinning-service --port 3000 --token secret |
| 26 | +
|
| 27 | +Note: The above parameters are referenced in the program below |
| 28 | + */ |
| 29 | +public class UsageRemotePinningAPI { |
| 30 | + |
| 31 | + public UsageRemotePinningAPI(IPFS ipfsClient) { |
| 32 | + try { |
| 33 | + run(ipfsClient); |
| 34 | + } catch (IOException ioe) { |
| 35 | + ioe.printStackTrace(); |
| 36 | + } |
| 37 | + } |
| 38 | + private void run(IPFS ipfs) throws IOException { |
| 39 | + |
| 40 | + // Add file to the local node |
| 41 | + MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("file.txt", "test data".getBytes())).get(0); |
| 42 | + // Retrieve CID |
| 43 | + Multihash hash = file.hash; |
| 44 | + |
| 45 | + //Add the service |
| 46 | + String serviceName = "mock"; |
| 47 | + ipfs.pin.remote.rmService(serviceName); //clean up if necessary |
| 48 | + ipfs.pin.remote.addService(serviceName, "http://127.0.0.1:3000", "secret"); |
| 49 | + |
| 50 | + //List services |
| 51 | + List<Map> services = ipfs.pin.remote.lsService(true); |
| 52 | + for(Map service : services) { |
| 53 | + System.out.println(service); |
| 54 | + } |
| 55 | + |
| 56 | + // Pin |
| 57 | + Map addHashResult = ipfs.pin.remote.add(serviceName, hash, Optional.empty(), true); |
| 58 | + System.out.println(addHashResult); |
| 59 | + |
| 60 | + // List |
| 61 | + List<IPFS.PinStatus> statusList = List.of(IPFS.PinStatus.values()); // all statuses |
| 62 | + Map ls = ipfs.pin.remote.ls(serviceName, Optional.empty(), Optional.of(statusList)); |
| 63 | + System.out.println(ls); |
| 64 | + |
| 65 | + // Remove pin from remote pinning service |
| 66 | + List<IPFS.PinStatus> queued = List.of(IPFS.PinStatus.queued); |
| 67 | + ipfs.pin.remote.rm(serviceName, Optional.empty(), Optional.of(queued), Optional.of(List.of(hash))); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public static void main(String[] args) { |
| 72 | + IPFS ipfsClient = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001")); |
| 73 | + new UsageRemotePinningAPI(ipfsClient); |
| 74 | + } |
| 75 | +} |
0 commit comments