Perl S3 Examples

Creating a Connection

This creates a connection so that you can interact with the server.

  1. use Amazon::S3;
  2. my $access_key = 'put your access key here!';
  3. my $secret_key = 'put your secret key here!';
  4. my $conn = Amazon::S3->new({
  5. aws_access_key_id => $access_key,
  6. aws_secret_access_key => $secret_key,
  7. host => 'objects.dreamhost.com',
  8. secure => 1,
  9. retry => 1,
  10. });

Listing Owned Buckets

This gets a list of Amazon::S3::Bucket objects that you own. We’ll also print out the bucket name and creation date of each bucket.

  1. my @buckets = @{$conn->buckets->{buckets} || []};
  2. foreach my $bucket (@buckets) {
  3. print $bucket->bucket . "\t" . $bucket->creation_date . "\n";
  4. }

The output will look something like this:

  1. mahbuckat1 2011-04-21T18:05:39.000Z
  2. mahbuckat2 2011-04-21T18:05:48.000Z
  3. mahbuckat3 2011-04-21T18:07:18.000Z

Creating a Bucket

This creates a new bucket called my-new-bucket

  1. my $bucket = $conn->add_bucket({ bucket => 'my-new-bucket' });

Listing a Bucket’s Content

This gets a list of hashes with info about each object in the bucket. We’ll also print out each object’s name, the file size, and last modified date.

  1. my @keys = @{$bucket->list_all->{keys} || []};
  2. foreach my $key (@keys) {
  3. print "$key->{key}\t$key->{size}\t$key->{last_modified}\n";
  4. }

The output will look something like this:

  1. myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
  2. myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

Deleting a Bucket

Note

The Bucket must be empty! Otherwise it won’t work!

  1. $conn->delete_bucket($bucket);

Forced Delete for Non-empty Buckets

Attention

not available in the Amazon::S3 perl module

Creating an Object

This creates a file hello.txt with the string "Hello World!"

  1. $bucket->add_key(
  2. 'hello.txt', 'Hello World!',
  3. { content_type => 'text/plain' },
  4. );

Change an Object’s ACL

This makes the object hello.txt to be publicly readable and secret_plans.txt to be private.

  1. $bucket->set_acl({
  2. key => 'hello.txt',
  3. acl_short => 'public-read',
  4. });
  5. $bucket->set_acl({
  6. key => 'secret_plans.txt',
  7. acl_short => 'private',
  8. });

Download an Object (to a file)

This downloads the object perl_poetry.pdf and saves it in /home/larry/documents/

  1. $bucket->get_key_filename('perl_poetry.pdf', undef,
  2. '/home/larry/documents/perl_poetry.pdf');

Delete an Object

This deletes the object goodbye.txt

  1. $bucket->delete_key('goodbye.txt');

Generate Object Download URLs (signed and unsigned)

This generates an unsigned download URL for hello.txt. This works because we made hello.txt public by setting the ACL above. Then this generates a signed download URL for secret_plans.txt that will work for 1 hour. Signed download URLs will work for the time period even if the object is private (when the time period is up, the URL will stop working).

Note

The Amazon::S3 module does not have a way to generate download URLs, so we are going to be using another module instead. Unfortunately, most modules for generating these URLs assume that you are using Amazon, so we have had to go with using a more obscure module, Muck::FS::S3. This should be the same as Amazon’s sample S3 perl module, but this sample module is not in CPAN. So, you can either use CPAN to install Muck::FS::S3, or install Amazon’s sample S3 module manually. If you go the manual route, you can remove Muck::FS:: from the example below.

  1. use Muck::FS::S3::QueryStringAuthGenerator;
  2. my $generator = Muck::FS::S3::QueryStringAuthGenerator->new(
  3. $access_key,
  4. $secret_key,
  5. 0, # 0 means use 'http'. set this to 1 for 'https'
  6. 'objects.dreamhost.com',
  7. );
  8. my $hello_url = $generator->make_bare_url($bucket->bucket, 'hello.txt');
  9. print $hello_url . "\n";
  10. $generator->expires_in(3600); # 1 hour = 3600 seconds
  11. my $plans_url = $generator->get($bucket->bucket, 'secret_plans.txt');
  12. print $plans_url . "\n";

The output will look something like this:

  1. http://objects.dreamhost.com:80/my-bucket-name/hello.txt
  2. http://objects.dreamhost.com:80/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX