Can't decrypt file


Hi all .I am trying to get file from ipfs network

Here is my code

var (
		fname  = req.Cid
		output = req.Output

	cID, _ := cid.Parse(fname)

	node, err := core.Node.IpfsNode.DAG.Get(ctx, cID)

	if err != nil {
		return &api.FileResponse{}, err

	data := node.RawData()

	f, err := os.OpenFile(output, os.O_CREATE|os.O_RDWR, 0644)

	if err != nil {
		return &api.FileResponse{}, errors.New(fmt.Sprintf("Error  : %v", err))

	defer f.Close()

	_, err = f.Write(data)

	if err != nil {
		return &api.FileResponse{}, errors.New(fmt.Sprintf("Error  : %v", err))

	return &api.FileResponse{}, nil

But then in the output file either empty or has unknown format!

Please help to solve this problem


Use to construct a “core API” from that IpfsNode instance. Then, with that, you can call Unixfs().Get(context, filepath) to get the file.

At the moment, you’re reading the underlying datastructures that make up an IPFS file (think inodes).


in my case the Unixfs() returns an interface look like this

type UnixfsAPI interface {
	// Add imports the data from the reader into merkledag file
	Add(context.Context, io.Reader) (Path, error)

	// Cat returns a reader for the file
	Cat(context.Context, Path) (Reader, error)

	// Ls returns the list of links in a directory
	Ls(context.Context, Path) ([]*ipld.Link, error)

There is not function called Get


Also I have only file hash like Qm… . How can I parse hash to path ?


Newer versions of this API use Get and return a special “files” object (that can be a either a directory or a file). In your case, use Cat.

You’ll have to call interface-go-ipfs-core.ParsePath.


Thank you very much, I have already solved this problem as you said.