Access Amazon S3 Service using Perl

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

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    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:
    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/amazon-s3-hosting-tips/8705/
    http://www.labnol.org/internet/tools/amazon-s3-buckets-tutorial/3890/
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice