1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Access Amazon S3 Service using Perl

Discussion in 'Perl' started by pradeep, Jul 20, 2009.

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:


    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 ;-)


    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.

    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
    ## the file once deleted, cannot be retrieved
    These were the basic functionalities of S3 accessed via S3, enjoy storing in S3.

    Additional Reading

  2. shabbir

    shabbir Administrator Staff Member

    Jul 12, 2004
    Likes Received:
    Trophy Points:

Share This Page