Creating signed S3 and Cloudfront URLs via the AWS SDK

前端 未结 3 485
别那么骄傲
别那么骄傲 2020-12-05 10:36

Has anyone successfully used the AWS SDK to generate signed URLs to objects in an S3 bucket which also work over CloudFront? I\'m using the JavaScript AWS SDK and it\'s real

3条回答
  •  无人及你
    2020-12-05 11:05

    AWS includes some built in classes and structures to assist in the creation of signed URLs and Cookies for CloudFront. I utilized these alongside the excellent answer by Jason Sims to get it working in a slightly different pattern (which appears to be very similar to the NPM package he created).

    Namely, the AWS.CloudFront.Signer type description which abstracts the process of creating signed URLs and Cookies.

    export class Signer {
        /**
         * A signer object can be used to generate signed URLs and cookies for granting access to content on restricted CloudFront distributions.
         * 
         * @param {string} keyPairId - The ID of the CloudFront key pair being used.
         * @param {string} privateKey - A private key in RSA format.
         */
        constructor(keyPairId: string, privateKey: string);
    
        ....
    }
    

    And either an options with a policy JSON string or without a policy with a url and expiration time.

    export interface SignerOptionsWithPolicy {
        /**
         * A CloudFront JSON policy. Required unless you pass in a url and an expiry time. 
         */
        policy: string;
    }
    export interface SignerOptionsWithoutPolicy {
        /**
         * The URL to which the signature will grant access. Required unless you pass in a full policy.
         */
        url: string
        /**
         * A Unix UTC timestamp indicating when the signature should expire. Required unless you pass in a full policy.
         */
        expires: number
    }
    

    Sample implementation:

    import aws, { CloudFront } from 'aws-sdk';
    
    export async function getSignedUrl() {
    
        // https://abc.cloudfront.net/my-resource.jpg
        const url = ;
    
        // Create signer object - requires a public key id and private key value
        const signer = new CloudFront.Signer(, );
    
        // Setup expiration time (one hour in the future, in this case)
        const expiration = new Date();
        expiration.setTime(expiration.getTime() + 1000 * 60 * 60);
        const expirationEpoch = expiration.valueOf();
    
        // Set options (Without policy in this example, but a JSON policy string can be substituted)
        const options = {
            url: url,
            expires: expirationEpoch
        };
    
        return new Promise((resolve, reject) => {
            // Call getSignedUrl passing in options, to be handled either by callback or synchronously without callback
            signer.getSignedUrl(options, (err, url) => {
                if (err) {
                    console.error(err.stack);
                    reject(err);
                }
                resolve(url);
            });
        });
    }
    

提交回复
热议问题