1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
require '/vendor/aws.phar'; use Aws\S3\S3Client; use Aws\Common\Enum\Size; use Aws\Common\Exception\MultipartUploadException; use Aws\S3\Model\MultipartUpload\UploadBuilder; // replace the $CONFIG['amazonKey'] and $CONFIG['amazonSecret'] to your own $client = S3Client::factory(array( 'key' => $CONFIG['amazonKey'], 'secret' => $CONFIG['amazonSecret'], )); $bucketName = 'yeesiang-test'; $keyName = "hello.txt"; $fileContent = "Hello World" $result = $client->putObject(array ( 'Bucket' => $bucketName, 'Key' => $keyName, 'Body' => $fileContent )); $uploadSuccess = $client->waitUntilObjectExists(array ( 'Bucket' => $bucketName, 'Key' => $keyName )); if($uploadSuccess) { echo "upload success!"; } else { echo "upload failed!"; } |
Using S3 for web storage with PHP
Amazon S3 is a reliable and affordable cloud storage. When you host a file up there, it is automatically scale-able.
Not to confuse with your traditional hosting ftp diskspace, s3 is meant to store static files like images, html, js, css and etc; but not dynamically executed server side code like PHP.
In this tutorial, I will teach you how to create a web enabled s3 bucket and how to use PHP code to upload your content onto it.
An S3 bucket is a grouping or set of discrete storage container where you can define specific rules and condition. For example, you can create a bucketA which is private while another bucketB which is publicly accessible from the web. You can also create another bucketC which only serve to log access data generated from the bucketB.
Be notice that each bucket name have to be globally unique and follow the naming convention here. Your bucket name will also be part of your url if you choose to open the bucket to public. You can have unlimited folders and files inside a bucket.
Create a web access bucket
Login to your AWS web console, goto S3 section. Click “Create Bucket” button found on the top right of page.
On the right is your bucket properties. We will need to make changes to Permissions and Static Web Hosting section.
For static web hosting section, note down your end point url. Then click the Enable Website hosting radio button. Do not forget to insert your index document name too. In this case, i just use index.htm. If this file is missing, it will show a 404 File not found HTTP error page. Unlike a convention HTTP server, it wont display a list of other files found in this directory.
For Permission section, Click on the Edit Bucket Policy button. Then, insert the code as shown. Remember to change your resource name to match your bucket name. This move is to give right to the public to have read access on your bucket and every files/folders in it.
Ok, now you have successfully created your bucket and may test it with the endpoint url.
Download Amazon SDK for PHP
Next, we will download Amazon SDK for PHP. As amazon update their SDK frequently, the following information might be out-dated. However, the concept would still be the same.
Visit http://aws.amazon.com/sdkforphp/ for the download file link. Click ‘AWS SDK for PHP‘. Notice the file downloaded is aws.phar. PHAR is a very nice feature from PHP 5.2 and newer for easy distribution and installation.
Writing PHP code to upload file to s3