Y T Nuke  
Results 1 to 2 of 2
Would you like to use Amazon's S3 servers to take care of your private membership ...
  1. #1
    Join Date
    Jan 2010
    Location
    San Jose
    Posts
    29
    Reputation
    30
    Thanks
    2
    Thanked 78 Times in 16 Posts

    Arrow How To Use Amazon S3 To Serve Private Membership Site Content

    Would you like to use Amazon's S3 servers to take care of your private membership site content?

    I'd been looking for a way to do this and no one could give me a straight answer, so I spent a few hours the other day and have finally got it working.

    I know a lot of you could benefit from this info, so here goes...

    1. Upload your content to S3 -- I use the Firefox plugin "S3 Organizer". The security settings should be private by default.

    2. Create a php file on your web server with the following code. Make sure you replace YOUR-KEY and YOUR-OTHER-KEY with your correct Amazon keys:

    Code:
    <?php
      // grab this with "pear install --onlyreqdeps Crypt_HMAC"
    require_once('Crypt/HMAC.php');
    
    // Amazon S3 credentials
    define('S3_ACCESS_KEY_ID', 'YOUR-KEY');
    define('S3_SECRET_ACCESS_KEY', 'YOUR-OTHER-KEY');
    
    /**
    * Generate a link to download a file from Amazon S3 using query string
    * authentication. This link is only valid for a limited amount of time.
    *
    * @param $bucket The name of the bucket in which the file is stored.
    * @param $filekey The key of the file, excluding the leading slash.
    * @param $expires The amount of time the link is valid (in seconds).
    * @param $operation The type of HTTP operation. Either GET or HEAD.
    */
    
    function mymodule_get_s3_auth_link($bucket, $filekey, $expires = 3000, $operation = 'GET') {
    $expire_time = time() + $expires;
    $filekey = rawurlencode($filekey);
    $filekey = str_replace('%2F', '/', $filekey);
    $path = $bucket .'/'. $filekey;
    
    /**
    * StringToSign = HTTP-VERB + "\n" +
    * Content-MD5 + "\n" +
    * Content-Type + "\n" +
    * Expires + "\n" +
    * CanonicalizedAmzHeaders +
    * CanonicalizedResource;
    */
    
    $stringtosign =
    $operation ."\n". // type of HTTP request (GET/HEAD)
    "\n". // Content-MD5 is meaningless for GET
    "\n". // Content-Type is meaningless for GET
    $expire_time ."\n". // set the expire date of this link
    "/$path"; // full path (incl bucket), starting with a /
    
    $signature = urlencode(mymodule_constructSig($stringtosign));
    
    $url = sprintf('http://%s.s3.amazonaws.com/%s?AWSAccessKeyId=%s&Expires=%u&Signature=%s', $bucket, $filekey, S3_ACCESS_KEY_ID, $expire_time, $signature);
    
    return $url;
    }
    
    function mymodule_hex2b64($str) {
    $raw = '';
    for ($i=0; $i < strlen($str); $i+=2) {
     $raw .= chr(hexdec(substr($str, $i, 2)));
    }
    return base64_encode($raw);
    }
    
    function mymodule_constructSig($str) {
    $hasher =& new Crypt_HMAC(S3_SECRET_ACCESS_KEY, 'sha1');
    $signature = mymodule_hex2b64($hasher->hash($str));
    return $signature;
    }
    ?>
    3. Now to access a file hosted on S3, use this code. Again make sure to replace YOUR-S3-CODE.php with the name of the file you created above, YOUR-BUCKET-NAME with your Amazon bucket name, and YOU_PATH_TO_FILE with the path of the ile within your bucket:

    Code:
    <?php
      // grab this with "pear install --onlyreqdeps Crypt_HMAC"
    require_once('YOUR-S3-CODE.php');
    
    echo mymodule_get_s3_auth_link('YOUR-BUCKET-NAME', 'YOUR_PATH_TO_FILE');
    
    ?>
    For example, if you wanted to link to a mov file in your members area you'd do somethig like this:

    Code:
    <a href="<?php echo mymodule_get_s3_auth_link('my-bucket', 'movie.mov');?>">Download Mov File</a>
    You could do the same thing to embed a streaming flv from S3.


  2. The Following 3 Users Say Thank You to TechnicalSergio For This Useful Post:

    lildog (05-15-2010), pipwaves (05-15-2010), steveo70 (05-15-2010)

  3. #2
    pipwaves's Avatar
    pipwaves is offline Newbies
    Join Date
    Nov 2009
    Location
    Australia
    Posts
    33
    Reputation
    7
    Thanks
    2
    Thanked 7 Times in 5 Posts

    Default Re: How To Use Amazon S3 To Serve Private Membership Site Content

    very handy.. thanks

  4. The Following User Says Thank You to pipwaves For This Useful Post:

    dgwreg (05-23-2010)

Similar Threads

  1. Replies: 345
    Last Post: 01-23-2012, 02:22 AM
  2. Membership site VS. private forum
    By mightykudgel in forum Membership Sites
    Replies: 2
    Last Post: 11-13-2009, 07:43 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
  SEnukeX SEO Software
Proudly Powered by Hostwinds.com Web Hosting Click Here For Exclusive BHW Discounts!

Cheap Web Hosting


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76