storj/examples/eestream/store/main.go
Kaloyan Raev d8f1ec1db6
ECClient (#110)
* WIP ECClient

* Get returns RangeCloser

* Introduce RedundancyStrategy

* Constructor takes max buffer memory

* Remove unnecessary NopCloser wrapper

* Added telemetry

* Tests

* Adapt to PSClient from master

* Decode should report error if empty rrs map is passed

* collectErrors helper

* Move to /pkg/storage

* Move to /pkg/storage/ec

* Rename ecclient.go to client.go

* Better logging

* Rename ec.ECClient to ec.Client

* Fix some test execution

* Adopt Transport Client from master
2018-07-03 11:35:01 +03:00

90 lines
1.8 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"crypto/sha256"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"github.com/vivint/infectious"
"storj.io/storj/pkg/eestream"
)
var (
pieceBlockSize = flag.Int("piece_block_size", 4*1024, "block size of pieces")
key = flag.String("key", "a key", "the secret key")
rsk = flag.Int("required", 20, "rs required")
rsn = flag.Int("total", 40, "rs total")
)
func main() {
flag.Parse()
if flag.Arg(0) == "" {
fmt.Printf("usage: cat data | %s <targetdir>\n", os.Args[0])
os.Exit(1)
}
err := Main()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
// Main is the exported CLI executable function
func Main() error {
err := os.MkdirAll(flag.Arg(0), 0755)
if err != nil {
return err
}
fc, err := infectious.NewFEC(*rsk, *rsn)
if err != nil {
return err
}
es := eestream.NewRSScheme(fc, *pieceBlockSize)
rs, err := eestream.NewRedundancyStrategy(es, 0, 0)
if err != nil {
return err
}
encKey := sha256.Sum256([]byte(*key))
var firstNonce [12]byte
encrypter, err := eestream.NewAESGCMEncrypter(
&encKey, &firstNonce, es.DecodedBlockSize())
if err != nil {
return err
}
readers, err := eestream.EncodeReader(context.Background(),
eestream.TransformReader(eestream.PadReader(os.Stdin,
encrypter.InBlockSize()), encrypter, 0), rs, 4*1024*1024)
if err != nil {
return err
}
errs := make(chan error, len(readers))
for i := range readers {
go func(i int) {
fh, err := os.Create(
filepath.Join(flag.Arg(0), fmt.Sprintf("%d.piece", i)))
if err != nil {
errs <- err
return
}
defer fh.Close()
_, err = io.Copy(fh, readers[i])
errs <- err
}(i)
}
for range readers {
err := <-errs
if err != nil {
return err
}
}
return nil
}