9 This creates a connection so that you can interact with the server.
14 my $access_key = 'put your access key here!';
15 my $secret_key = 'put your secret key here!';
17 my $conn = Amazon::S3->new({
18 aws_access_key_id => $access_key,
19 aws_secret_access_key => $secret_key,
20 host => 'objects.dreamhost.com',
29 This gets a list of `Amazon::S3::Bucket`_ objects that you own.
30 We'll also print out the bucket name and creation date of each bucket.
34 my @buckets = @{$conn->buckets->{buckets} || []};
35 foreach my $bucket (@buckets) {
36 print $bucket->bucket . "\t" . $bucket->creation_date . "\n";
39 The output will look something like this::
41 mahbuckat1 2011-04-21T18:05:39.000Z
42 mahbuckat2 2011-04-21T18:05:48.000Z
43 mahbuckat3 2011-04-21T18:07:18.000Z
49 This creates a new bucket called ``my-new-bucket``
53 my $bucket = $conn->add_bucket({ bucket => 'my-new-bucket' });
56 Listing a Bucket's Content
57 --------------------------
59 This gets a list of hashes with info about each object in the bucket.
60 We'll also print out each object's name, the file size, and last
65 my @keys = @{$bucket->list_all->{keys} || []};
66 foreach my $key (@keys) {
67 print "$key->{key}\t$key->{size}\t$key->{last_modified}\n";
70 The output will look something like this::
72 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
73 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
80 The Bucket must be empty! Otherwise it won't work!
84 $conn->delete_bucket($bucket);
87 Forced Delete for Non-empty Buckets
88 -----------------------------------
92 not available in the `Amazon::S3`_ perl module
98 This creates a file ``hello.txt`` with the string ``"Hello World!"``
103 'hello.txt', 'Hello World!',
104 { content_type => 'text/plain' },
108 Change an Object's ACL
109 ----------------------
111 This makes the object ``hello.txt`` to be publicly readable and
112 ``secret_plans.txt`` to be private.
118 acl_short => 'public-read',
121 key => 'secret_plans.txt',
122 acl_short => 'private',
126 Download an Object (to a file)
127 ------------------------------
129 This downloads the object ``perl_poetry.pdf`` and saves it in
130 ``/home/larry/documents/``
134 $bucket->get_key_filename('perl_poetry.pdf', undef,
135 '/home/larry/documents/perl_poetry.pdf');
141 This deletes the object ``goodbye.txt``
145 $bucket->delete_key('goodbye.txt');
147 Generate Object Download URLs (signed and unsigned)
148 ---------------------------------------------------
149 This generates an unsigned download URL for ``hello.txt``. This works
150 because we made ``hello.txt`` public by setting the ACL above.
151 Then this generates a signed download URL for ``secret_plans.txt`` that
152 will work for 1 hour. Signed download URLs will work for the time
153 period even if the object is private (when the time period is up, the
154 URL will stop working).
157 The `Amazon::S3`_ module does not have a way to generate download
158 URLs, so we are going to be using another module instead. Unfortunately,
159 most modules for generating these URLs assume that you are using Amazon,
160 so we have had to go with using a more obscure module, `Muck::FS::S3`_. This
161 should be the same as Amazon's sample S3 perl module, but this sample
162 module is not in CPAN. So, you can either use CPAN to install
163 `Muck::FS::S3`_, or install Amazon's sample S3 module manually. If you go
164 the manual route, you can remove ``Muck::FS::`` from the example below.
168 use Muck::FS::S3::QueryStringAuthGenerator;
169 my $generator = Muck::FS::S3::QueryStringAuthGenerator->new(
172 0, # 0 means use 'http'. set this to 1 for 'https'
173 'objects.dreamhost.com',
176 my $hello_url = $generator->make_bare_url($bucket->bucket, 'hello.txt');
177 print $hello_url . "\n";
179 $generator->expires_in(3600); # 1 hour = 3600 seconds
180 my $plans_url = $generator->get($bucket->bucket, 'secret_plans.txt');
181 print $plans_url . "\n";
183 The output will look something like this::
185 http://objects.dreamhost.com:80/my-bucket-name/hello.txt
186 http://objects.dreamhost.com:80/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX
189 .. _`Amazon::S3`: http://search.cpan.org/~tima/Amazon-S3-0.441/lib/Amazon/S3.pm
190 .. _`Amazon::S3::Bucket`: http://search.cpan.org/~tima/Amazon-S3-0.441/lib/Amazon/S3/Bucket.pm
191 .. _`Muck::FS::S3`: http://search.cpan.org/~mike/Muck-0.02/