Created
          December 5, 2012 16:01 
        
      - 
      
 - 
        
Save exinferis/4216882 to your computer and use it in GitHub Desktop.  
    Creating an S3 signature and policy
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | createCredentials: ( mimetype, file, cb ) => | |
| # calling the create policy method and using its reposnse | |
| @createS3Policy( mimetype, file, ( err, policy ) => | |
| if not err | |
| # now building the S3 credential object | |
| s3CredObj = | |
| PolicyBase64: policy | |
| Signature: crypto.createHmac( "sha1", @Secret ).update( policy ).digest( "base64" ) | |
| Key: @Key | |
| ActionStatus: "201" | |
| FileDescriptor: file | |
| ContentType: "#{mimetype}" | |
| Bucket: @Bucket | |
| cb( err, s3CredObj ) | |
| return | |
| ) | |
| return | |
| createS3Policy: ( mimetype, file, cb ) => | |
| # building the policy based on the config entries | |
| _date = new Date( ) | |
| _s3Policy = | |
| "expiration": "#{_date.getFullYear()}-#{_date.getMonth() + 1}-#{_date.getDate()}T#{_date.getHours() + 1}:#{_date.getMinutes()}:#{_date.getSeconds()}Z", | |
| "conditions": [ | |
| {"bucket": @Bucket} | |
| ["starts-with", "$Content-Disposition", ""] | |
| ["starts-with", "$key", "pic_"] | |
| {"acl": "public-read"} | |
| {"success_action_status": "201"} | |
| ["content-length-range", 0, 1024 * 200] #images restricted to 200k | |
| ["eq", "$Content-Type", mimetype], | |
| ] | |
| # encoding the Policy | |
| pBuffer = new Buffer( JSON.stringify( _s3Policy ) ) | |
| PolicyBase64 = pBuffer.toString( "base64" ) | |
| cb( null, PolicyBase64 ) | |
| return | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment