Friday, November 14, 2014

Working with Amazon S3 in Perl

The following perl program shows how to store and retrieve files to and from Amazon S3 buckets.
#!/usr/bin/perl
use Net::Amazon::S3; # Required module for accessing the AWS bucket

# Amazon AWS credentials
my $access_key = 'xxxxxxxxxxxxxxxx'; # Your AWS Access Key ID
my $secret_key = 'xxxxxxxxxxxxxxxx'; # Your AWS Secret Key

# Create a S3 object providing the AWS credentials
our $s3 = Net::Amazon::S3->new(
    {
        aws_access_key_id     => $access_key,
        aws_secret_access_key => $secret_key
    }
);

# create a new s3 bucket
my $bucket = $s3->add_bucket( { bucket => 'test_bucket' } ) or $s3->error;

# or use an existing s3 bucket
$bucket = $s3->bucket('test_bucket');

# storing a file in the bucket, the first parameter is the source filename and the second parameter is the target filename
$bucket->add_key_filename( 'watermark.jpg','watermark.jpg', { content_type => 'image/jpeg', },) or $s3->error;

# create a file directly in the bucket and store content in the file on the fly
$bucket->add_key( 'message.txt',"hello how are you", { content_type => 'text/javascript', 'x-amz-meta-colour' => 'orange', acl_short => 'private' } );

# list files in the bucket
$response = $bucket->list_all or $s3->error;

# iterates a loop for reading all files in the bucket one by one 
foreach my $key ( @{ $response->{keys} } ) {
 my $key_name = $key->{key};  # returns the filename
 my $key_size = $key->{size}; # returns the size of the file 
 print "Bucket contains key '$key_name' of size $key_size\n";
}

# downloads a file from the bucket
$response = $bucket->get_key_filename( 'watermark.jpg', 'GET', 'watermark_dwld.jpg' ) or $s3->error;

# deleting a file from the bucket
$bucket->delete_key('watermark.jpg');


Output:
-------
On running the Perl script from command line, it will create a bucket named 'test_bucket' in Amazon S3, store a file named 'watermark.jpg' in the bucket, create a file named 'message.txt' in the bucket with content 'hello how are you' written in the file on the fly, display names and sizes of all files stored in the bucket, download the file named 'watermark.jpg' stored in the bucket with the name 'watermark_dwld.jpg' in the working directory of the Perl script and finally delete the file name 'watermark.jpg' from the bucket. 

Wednesday, November 5, 2014

Perl script to fetch multiple messages from an Amazon SQS queue

A way out to retrieve multiple messages in a batch from an AWS queue.
#!/usr/bin/perl

use Amazon::SQS::Simple; # module required for accesing Amazon SQS

# Amazon AWS credentials
my $access_key = 'xxxxxxxxxxxxxxxx'; # Your AWS Access Key ID
my $secret_key = 'xxxxxxxxxxxxxxxx'; # Your AWS Secret Key

my $sqs = new Amazon::SQS::Simple($access_key, $secret_key); # Create a SQS object providing the AWS credentials

my $queue = $sqs->GetQueue("queue url"); # create an object for accessing an old queue  

while (1) # working in an infinite loop
{    
 my @msg = $queue->ReceiveMessage( MaxNumberOfMessages => 10 ); # get the messages into @msg array, here 10 messages are retrieved in a batch

 if ( !scalar(@msg) ) # will be true if no more messages are present in the queue 
    {
  print "Probably no data in queue\n";
 }
 
 foreach my $msg (@msg) # read one by one the messages from the @msg array  
    {
  my $message = $msg->MessageBody(); # fetch the message body

  print "$message\n"; # displaying the message body on the screen

  $queue->DeleteMessage( $msg->ReceiptHandle() ); # delete the retrieved message from the queue, if required 
 }
}

$queue->Delete(); # delete the AWS queue, if required 


Output:
-------
On running the Perl script from command line, it will output on the screen, all the messages reading from the AWS queue.

Working with Amazon SQS in Perl

The following perl program shows how to access the Amazon SQS (Simple Queue Service) for storing and retrieving messages using the Amazon::SQS::Simple module.
#!/usr/bin/perl

use Amazon::SQS::Simple; # module required for accesing Amazon SQS

# Amazon AWS credentials
my $access_key = 'xxxxxxxxxxxxxxxx'; # Your AWS Access Key ID
my $secret_key = 'xxxxxxxxxxxxxxxx'; # Your AWS Secret Key

my $sqs = new Amazon::SQS::Simple($access_key, $secret_key); # Create a SQS object providing the AWS (Amazon Web Services) credentials

my $queue = $sqs->CreateQueue("queue name"); # optional, create a queue if required or else follow the next step for accesing an old queue

# or

my $queue = $sqs->GetQueue("queue url"); # create an object for accessing an old queue  

$queue->SendMessage("Hello World!!!"); # store "Hello World!!!" message in the AWS queue

my $msg = $queue->ReceiveMessage(); # retrieving a stored message from the AWS queue

my $message = $msg->MessageBody(); # fetch the message body

print $message; # displaying the message body on the screen

$queue->DeleteMessage( $msg->ReceiptHandle() ); # delete the retrieved message from the queue, if required 

$queue->Delete(); # delete the AWS queue, if required 


Output:
-------
On running the Perl script from command line, it will output "Hello World!!!" on the screen reading from the AWS queue.