Go - Bucket.DownloadUrl()

Create a download url for a file within a bucket.

import (
"context"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/storage"
)
func main() {
bucket := nitric.NewBucket("bucket-name").Allow(storage.BucketRead)
downloadUrl, err := bucket.DownloadUrl(context.TODO(), "cat.png")
if err != nil {
return
}
nitric.Run()
}

Parameters

  • Name
    ctx
    Required
    Required
    Type
    context
    Description

    The context of the call, used for tracing.

  • Name
    key
    Required
    Required
    Type
    string
    Description

    The key of the file to create a download url for.

  • Name
    ...opts
    Required
    Required
    Type
    storage.PresignUrlOption
    Description

    Options to configure the download url.

Examples

import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/storage"
)
func main() {
bucket := nitric.NewBucket("bucket-name").Allow(nitric.BucketRead)
downloadUrl, err := bucket.File("cat.png").DownloadUrl(context.TODO(), "cat.png", storage.WithPresignUrlExpiry(time.Minute*5))
if err != nil {
return
}
nitric.Run()
}

Redirect response to an image URL

import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric/apis"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
api := nitric.NewApi("main")
bucket := nitric.NewBucket("images").Allow(nitric.BucketRead)
api.Get("/images/:id", func(ctx *apis.Ctx) {
id := ctx.Request.PathParams()["id"]
downloadUrl, err := bucket.DownloadUrl(context.TODO(), id)
if err != nil {
return ctx, err
}
ctx.Response.Headers["Location"] = []string{downloadUrl}
ctx.Response.Status = 303
})
nitric.Run()
}
Last updated on Nov 8, 2024