Last active
June 14, 2022 22:13
-
-
Save halkyon/491041586f56a3f8f1150fa1fd4769e3 to your computer and use it in GitHub Desktop.
share file programatically using storj linksharing service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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!