The Go SDK is currently in experimental status. If you would like to provide feedback, please reach out to us with your suggestions and comments on our Discord.
Go - Bucket.File.DownloadUrl()
Create a download url for a file within a bucket.
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
bucket, err := nitric.NewBucket("bucket-name").Allow(nitric.BucketRead)
if err != nil {
return
}
uploadUrl, err := bucket.File("cat.png").DownloadUrl(context.TODO(), 3600)
if err != nil {
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Parameters
- Name
ctx
- Required
- Required
- Type
- context
- Description
The context of the call, used for tracing.
- Name
expiry
- Required
- Required
- Type
- int
- Description
Seconds until link expiry. Maximum of
604800
(7 days).
Examples
Create a readable link that is valid for the next 5 minutes
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
bucket, err := nitric.NewBucket("bucket-name").Allow(nitric.BucketRead)
if err != nil {
return
}
uploadUrl, err := bucket.File("cat.png").DownloadUrl(context.TODO(), 300)
if err != nil {
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Redirect response to an image URL
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/handler"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
api, err := nitric.NewApi("main")
if err != nil {
return
}
bucket, err := nitric.NewBucket("images").Allow(nitric.BucketRead)
if err != nil {
return
}
api.Get("/images/:id", func(ctx *handler.HttpContext, _ handler.HttpHandler) (*handler.HttpContext, error) {
id := ctx.Request.PathParams()["id"]
downloadUrl, err := bucket.File(id).DownloadUrl(context.TODO(), 600)
if err != nil {
return ctx, err
}
ctx.Response.Headers["Location"] = []string{downloadUrl}
ctx.Response.Status = 303
return ctx, nil
})
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}