github.com/aavshr/aws-sdk-go@v1.41.3/example/service/cloudfront/signCookies/signCookies.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  package main
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"net/http"
    11  	"time"
    12  
    13  	"github.com/aavshr/aws-sdk-go/service/cloudfront/sign"
    14  )
    15  
    16  // Makes a request for object using CloudFront cookie signing, and outputs
    17  // the contents of the object to stdout.
    18  //
    19  // Usage example:
    20  // signCookies -file <privkey file>  -id <keyId> -r <resource pattern> -g <object to get>
    21  func main() {
    22  	var keyFile string  // Private key PEM file
    23  	var keyID string    // Key pair ID of CloudFront key pair
    24  	var resource string // CloudFront resource pattern
    25  	var object string   // S3 object frontented by CloudFront
    26  
    27  	flag.StringVar(&keyFile, "file", "", "private key file")
    28  	flag.StringVar(&keyID, "id", "", "key pair id")
    29  	flag.StringVar(&resource, "r", "", "resource to request")
    30  	flag.StringVar(&object, "g", "", "object to get")
    31  	flag.Parse()
    32  
    33  	// Load the PEM file into memory so it can be used by the signer
    34  	privKey, err := sign.LoadPEMPrivKeyFile(keyFile)
    35  	if err != nil {
    36  		fmt.Println("failed to load key,", err)
    37  		return
    38  	}
    39  
    40  	// Create the new CookieSigner to get signed cookies for CloudFront
    41  	// resource requests
    42  	signer := sign.NewCookieSigner(keyID, privKey)
    43  
    44  	// Get the cookies for the resource. These will be used
    45  	// to make the requests with
    46  	cookies, err := signer.Sign(resource, time.Now().Add(1*time.Hour))
    47  	if err != nil {
    48  		fmt.Println("failed to sign cookies", err)
    49  		return
    50  	}
    51  
    52  	// Use the cookies in a http.Client to show how they allow the client
    53  	// to request resources from CloudFront.
    54  	req, err := http.NewRequest("GET", object, nil)
    55  	fmt.Println("Cookies:")
    56  	for _, c := range cookies {
    57  		fmt.Printf("%s=%s;\n", c.Name, c.Value)
    58  		req.AddCookie(c)
    59  	}
    60  
    61  	// Send and handle the response. For a successful response the object's
    62  	// content will be written to stdout. The same process could be applied
    63  	// to a http service written cookies to the response but using
    64  	// http.SetCookie(w, c,) on the ResponseWriter.
    65  	resp, err := http.DefaultClient.Do(req)
    66  	if err != nil {
    67  		fmt.Println("failed to send request", err)
    68  		return
    69  	}
    70  	defer resp.Body.Close()
    71  
    72  	b, err := ioutil.ReadAll(resp.Body)
    73  	if err != nil {
    74  		fmt.Println("failed to read requested body", err)
    75  		return
    76  	}
    77  
    78  	fmt.Println("Response:", resp.Status)
    79  	fmt.Println(string(b))
    80  }