Get ipfs hash of file from code

Hello. I’m writing small application that uses ipfs internally. I am launching ipfs node from go code and everything works great. But recently needed to get ipfs hash of file and it appeared to be a problem. I found than calling add command with only hash option might help me, but unfortunately it doesn’t return ipfs hash directly, but prints it directly to stdout using fmt.Fprintf. Maybe somebody knows better way to do it rather than rewriting “add” command in my project or modifying ipfs codebase?

From go, one generally uses the CoreAPI. It’s currently a bit in-flux/in-progress at the moment, but you can construct a CoreAPI node by calling coreapi.NewCoreAPI(myIpfsNode). You can then call myCoreAPI.Unixfs().Add(ctx, someReader)`.

You can avoid actually adding the file to the local datastore by constructing a new IpfsNode with the NilRepo option set to true. See: