Skip to content

Instantly share code, notes, and snippets.

@BlackDuck888
Last active July 9, 2019 12:32
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 BlackDuck888/e6e149476ae19bbe6b1ca8b4713725db to your computer and use it in GitHub Desktop.
Save BlackDuck888/e6e149476ae19bbe6b1ca8b4713725db to your computer and use it in GitHub Desktop.
Storj file upload example

To Build

go build minimal_fileupload_example.go

module storj_lib_example
require storj.io/storj v0.14.3 // indirect
package main
// This is a simple example for a fileupload to a bucket
// I keep it simple as possible, and do not care about bucket creation or correct error handling
// For a real life you need to handle errors!
import (
"context"
"fmt"
"io"
"log"
"os"
"storj.io/storj/lib/uplink"
"storj.io/storj/pkg/storj"
)
const (
APIKey = "MyApiKey"
satelliteAddress = "satellite.stefan-benten.de:7777"
bucketName = "test"
EncryptionKey = "supersecure"
)
func main() {
var encryptionKey storj.Key
copy(encryptionKey[:], []byte(EncryptionKey))
apiKey, err := uplink.ParseAPIKey(APIKey)
if err != nil {
log.Fatalln("could not parse api key:", err)
}
ctx := context.Background()
// Create an Uplink object with a default config
upl, err := uplink.NewUplink(ctx, nil)
if err != nil {
fmt.Errorf("could not create new Uplink object: %v", err)
return
}
defer upl.Close()
// Open up the Project we will be working with
proj, err := upl.OpenProject(ctx, satelliteAddress, apiKey)
if err != nil {
fmt.Errorf("could not open project: %v", err)
return
}
defer proj.Close()
// Open up the desired Bucket within the Project
bucket, err := proj.OpenBucket(ctx, bucketName, &uplink.EncryptionAccess{Key: encryptionKey})
if err != nil {
fmt.Errorf("could not open bucket %q: %v", bucketName, err)
return
}
defer bucket.Close()
// create file reader
var file *os.File
file, err = os.Open("./video.avi")
if err != nil {
fmt.Println("File not found!")
return
}
defer file.Close()
reader := io.Reader(file)
err = bucket.UploadObject(ctx, "filename_in_bucket.txt", reader, &uplink.UploadOptions{})
if err != nil {
fmt.Errorf("could not upload: %v", err)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment