Skip to content

Instantly share code, notes, and snippets.

@linyiru
Forked from poindextrose/glcoud-signed-url.go
Created August 9, 2019 13:08
Show Gist options
  • Save linyiru/2ba928f0a2acd6848027fb4e7b17890f to your computer and use it in GitHub Desktop.
Save linyiru/2ba928f0a2acd6848027fb4e7b17890f to your computer and use it in GitHub Desktop.
Example on how to create a signed URL on Google Cloud Storage with Go
package main
import (
"fmt"
"time"
"google.golang.org/cloud/storage"
)
const (
projectID = "myProject-1234"
)
func main() {
bucket := "mybucket"
filename := "myFilename"
method := "PUT"
expires := time.Now().Add(time.Second * 60)
url, err := storage.SignedURL(bucket, filename, &storage.SignedURLOptions{
GoogleAccessID: "XXXXXX@developer.gserviceaccount.com",
PrivateKey: []byte("-----BEGIN PRIVATE KEY-----\nXXXXXXXX"),
Method: method,
Expires: expires,
})
if err != nil {
fmt.Println("Error " + err.Error())
}
fmt.Println("URL = " + url)
}
@linyiru
Copy link
Author

linyiru commented Aug 10, 2019

import (
   "cloud.google.com/go/storage"
   "time"
)
func (g SignedUrl) GetUrl(uuid string, bucket string) (string, error) {
   expires := time.Now().Add(time.Minute * 10)
   r, err := storage.SignedURL(bucket, uuid, &storage.SignedURLOptions{
      GoogleAccessID: g.keyId,
      PrivateKey:     g.privateKey,
      Method:         "PUT",
      Expires:        expires,
      ContentType:    "image/png",
   })
   return r, err
}

from: https://medium.com/@fluxcapacitor/using-golang-to-create-google-cloud-signed-urls-to-upload-images-using-reactjs-846cfd7ba602

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment