Amazon S3 is Amazon’s file storage service. It allows users to upload their files to their server, for later access or for sharing to other people. In this tutorial I’m going to walk you through how to use amazon s3 within your PHP applications.
First thing that you need to do is create a
composer.json file and add the following:
1 2 3 4 5
composer install from your terminal to install the Amazon Web Service SDK.
Once the installation is done you can now create a
tester.php file which we will use for interacting with the Amazon AWS API. Add the following code to the file:
1 2 3 4 5 6
What the code above does is include the autoload file so that we can use the AWS SDK from our file. Next we set to use the
Aws\Common\Aws namespace so can access the different classes that are available in those namespaces. One of which classes is the
Aws class which we can use to set the configuration options for the Bucket where we are trying to connect to. All we have to do is call the
factory method and pass in the path to the configuration file:
1 2 3
The configuration file contains the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
The configuration file basically just returns an array that contains the options that we need. First of those is the
includes, which allows us to bootstrap the configuration file with AWS specific features. Next is the
services where we specify the API credentials and region.
Once that’s done we can now upload files to the s3 bucket of your choice by using the
$aws object and calling the
get method. This method takes up the name of the AWS service you want use. In this case were using s3 so we put in
s3. Next we call the
putObject method on the
$s3 object and pass in the required parameters as an array. The required keys are
Bucket is the name of the bucket where you want to upload the file.
Key is the path to the file. With s3 you don’t have to worry if the directory where you are uploading the file already exists. No matter how deep it is, s3 automatically creates the directories for you. Next is the
Body which takes up the results of the
fopen method call. This method takes up the path to the file in your local computer and the operation you want to perform. In this case we just want to read the file contents so we specify
r. Next is the
ACL or the Access Control List of an object. Its basically like a file permission. Here we specified
public-read which means that the file can be read publically. For more information about ACL, you can check out this page. We wrap all of those code inside a try catch so we can handle errors gracefully.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Next here’s how to delete existing files from your s3 bucket. This uses the
deleteObject method which takes up the name of the bucket and the path to the file as its argument.
1 2 3 4 5 6 7 8 9 10 11 12 13
Lastly here’s how to get a list of buckets that are currently in your Amazon Account:
1 2 3 4 5 6 7
That’s it! In this tutorial you’ve learned how to work with Amazon S3 from within your PHP applications. Specifically, we’ve taken a look at how to upload files, delete files and list buckets.