Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Access Amazon S3 Service using Perl (http://www.go4expert.com/articles/access-amazon-s3-service-using-perl-t18637/)

pradeep 20Jul2009 18:59

Access Amazon S3 Service using Perl
 

Introcution


Amazon's S3 [Simple Storage Service] is a really amazing service, where you can store unlimited amount of data at a very cheap rate, and the service being really reliable at the same time. Signing up for the service is really easy, you are charged a very low rate only for what you use, you can read more about the service at http://aws.amazon.com/s3/

You can create a "bucket" which is similar to a folder, the bucket name has to be unique all throughout the s3 service, that means if someone has already created the bucket with the name "images" you cannot create "images", also the you cannot create nested buckets i.e. buckets inside buckets. The number of objects [files] you can store in a bucket is unlimited. You may use S3 as a backup storage, or as live storage.

After signing up for the service you'll be provided an access key and a secret key which will be needed to access the service, never provide your secret key to anyone else. Now, let's get into writing some code ;-)

Coding



For accessing the service, we'll be using a Perl module Net::Amazon::S3 , although you can write your own code to access the S3 service, which we will dig into in another article.

Code: Perl

my %params = ('aws_access_key_id' ,$access_key,'aws_secret_access_key' , $secret_key);
my $s3 = Net::Amazon::S3->new(\%params);
## Our object is now ready

## get the bucket object
our $bucket = $s3->bucket('go4expert'); ## the bucket we'll be using

## adding files
my %attributes = ('
content_type', 'image/jpeg','x-amz-meta-colour' ,'orange','acl_short', 'private'};
$bucket->add_key('
myImage.jpg', $image_data,\%attributes) or die $s3->err . ": " . $s3->errstr;

## ACL = Acess Control Lists
## ACL'
s are of the following types
## private - Owner gets FULL_CONTROL.
## public-read - Owner gets FULL_CONTROL and the anonymous principal is granted READ access.
## public-read-write - Owner gets FULL_CONTROL, the anonymous principal is granted READ and WRITE access.
## authenticated-read - Owner gets FULL_CONTROL, and any principal authenticated as a registered Amazon S3 user is granted READ access.

## reading a file
$response  = $bucket->get_key('myImage.jpg');
## $response->{'value'} contains the data

## deleting a file
$bucket->delete_key('myImage.jpg');
## the file once deleted, cannot be retrieved


These were the basic functionalities of S3 accessed via S3, enjoy storing in S3.

Additional Reading



http://www.labnol.org/internet/amazo...ing-tips/8705/
http://www.labnol.org/internet/tools...tutorial/3890/

shabbir 3Aug2009 14:33

Re: Access Amazon S3 Service using Perl
 
Nominate this article for Article of the month - Jul 2009


All times are GMT +5.5. The time now is 06:34.