GithubHelp home page GithubHelp logo

minghsu0107 / cloudfront-signed-url-cookies Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 0.0 32 KB

This example shows how to serve private contents on AWS S3 through CloudFront signed URL and signed cookies.

Home Page: https://minghsu0107.github.io/posts/aws-cloudfront-with-signed-url/

Go 100.00%
s3-bucket cloudfront golang signed-url

cloudfront-signed-url-cookies's Introduction

AWS CloudFront with Signed URL

This is the repository of my blog post.

This example shows how to serve private contents on AWS S3 through CloudFront signed URL and signed cookies. We will be using aws-sdk-go-v2 as the programming client.

Prerequisite

  • A S3 bucket.
  • A CloudFront distribution.
    • Should be created using the S3 owner because S3 bucket policies don’t apply to objects owned by other accounts.
  • The CloudFront bucket access restriction is enabled.
  • A CloudFront origin access identity is created and added to your S3 permission policy.
  • The CloudFront viewer access restriction is enabled and associated with your key group.
  • The public access of your S3 is blocked (default).

Usage

S3_REGION=us-east-2 \
S3_ACCESS_KEY=my-s3-access-key \
S3_SECRET_KEY=my-s3-secret-key \
S3_BUCKET=my-s3-bucket \
CF_DOMAIN=mycfdomain.cloudfront.net \
CF_PUBLIC_KEY_ID=my-cloudfront-access-key \
CF_PRIKEY_PATH=my-cloudfront-prikey-path \
go run main.go

Result

  1. hello.txt will be uploaded to S3 bucket my-s3-bucket with key mysubpath/hello.txt. Its CloudFront URL https://mycfdomain.cloudfront.net/mysubpath/hello.txt will be signed, and the signed URL will be printed to standard output. Users can access the object via this signed URL until it expires after 1 hour.
  2. Signed cookies will be returned and printed to standard output. The signed cookies use the following custom policy:
    • Allow users to access https://mycfdomain.cloudfront.net/mysubpath/* (wildcard).
    • Signed cookies will expire after 1 hour.
  3. The program will request https://mycfdomain.cloudfront.net/mysubpath/hello.txt with signed cookies and print the content of hello.txt to standard output.
  4. An http server will be started. Users can set signed cookies via GET http://localhost/auth. The following cookies will be set: CloudFront-Signature, CloudFront-Policy, and CloudFront-Key-Pair-Id.

cloudfront-signed-url-cookies's People

Contributors

minghsu0107 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.