Skip to content

Instantly share code, notes, and snippets.

@mniewrzal
Created July 25, 2019 09:37
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 mniewrzal/31cf28f1c5745d0ea6e4840a5dbeb951 to your computer and use it in GitHub Desktop.
Save mniewrzal/31cf28f1c5745d0ea6e4840a5dbeb951 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"
"math/rand"
"storj.io/storj/lib/uplink"
"storj.io/storj/pkg/storj"
)
func main() {
err := doUploadDownload()
if err != nil {
fmt.Println("error: ", err.Error())
}
}
func doUploadDownload() error {
ctx := context.Background()
fmt.Println("Creating New Uplink Object")
var cfg uplink.Config
cfg.Volatile.TLS.SkipPeerCAWhitelist = true
upl, err := uplink.NewUplink(ctx, &cfg)
//upl, err := uplink.NewUplink(ctx, nil)
if err != nil {
return fmt.Errorf("could not create new Uplink object: %v", err)
}
defer upl.Close()
apiKey := "13YqdS9JwER..."
parsedKey, err := uplink.ParseAPIKey(apiKey)
if err != nil {
return fmt.Errorf("could not parse api key: %v", err)
}
fmt.Println("Open Project ")
proj, err := upl.OpenProject(ctx, "mars.tardigrade.io:7777", parsedKey)
if err != nil {
return fmt.Errorf("could not open project: %v", err)
}
defer proj.Close()
bucketName := "my-bucket"
fmt.Println("Create Bucket ", bucketName)
_, err = proj.CreateBucket(ctx, bucketName, nil)
fmt.Println("Create Bucket ", err)
if err != nil {
return fmt.Errorf("could not create bucket: %v", err)
}
encryptionPassphrase := "abc..."
fmt.Println("Creating new encryption ", encryptionPassphrase)
fmt.Println("List of bucket ")
list := uplink.BucketListOptions{
Direction: storj.Forward}
for {
result, err := proj.ListBuckets(ctx, &list)
if err != nil {
return err
}
for _, bucket := range result.Items {
fmt.Println("Bucket: %v\n", bucket.Name)
}
if !result.More {
break
}
list = list.NextPage(result)
}
encryptionKey, err := proj.SaltedKeyFromPassphrase(ctx, encryptionPassphrase)
if err != nil {
return fmt.Errorf("could not create encryption key: %v", err)
}
access := uplink.NewEncryptionAccessWithDefaultKey(*encryptionKey)
s, err := access.Serialize()
if err != nil {
return fmt.Errorf("could not serialize access: %v", err)
}
fmt.Println("Serialized access: ", s)
fmt.Println("Opening created bucket", bucketName)
bucket, err := proj.OpenBucket(ctx, bucketName, access)
fmt.Println("Opening bucket", err)
if err != nil {
return fmt.Errorf("could not open bucket %q: %v", bucketName, err)
}
defer bucket.Close()
dataToUpload := make([]byte, 1024*1024*2)
_, _ = rand.Read(dataToUpload)
fmt.Println("Converting Data to Buffer")
buf := bytes.NewBuffer(dataToUpload)
if err != nil {
fmt.Println(err)
}
uploadPath := "file01.aaa"
err = bucket.UploadObject(ctx, uploadPath, buf, nil)
fmt.Println("Error uploading object ", err)
if err != nil {
return fmt.Errorf("could not upload: %v", err)
}
fmt.Println("Opening bucket for downloading")
// 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()
fmt.Println("Downloading range")
// 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()
fmt.Println("Read from the stream")
// 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(dataToUpload, receivedContents) {
return fmt.Errorf("error: uploaded data != downloaded data")
}
fmt.Println("Successful upload/download")
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment