9 This installs AWS PHP SDK using composer (see here_ how to install composer).
11 .. _here: https://getcomposer.org/download/
15 $ composer install aws/aws-sdk-php
20 This creates a connection so that you can interact with the server.
24 The client initialization requires a region so we use ``''``.
32 define('AWS_KEY', 'place access key here');
33 define('AWS_SECRET_KEY', 'place secret key here');
34 $ENDPOINT = 'http://objects.dreamhost.com';
36 // require the amazon sdk from your composer vendor dir
37 require __DIR__.'/vendor/autoload.php';
39 // Instantiate the S3 class and point it at the desired host
40 $client = new S3Client([
42 'version' => '2006-03-01',
43 'endpoint' => $ENDPOINT,
46 'secret' => AWS_SECRET_KEY
48 // Set the S3 class to use objects.dreamhost.com/bucket
49 // instead of bucket.objects.dreamhost.com
50 'use_path_style_endpoint' => true
55 This gets a ``AWS\Result`` instance that is more convenient to visit using array access way.
56 This also prints out the bucket name and creation date of each bucket.
61 $listResponse = $client->listBuckets();
62 $buckets = $listResponse['Buckets'];
63 foreach ($buckets as $bucket) {
64 echo $bucket['Name'] . "\t" . $bucket['CreationDate'] . "\n";
67 The output will look something like this::
69 mahbuckat1 2011-04-21T18:05:39.000Z
70 mahbuckat2 2011-04-21T18:05:48.000Z
71 mahbuckat3 2011-04-21T18:07:18.000Z
77 This creates a new bucket called ``my-new-bucket`` and returns a
78 ``AWS\Result`` object.
83 $client->createBucket(['Bucket' => 'my-new-bucket']);
86 List a Bucket's Content
87 -----------------------
89 This gets a ``AWS\Result`` instance that is more convenient to visit using array access way.
90 This then prints out each object's name, the file size, and last modified date.
95 $objectsListResponse = $client->listObjects(['Bucket' => $bucketname]);
96 $objects = $objectsListResponse['Contents'] ?? [];
97 foreach ($objects as $object) {
98 echo $object['Key'] . "\t" . $object['Size'] . "\t" . $object['LastModified'] . "\n";
103 If there are more than 1000 objects in this bucket,
104 you need to check $objectsListResponse['isTruncated']
105 and run again with the name of the last key listed.
106 Keep doing this until isTruncated is not true.
108 The output will look something like this if the bucket has some files::
110 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
111 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
117 This deletes the bucket called ``my-old-bucket`` and returns a
118 ``AWS\Result`` object
122 The Bucket must be empty! Otherwise it won't work!
127 $client->deleteBucket(['Bucket' => 'my-old-bucket']);
133 This creates an object ``hello.txt`` with the string ``"Hello World!"``
139 'Bucket' => 'my-bucket-name',
140 'Key' => 'hello.txt',
141 'Body' => "Hello World!"
145 Change an Object's ACL
146 ----------------------
148 This makes the object ``hello.txt`` to be publicly readable and
149 ``secret_plans.txt`` to be private.
154 $client->putObjectAcl([
155 'Bucket' => 'my-bucket-name',
156 'Key' => 'hello.txt',
157 'ACL' => 'public-read'
159 $client->putObjectAcl([
160 'Bucket' => 'my-bucket-name',
161 'Key' => 'secret_plans.txt',
169 This deletes the object ``goodbye.txt``
174 $client->deleteObject(['Bucket' => 'my-bucket-name', 'Key' => 'goodbye.txt']);
177 Download an Object (to a file)
178 ------------------------------
180 This downloads the object ``poetry.pdf`` and saves it in
181 ``/home/larry/documents/``
186 $object = $client->getObject(['Bucket' => 'my-bucket-name', 'Key' => 'poetry.pdf']);
187 file_put_contents('/home/larry/documents/poetry.pdf', $object['Body']->getContents());
189 Generate Object Download URLs (signed and unsigned)
190 ---------------------------------------------------
192 This generates an unsigned download URL for ``hello.txt``.
193 This works because we made ``hello.txt`` public by setting
194 the ACL above. This then generates a signed download URL
195 for ``secret_plans.txt`` that will work for 1 hour.
196 Signed download URLs will work for the time period even
197 if the object is private (when the time period is up,
198 the URL will stop working).
203 $hello_url = $client->getObjectUrl('my-bucket-name', 'hello.txt');
204 echo $hello_url."\n";
206 $secret_plans_cmd = $client->getCommand('GetObject', ['Bucket' => 'my-bucket-name', 'Key' => 'secret_plans.txt']);
207 $request = $client->createPresignedRequest($secret_plans_cmd, '+1 hour');
208 echo $request->getUri()."\n";
210 The output of this will look something like::
212 http://objects.dreamhost.com/my-bucket-name/hello.txt
213 http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=sandboxAccessKey%2F20190116%2F%2Fs3%2Faws4_request&X-Amz-Date=20190116T125520Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Signature=61921f07c73d7695e47a2192cf55ae030f34c44c512b2160bb5a936b2b48d923