ipfs-shipyard / java-ipfs-http-client

A Java implementation of the HTTP IPFS API
MIT License
538 stars 243 forks source link

How do I connect to the public IPFS gateway of Infura instead of using a local daemon? #115

Closed ghost closed 5 years ago

ghost commented 5 years ago

Infura specifies these two IPFS endpoints: IPFS Gateway: https://ipfs.infura.io/ipfs/ IPFS API: https://ipfs.infura.io:5001/api/

What exactly do I need to change in the IPFS instance declaration (IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/5001");) to add and get files from the Infura public gateway? I cannot use a local node that needs to constantly be running, since it will be a deployed application...I have tried many approaches but none have worked, and I cannot find ANY examples of this being done in Java. All help is appreciated.

gjeanmart commented 5 years ago

In theory, you should be able to do IPFS ipfs = new IPFS(" /dnsaddr/ipfs.infura.io/tcp/5001/https");

But it doesn't work because java-ipfs currently has java-multiaddres:1.3.0 as dependency and /dnsaddr/ isn't supported by this version. I submitted the PR #118 to upgrade java-multiaddres to 1.3.1. Once merged and released, it should work !

ghost commented 5 years ago

@gjeanmart Thank you so much for your answer! I can instantiate the MultiAddress when additionally injecting the java-multiaddress:1.3.1 dependency, but of course with java-ipfs it doesn't work yet. Do you have any idea on when the version with this change will be released?

gjeanmart commented 5 years ago

The PR has been merged to master last night ! I have no idea what's flow to release new stuff.

CC @ianopolous

ghost commented 5 years ago

Might be a dumb question, but is is possible to get the Java IPFS API with this change without waiting for a new version? Does it work if I clone the repository to my working directory?

gjeanmart commented 5 years ago

Yeah there is different way to do this, you can:

  1. Clone the repo on your machine git clone https://github.com/ipfs/java-ipfs-api.git
  2. Change the version in pom.xml to v1.2.3-SNAPSHOT for example:
    <groupId>com.github.ipfs</groupId>
    <artifactId>java-ipfs-api</artifactId>
    <version>v1.2.3-SNAPSHOT</version>
    <packaging>jar</packaging>
  3. Run the command: mvn clean install -DskipTests to compile, build, package and copy the jar in your local maven repository.
  4. In your project, change the IPFS dependency to version v.1.2.3-SNAPSHOT
        <dependency>
            <groupId>com.github.ipfs</groupId>
            <artifactId>java-ipfs-api</artifactId>
            <version>v.1.2.3-SNAPSHOT</version>
        </dependency>

    So when the new release v1.2.3 will be ready, you would only have to remove the -SNAPSHOT.

Voila!

ghost commented 5 years ago

Hi @gjeanmart thank you very much, I have it now. Unfortunately, I found that using an IPFS instance still didn't work. Same as before, it was loading infinitely. After some debugging I found the error. In the constructor IPFS(String host, int port, String version, boolean ssl), the Version.parse() method is given the variable with parentheses after it (see below), which does not throw an exception but causes an invisible crash. Do you want me to fix this?

this.version = version; .. Version detected = Version.parse(this.version());

gjeanmart commented 5 years ago

I don't see any problem with the code on master:

    @Test
    public void connection() throws IOException {
        IPFS ipfs = new IPFS("/dnsaddr/ipfs.infura.io/tcp/5001/https");
        System.out.println("connected");
        System.out.println("id: " + ipfs.id());
    }
connected
id: {ID=QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, PublicKey=CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDrasCezcDVRWo2jydDHji6WlYI6/XIr7j9H2XNVhAO9EM+L9qGCvbQ5TZIISCWyXe6mM/slbJzk/Kff/kU26fy9hEn1CO2SmVxLE5boFGKrPualAH/vjkYUUmVEn15pjUwgN2/Q+lRT1ffnlZcj9v1ujwCgBjq1SW8BgdFuGsTyQw5jEjPs5aWe3OqG9v6BMI20y9wLh7IPvINYkLUxUsi0UOLRYUfeUgFVmjifPfnU8yuPALVzOOd+Dc8SYrC/ltlt9T85/iUb1j23hWR+LFD1xWY09TEjJ32J1w2NedM2Lg/R1MEEzS96bB9TJahEZHaIZcEBDxXaTwDbRGJvD5NAgMBAAE=, Addresses=[/ip4/127.0.0.1/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/10.0.20.97/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/172.17.0.1/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/54.211.239.108/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/10.0.20.97/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/10.128.0.3/tcp/30361/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk], AgentVersion=go-ipfs/0.4.17/, ProtocolVersion=ipfs/0.1.0}
ghost commented 5 years ago

Thank you infinitely, everything works now. I just didn't have the newest source files. You saved my project!

gjeanmart commented 5 years ago

:metal:

amrouchk commented 5 years ago

Hello, sorry if this is too basic but I couldnt run the example shown in readme. I tried this IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/5001"); with this ipfs.refs.local(); but it doesnt work also this one doesnt work with me @Test public void connection() throws IOException { IPFS ipfs = new IPFS("/dnsaddr/ipfs.infura.io/tcp/5001/https"); System.out.println("connected"); System.out.println("id: " + ipfs.id()); } I installed ipfs-go and the daemon is running so everything seems good Any helps?? appreciates 1 2 screenshot from 2018-11-29 00-22-26

kuabhish commented 4 years ago

I don't see any problem with the code on master:

    @Test
    public void connection() throws IOException {
        IPFS ipfs = new IPFS("/dnsaddr/ipfs.infura.io/tcp/5001/https");
        System.out.println("connected");
        System.out.println("id: " + ipfs.id());
    }
connected
id: {ID=QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, PublicKey=CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDrasCezcDVRWo2jydDHji6WlYI6/XIr7j9H2XNVhAO9EM+L9qGCvbQ5TZIISCWyXe6mM/slbJzk/Kff/kU26fy9hEn1CO2SmVxLE5boFGKrPualAH/vjkYUUmVEn15pjUwgN2/Q+lRT1ffnlZcj9v1ujwCgBjq1SW8BgdFuGsTyQw5jEjPs5aWe3OqG9v6BMI20y9wLh7IPvINYkLUxUsi0UOLRYUfeUgFVmjifPfnU8yuPALVzOOd+Dc8SYrC/ltlt9T85/iUb1j23hWR+LFD1xWY09TEjJ32J1w2NedM2Lg/R1MEEzS96bB9TJahEZHaIZcEBDxXaTwDbRGJvD5NAgMBAAE=, Addresses=[/ip4/127.0.0.1/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/10.0.20.97/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/172.17.0.1/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/54.211.239.108/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/10.0.20.97/tcp/4001/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk, /ip4/10.128.0.3/tcp/30361/ipfs/QmWTPu7cq5dwTQTLd6FPxHbeVfin6F4HaVpXfQPh1CjiZk], AgentVersion=go-ipfs/0.4.17/, ProtocolVersion=ipfs/0.1.0}

ipfs.id() didn't work for me ...

I was getting this error:

java.lang.RuntimeException: IOException contacting IPFS daemon.
    Trailer: null ipfs method not allowed
gjeanmart commented 4 years ago

The error is due to the fact that the IPFS provider Infura forbids the method id of the API. It wasn't the case before (they can choose to restrict the API as they wish).

To illustrate:

$ curl -X POST "https://ipfs.infura.io:5001/api/v0/id"
ipfs method not allowed

But retrieving a file still works

$ curl -X POST "https://ipfs.infura.io:5001/api/v0/cat?arg=QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u"
Hello World
kuabhish commented 4 years ago

Understood @gjeanmart