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").With(nitric.BucketReading)
  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").With(nitric.BucketReading)
  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/faas"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  api, err := nitric.NewApi("main")
  if err != nil {
    return
  }

  bucket, err := nitric.NewBucket("images").With(nitric.BucketReading)
  if err != nil {
    return
  }

  api.Get("/images/:id", func(ctx *faas.HttpContext, _ faas.HttpHandler) (*faas.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)
  }
}