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

Create a readable link that is valid for the next 5 minutes

import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
  "github.com/nitrictech/go-sdk/nitric/storage"
)

func main() {
  bucket, err := nitric.NewBucket("bucket-name").Allow(nitric.BucketRead)
  if err != nil {
    return
  }

  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, 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 *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()
}