Thursday 1 August 2013

Coffee to S3

It's late, I have tron on in the background and am thinking of how to make the Grid a reality.. may not be something I can come up with tonight :/

..what else is on my mind...

I'm pushing things to Amazon's Simple Storage Service(S3) but would be good to automate them.


Coffee time! and I'm not just saying that because its late. ok, bad pun

Next we need to install the AWS SDK(more info on AWS SDK for node.js).

console:

npm install aws-sdk
Now you will need to get your AWS access info

First login to your Amazon account at http://aws.amazon.com

Once logged in go to the top right, click on your name and then click "Security Credentials"







Next you will need to go to you Access Keys and click "Create New Root Key"



Next you will be prompted with the "Create Access Key". [This will invalidate your old key]
download the 'Key File' to access your newly generated key.




back to the editor and create two new files config.json and  aws.coffee

file config.json


{ "accessKeyId": " AWSAccessKeyId goes here ", "secretAccessKey": " AWSSecretKey goes here ", "region": "us-west-2" }

file aws.coffee

#load the aws sdk
AWS = require('aws-sdk')

#load the keys to access your account
AWS.config.loadFromPath './config.json'

#lets create an object to connect to S3
s3 = new AWS.S3()

#As buckets names are shared across all account in a region
#Let create a random number so multiple people can run this example
ran = Math.random()

#call the createBucket function and add a file
s3.createBucket
 Bucket: "codemeasandwich#{ran}"
, ->
 params =
  Bucket: "codemeasandwich#{ran}"
  Key: "aFile"
  Body: "HelloWorld"

 s3.putObject params, (err, data) ->
  if err
   console.log err
  else
   console.log data
   console.log "Successfully uploaded data to myBucket/myKey"


Now lets fire up the console and run our "aws.coffee"

console:
coffee aws.coffee
now check that this is all ok

No comments:

Post a Comment