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.