9 This creates a connection so that you can interact with the server.
14 define('AWS_KEY', 'place access key here');
15 define('AWS_SECRET_KEY', 'place secret key here');
16 define('AWS_CANONICAL_ID', 'your DHO Username');
17 define('AWS_CANONICAL_NAME', 'Also your DHO Username!');
18 $HOST = 'objects.dreamhost.com';
20 // require the amazon sdk for php library
21 require_once 'AWSSDKforPHP/sdk.class.php';
23 // Instantiate the S3 class and point it at the desired host
24 $Connection = new AmazonS3(array(
26 'secret' => AWS_SECRET_KEY,
27 'canonical_id' => AWS_CANONICAL_ID,
28 'canonical_name' => AWS_CANONICAL_NAME,
30 $Connection->set_hostname($HOST);
31 $Connection->allow_hostname_override(false);
33 // Set the S3 class to use objects.dreamhost.com/bucket
34 // instead of bucket.objects.dreamhost.com
35 $Connection->enable_path_style();
40 This gets a list of CFSimpleXML objects representing buckets that you
41 own. This also prints out the bucket name and creation date of each
47 $ListResponse = $Connection->list_buckets();
48 $Buckets = $ListResponse->body->Buckets->Bucket;
49 foreach ($Buckets as $Bucket) {
50 echo $Bucket->Name . "\t" . $Bucket->CreationDate . "\n";
53 The output will look something like this::
55 mahbuckat1 2011-04-21T18:05:39.000Z
56 mahbuckat2 2011-04-21T18:05:48.000Z
57 mahbuckat3 2011-04-21T18:07:18.000Z
63 This creates a new bucket called ``my-new-bucket`` and returns a
64 ``CFResponse`` object.
68 This command requires a region as the second argument,
69 so we use ``AmazonS3::REGION_US_E1``, because this constant is ``''``
74 $Connection->create_bucket('my-new-bucket', AmazonS3::REGION_US_E1);
77 List a Bucket's Content
78 -----------------------
80 This gets an array of ``CFSimpleXML`` objects representing the objects
81 in the bucket. This then prints out each object's name, the file size,
82 and last modified date.
87 $ObjectsListResponse = $Connection->list_objects($bucketname);
88 $Objects = $ObjectsListResponse->body->Contents;
89 foreach ($Objects as $Object) {
90 echo $Object->Key . "\t" . $Object->Size . "\t" . $Object->LastModified . "\n";
95 If there are more than 1000 objects in this bucket,
96 you need to check $ObjectListResponse->body->isTruncated
97 and run again with the name of the last key listed.
98 Keep doing this until isTruncated is not true.
100 The output will look something like this if the bucket has some files::
102 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
103 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
109 This deletes the bucket called ``my-old-bucket`` and returns a
110 ``CFResponse`` object
114 The Bucket must be empty! Otherwise it won't work!
119 $Connection->delete_bucket('my-old-bucket');
122 Forced Delte for Non-empty Buckets
123 ----------------------------------
125 This will delete the bucket even if it is not empty.
130 $Connection->delete_bucket('my-old-bucket', 1);
136 This creates an object ``hello.txt`` with the string ``"Hello World!"``
141 $Connection->create_object('my-bucket-name', 'hello.txt', array(
142 'body' => "Hello World!",
146 Change an Object's ACL
147 ----------------------
149 This makes the object ``hello.txt`` to be publicly readable and
150 ``secret_plans.txt`` to be private.
155 $Connection->set_object_acl('my-bucket-name', 'hello.txt', AmazonS3::ACL_PUBLIC);
156 $Connection->set_object_acl('my-bucket-name', 'secret_plans.txt', AmazonS3::ACL_PRIVATE);
162 This deletes the object ``goodbye.txt``
167 $Connection->delete_object('my-bucket-name', 'goodbye.txt');
170 Download an Object (to a file)
171 ------------------------------
173 This downloads the object ``poetry.pdf`` and saves it in
174 ``/home/larry/documents/``
179 $FileHandle = fopen('/home/larry/documents/poetry.pdf', 'w+');
180 $Connection->get_object('my-bucket-name', 'poetry.pdf', array(
181 'fileDownload' => $FileHandle,
185 Generate Object Download URLs (signed and unsigned)
186 ---------------------------------------------------
188 This generates an unsigned download URL for ``hello.txt``.
189 This works because we made ``hello.txt`` public by setting
190 the ACL above. This then generates a signed download URL
191 for ``secret_plans.txt`` that will work for 1 hour.
192 Signed download URLs will work for the time period even
193 if the object is private (when the time period is up,
194 the URL will stop working).
199 my $plans_url = $Connection->get_object_url('my-bucket-name', 'hello.txt');
200 echo $plans_url . "\n";
201 my $secret_url = $Connection->get_object_url('my-bucket-name', 'secret_plans.txt', '1 hour');
202 echo $secret_url . "\n";
204 The output of this will look something like::
206 http://objects.dreamhost.com/my-bucket-name/hello.txt
207 http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX