car

package module
v0.0.0-...-ffc2019 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 25 Imported by: 1

README

go-ipfs-car

go-ipfs-car is a standalone library for packing files into a Content Addressable aRchives (CAR) file. The library can be used to generate .car file and upload the packaged data into IPFS all at once.

Features

  • Light-weight in-memory operation without bootstraping Kubo core.
  • Seamless integration with IPFS network
  • Support Car v1 and v2

Getting Started

Installation
go get -u github.com/photon-storage/go-ipfs-car
Example

Packing Car v1 format:

package main

import (
	"bytes"
	"context"
	"fmt"

	car "github.com/photon-storage/go-ipfs-car"
)

func main() {
	b := car.NewBuilder()
	v1car, err := b.Buildv1(
		context.TODO(),
		"./data",
		car.ImportOpts.CIDv1(),
	)
	if err != nil {
		fmt.Printf("Error creating car v1 builder: %v", err)
		return
	}

	v1buf := bytes.Buffer{}
	if err := v1car.Write(&v1buf); err != nil {
		fmt.Printf("Error writing out car v1 format: %v", err)
		return
	}

	fmt.Printf("Car v1 generated, CID =%v, size = %v\n", v1car.Root(), v1buf.Len())
}

Packing Car v2 format:

package main

import (
	"bytes"
	"context"
	"fmt"

	car "github.com/photon-storage/go-ipfs-car"
)

func main() {
	b := car.NewBuilder()

	ch := make(chan *car.ImportEvent, 8)
	go func() {
		for v := range ch {
			fmt.Printf("New block built, CID = %v\n", v.CID)
		}
		fmt.Printf("Done building\n")
	}()

	v2car, err := b.Buildv2(
		context.TODO(),
		"./data",
		car.ImportOpts.CIDv1(),
		car.ImportOpts.Events(ch),
	)
	if err != nil {
		fmt.Printf("Error creating car v2 builder: %v", err)
		return
	}

	v2buf := bytes.Buffer{}
	if _, err := v2car.WriteTo(&v2buf); err != nil {
		fmt.Printf("Error writing out car v2 format: %v", err)
		return
	}

	fmt.Printf("Car v2 generated, CID =%v, size = %v\n", v2car.Root(), v2buf.Len())
}

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMhType           = errors.New("invalid multihash type")
	ErrIncompactibleCidVersion = errors.New("incompactible CID version")
)
View Source
var (
	ErrNoopExchgNotFound = errors.New("not found in noop exchange")
)
View Source
var ImportOpts importScope

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder builds a WriterTo from the given input source. The WriterTo can be used to output car format data to a io.Writer.

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates a new car builder.

func (*Builder) Buildv1

func (b *Builder) Buildv1(
	ctx context.Context,
	input any,
	opts ...ImportOption,
) (*CarV1, error)

Buildv1 builds a CarV1 for outputing car v1 format data.

func (*Builder) Buildv2

func (b *Builder) Buildv2(
	ctx context.Context,
	input any,
	opts ...ImportOption,
) (*CarV2, error)

Buildv2 builds a CarV2 for outputing car v2 format data.

type CarV1

type CarV1 struct {
	// contains filtered or unexported fields
}

func (*CarV1) Root

func (c *CarV1) Root() cid.Cid

func (*CarV1) Write

func (c *CarV1) Write(w io.Writer) error

type CarV2

type CarV2 struct {
	io.WriterTo
	// contains filtered or unexported fields
}

func (*CarV2) Root

func (c *CarV2) Root() cid.Cid

type DataImporter

type DataImporter struct {
	// contains filtered or unexported fields
}

DataImporter creates a new importer that imports data (can be byte slice, io.Reader or path from local file system) into in-memory dag service.

func NewDataImporter

func NewDataImporter() *DataImporter

NewDataImporter creates a new DataImporter.

func (*DataImporter) Blockstore

func (di *DataImporter) Blockstore() blockstore.Blockstore

func (*DataImporter) Import

func (di *DataImporter) Import(
	ctx context.Context,
	input any,
	opts ...ImportOption,
) (cid.Cid, error)

Import imports the given input.

type ImportEvent

type ImportEvent struct {
	Name  string
	CID   cid.Cid
	Bytes int64
	Size  string
}

type ImportOption

type ImportOption func(*importOptions) error

Directories

Path Synopsis
example
v1
v2

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL