Skip to content

Instantly share code, notes, and snippets.

@kaloyan-raev
Created April 17, 2019 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaloyan-raev/d4a857f09f2ab3e3d83d6ed72858a579 to your computer and use it in GitHub Desktop.
Save kaloyan-raev/d4a857f09f2ab3e3d83d6ed72858a579 to your computer and use it in GitHub Desktop.
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!")
}
@kaloyan-raev
Copy link
Author

kaloyan-raev commented May 29, 2019

Regarding the Android app, it requires credentials to the Mars satellite. I recommend to check with the Storj community leaders if they can provide you an account. I am not actually sure they can do this at the moment, but it's worth trying.

Alternatively, if you have experience with Android development you can try modifying the source code to work against your storj-sim instance.

@iglesiasbrandon
Copy link

@chaxz93 We are inviting developers from our waitlist as we scale the network's capacity please be patient with us you will receive an invitation to one of the whitelisted satellites soon enough!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment