Wednesday, 9 October 2013

Getting Started with the Amazon Web Services

The Amazon Web Services (AWS) is a set of virtual computing and storage services. It is scalable, flexible, and has low start-up cost.

AWS application can be programmed through PHP, Python, Java, and Ruby, etc. Mobile apps can also access AWS through SDK for Android and iOS.


Sign up for AWS products
A credit card is required in order to sign up for AWS. You can use EntroPay, a virtual credit card provider, as the credit card.

Get the Security Credentials from your AWS account
Create a group and user in AWS IAM. The purpose is to get the access key ID and the secret key.

Using AWS SDK for PHP
  • install PHP
  in Windows, copy the following dll files to Windows/System32 folder
    libeay32.dll
    ssleay32.dll
  and also, modify php.ini, and enable the curl extension.
  • install the SDK for PHP
git clone git://github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP
cd ./AWSSDKforPHP
  • configure the security credentials
  copy the config-sample.inc.php file in the SDK folder, and rename it to config.inc.php, and change the access key ID and secret key
  • run the sample php file 
  in the samples folder, run
    php cli-s3_get_urls_for_uploads.php

EXAMPLE
one example of access the bucket using non composer way, it is heavily commented.

<?php

//non-composer way, you need to download aws.phar separately
require 'aws.phar';

use Aws\S3\S3Client;

// Instantiate the S3 client with your AWS credentials and desired AWS region, 
// you must create a user in AWS IAM console
$client = S3Client::factory(array(
    'key'    => 'AKIAILBKCPBE6I2GEKOA',
    'secret' => '2Nbn7LJl/sDz40kviPCFBaOHxZItDelQ1pZzhPyq',
));

//The requested bucket name is not available. 
//The bucket namespace is shared by all users of the system. 
//Please select a different name and try again.
//If you see this error, it means the bucket name is already used by other people. 
//Please choose a bucket name which is not common
$bucket = 'my-bupket1'; 
$array_bucket = array('Bucket' => $bucket);

//check for existence of bucket
if (($client->doesBucketExist($bucket, true, $array_bucket)) == false)
{
  $result = $client->createBucket($array_bucket);

  // Wait until the bucket is created
  $client->waitUntil('BucketExists', array('Bucket' => $bucket));
  echo "bucket created";
}
else
{
  echo "bucket exist";
}

// Key is the identifier of the object
// Body is the content of the object
$client->putObject(array(
    'Bucket' => $bucket,
    'Key'    => 'README.md',
    'Body'   => fopen('README.md', 'r')));

$result = $client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => 'README.md'
));

//the Body value can be cast to a string
echo $result['Body'] . "\n";
?>

Using AWS SDK for Android
  • install Eclipse for Android
  This is necessary to build an Android application.
  • install the SDK for Android
  After that, go to download the SDK from http://aws.amazon.com/developers/getting-started/android/
  • import the sample Android project
   From the samples folder, choose S3_Uploader, change the security credentials, and build the project, and run the application.

1 comment: