Skip to content

Instantly share code, notes, and snippets.

@kaloyan-raev kaloyan-raev/main.go
Created Apr 17, 2019

Embed
What would you like to do?
Example program in Go using Storj libuplink.
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"storj.io/storj/lib/uplink"
"storj.io/storj/pkg/storj"
)
const (
myAPIKey = "change-me-to-the-api-key-created-in-satellite-gui"
satellite = "mars.tardigrade.io:7777"
myBucket = "my-first-bucket"
myUploadPath = "foo/bar/baz"
myData = "one fish two fish red fish blue fish"
myEncryptionKey = "you'll never guess this"
)
// WorkWithLibUplink uploads the specified data to the specified path in the
// specified bucket, using the specified Satellite, encryption key, and API key.
func WorkWithLibUplink(satelliteAddress string, encryptionKey *storj.Key, apiKey uplink.APIKey,
bucketName, uploadPath string, dataToUpload []byte) error {
ctx := context.Background()
// Create an Uplink object with a default config
upl, err := uplink.NewUplink(ctx, nil)
if err != nil {
return fmt.Errorf("could not create new Uplink object: %v", err)
}
defer upl.Close()
// It is temporarily required to set the encryption key in project options.
// This requirement will be removed in the future.
opts := uplink.ProjectOptions{}
opts.Volatile.EncryptionKey = encryptionKey
// Open up the Project we will be working with
proj, err := upl.OpenProject(ctx, satelliteAddress, apiKey, &opts)
if err != nil {
return fmt.Errorf("could not open project: %v", err)
}
defer proj.Close()
// Create the desired Bucket within the Project
_, err = proj.CreateBucket(ctx, bucketName, nil)
if err != nil {
return fmt.Errorf("could not create bucket: %v", err)
}
// Open up the desired Bucket within the Project
bucket, err := proj.OpenBucket(ctx, bucketName, &uplink.EncryptionAccess{Key: *encryptionKey})
if err != nil {
return fmt.Errorf("could not open bucket %q: %v", bucketName, err)
}
defer bucket.Close()
// Upload our Object to the specified path
buf := bytes.NewBuffer(dataToUpload)
err = bucket.UploadObject(ctx, uploadPath, buf, nil)
if err != nil {
return fmt.Errorf("could not upload: %v", err)
}
// Initiate a download of the same object again
readBack, err := bucket.OpenObject(ctx, uploadPath)
if err != nil {
return fmt.Errorf("could not open object at %q: %v", uploadPath, err)
}
defer readBack.Close()
// We want the whole thing, so range from 0 to -1
strm, err := readBack.DownloadRange(ctx, 0, -1)
if err != nil {
return fmt.Errorf("could not initiate download: %v", err)
}
defer strm.Close()
// Read everything from the stream
receivedContents, err := ioutil.ReadAll(strm)
if err != nil {
return fmt.Errorf("could not read object: %v", err)
}
if !bytes.Equal(receivedContents, dataToUpload) {
return fmt.Errorf("got different object back: %q != %q", dataToUpload, receivedContents)
}
return nil
}
func main() {
var encryptionKey storj.Key
copy(encryptionKey[:], []byte(myEncryptionKey))
apiKey, err := uplink.ParseAPIKey(myAPIKey)
if err != nil {
log.Fatalln("could not parse api key:", err)
}
err = WorkWithLibUplink(satellite, &encryptionKey, apiKey, myBucket, myUploadPath, []byte(myData))
if err != nil {
log.Fatalln("error:", err)
}
fmt.Println("success!")
}
@chaxz93

This comment has been minimized.

Copy link

commented May 20, 2019

I get this error when trying to run on the testnet locally.

error: could not open project: metainfo error: transport error: connection error: desc = "transport: authentication handshake failed: tls peer certificate verification error: not signed by any CA in the whitelist: CA cert"
exit status 1

Also anyway I could have some api credentials to test your promising looking android app:
https://github.com/kaloyan-raev/hello-storj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.