Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @aws-sdk/cloudfront-signer

@aws-sdk/cloudfront-signer

This package provides functions to generate signed urls and cookies for accessing private content on CloudFront based on a CloudFront trusted key group key pair.

Please note the process for creating a signed URL with Cloudfront is very different than the process for S3. For more information, please visit the documentation for restricting CloudFront content with signed URLs and signed cookies.

Sign a URL

JavaScript Example:

import { getSignedUrl } from "@aws-sdk/cloudfront-signer"; // ESM
// const { getSignedUrl } = require("@aws-sdk/cloudfront-signer"); // CJS

const cloudfrontDistributionDomain = "https://d111111abcdef8.cloudfront.net";
const s3ObjectKey = "private-content/private.jpeg";
const url = `${cloudfrontDistributionDomain}/${s3ObjectKey}`;
const privateKey = "CONTENTS-OF-PRIVATE-KEY";
const keyPairId = "PUBLIC-KEY-ID-OF-CLOUDFRONT-KEY-PAIR";
const dateLessThan = "2022-01-01"; // any Date constructor compatible

const signedUrl = getSignedUrl({
  url,
  keyPairId,
  dateLessThan,
  privateKey,
});

Sign a URL for cookies

import { getSignedCookies } from "@aws-sdk/cloudfront-signer"; // ESM
// const { getSignedCookies } = require("@aws-sdk/cloudfront-signer"); // CJS

const cloudfrontDistributionDomain = "https://d111111abcdef8.cloudfront.net";
const s3ObjectKey = "private-content/private.jpeg";
const url = `${cloudfrontDistributionDomain}/${s3ObjectKey}`;
const privateKey = "CONTENTS-OF-PRIVATE-KEY";
const keyPairId = "PUBLIC-KEY-ID-OF-CLOUDFRONT-KEY-PAIR";
const dateLessThan = "2022-01-01";

const cookies = getSignedCookies({
  url,
  keyPairId,
  dateLessThan,
  privateKey,
});

Index

References

CloudfrontSignInput

Re-exports CloudfrontSignInput

CloudfrontSignInputBase

Re-exports CloudfrontSignInputBase

CloudfrontSignInputWithParameters

Re-exports CloudfrontSignInputWithParameters

CloudfrontSignInputWithPolicy

Re-exports CloudfrontSignInputWithPolicy

CloudfrontSignedCookiesOutput

Re-exports CloudfrontSignedCookiesOutput

getSignedCookies

Re-exports getSignedCookies

getSignedUrl

Re-exports getSignedUrl

Type aliases

CloudfrontSignInput

Input type to getSignedUrl and getSignedCookies.

CloudfrontSignInputWithParameters

CloudfrontSignInputWithParameters: CloudfrontSignInputBase & { dateLessThan: string; policy?: never }

CloudfrontSignInputWithPolicy

CloudfrontSignInputWithPolicy: CloudfrontSignInputBase & { dateGreaterThan?: never; dateLessThan?: never; ipAddress?: string; policy: string }

Functions

getSignedCookies

  • getSignedCookies(__namedParameters: { dateGreaterThan: string; dateLessThan: string; ipAddress: string; keyPairId: string; policy: string; privateKey: string | Buffer; url: string }): CloudfrontSignedCookiesOutput
  • Creates signed cookies using a canned or custom policy.

    Parameters

    • __namedParameters: { dateGreaterThan: string; dateLessThan: string; ipAddress: string; keyPairId: string; policy: string; privateKey: string | Buffer; url: string }
      • dateGreaterThan: string
      • dateLessThan: string
      • ipAddress: string
      • keyPairId: string
      • policy: string
      • privateKey: string | Buffer
      • url: string

    Returns CloudfrontSignedCookiesOutput

    an object with keys/values that can be added to cookies.

getSignedUrl

  • getSignedUrl(__namedParameters: { dateGreaterThan: string; dateLessThan: string; ipAddress: string; keyPairId: string; policy: string; privateKey: string | Buffer; url: string }): string
  • Creates a signed URL string using a canned or custom policy.

    Parameters

    • __namedParameters: { dateGreaterThan: string; dateLessThan: string; ipAddress: string; keyPairId: string; policy: string; privateKey: string | Buffer; url: string }
      • dateGreaterThan: string
      • dateLessThan: string
      • ipAddress: string
      • keyPairId: string
      • policy: string
      • privateKey: string | Buffer
      • url: string

    Returns string

    the input URL with signature attached as query parameters.