Skip to content

Instantly share code, notes, and snippets.

@halkyon
Last active June 14, 2022 22:13
Show Gist options
  • Save halkyon/491041586f56a3f8f1150fa1fd4769e3 to your computer and use it in GitHub Desktop.
Save halkyon/491041586f56a3f8f1150fa1fd4769e3 to your computer and use it in GitHub Desktop.
share file programatically using storj linksharing service
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/pkg/errors"
"storj.io/uplink"
"storj.io/uplink/edge"
)
const (
authServiceAddress = "auth.storjshare.io:7777"
linksharingAddress = "link.storjshare.io"
)
func main() {
baseURL := "https://"+linksharingAddress
bucket := "test-files"
key := "test.txt"
access, err := restrictAccess(os.Getenv("ACCESS_GRANT"), bucket, key)
if err != nil {
panic(err)
}
// Serialize that grant for further use and to later revoke if needed.
// serialized, err := access.Serialize()
// if err != nil {
// panic(err)
// }
cred, err := registerAccess(context.Background(), access)
if err != nil {
panic(err)
}
url, err := edge.JoinShareURL(baseURL, cred.AccessKeyID, bucket, key, &edge.ShareURLOptions{
Raw: false,
})
if err != nil {
panic(err)
}
fmt.Println(url)
}
func restrictAccess(accessGrant, bucket, prefix string) (*uplink.Access, error) {
access, err := uplink.ParseAccess(accessGrant)
if err != nil {
return nil, errors.Wrap(err, "could not request access grant")
}
return access.Share(uplink.Permission{
AllowDownload: true,
AllowList: true,
NotAfter: time.Now().Add(time.Second * 7890000), // expires in 3 months
}, uplink.SharePrefix{
Bucket: bucket,
Prefix: prefix,
})
}
func registerAccess(ctx context.Context, access *uplink.Access) (*edge.Credentials, error) {
config := edge.Config{
AuthServiceAddress: authServiceAddress,
}
return config.RegisterAccess(ctx, access, &edge.RegisterAccessOptions{
Public: true,
})
}
@amwolff
Copy link

amwolff commented Mar 1, 2022

This is great. We should make a container with all these useful little scripts. I have a place locally for the things I produce, but maybe we could make a shared space somehow? I guess a separate repository seems good for that.

@halkyon
Copy link
Author

halkyon commented Mar 1, 2022

@amwolff I'm often looking for one very specific example of an esoteric feature like multi-part form upload in S3, and admittedly I've gone hunting on your gist at least a couple of times :) having an examples repo sounds like a great idea!

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