-
-
Save kaloyan-raev/d4a857f09f2ab3e3d83d6ed72858a579 to your computer and use it in GitHub Desktop.
// 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!") | |
} |
I am really sorry for missing your comment. Not sure what happened with the mail notification. I was notified by Storj community leaders.
This gist was designed to work with the Storj V3 satellites, hosted by Storj Labs, which have certificates that are trusted by libuplink.
If you want to test this against storj-sim, you can replace line 34 with:
var cfg uplink.Config
cfg.Volatile.TLS.SkipPeerCAWhitelist = true
upl, err := uplink.NewUplink(ctx, &cfg)
This will skip the satellite's CA verification in libuplink.
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.
@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!
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