Programming With Yii2: Using Amazon S3

Final product image
What You'll Be Creating

If you're asking, "What's Yii?" check out my earlier tutorial, Introduction to the Yii Framework, which reviews the benefits of Yii and includes an overview of what's new in Yii 2.0, released in October 2014.

In this Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In today's tutorial, I'll walk you through the basics of browsing, uploading and downloading files to and from Amazon's cloud-based S3 storage service. Essentially, I've created a simple storage model and controller as examples which you can extend for your needs.

Just a reminder, I do participate in the comment threads below. I'm especially interested if you have different approaches, additional ideas, or want to suggest topics for future tutorials. If you have a question or topic suggestion, please post below. You can also reach me on Twitter @reifman directly.

What's Amazon S3?

Amazon S3 provides easy-to-use, advanced cloud-based storage for objects and files. It offers 99.99% availability and 99.999999999% durability of objects.

It offers a variety of features for simple or advanced usage. It's commonly used as the storage component for Amazon's CDN service CloudFront, but these are distinct and can be used independently of each other.

You can also use S3 to migrate files over time to archive in Amazon Glacier, for added cost savings.

Like most all of AWS, you operate S3 via APIs, and today, I'm going to walk you through browsing, uploading and downloading files from S3 with Yii.

Getting Started

To run the demonstration code, you'll need your own Amazon AWS account and access keys. You can browse your S3 tree from the AWS console shown below:

Yii AWS S3 - AWS Console Menu

S3 consists of buckets which hold numerous directories and files within them. Since I used to use AWS as a CDN, my WordPress tree remains in my old bucket. You can browse your bucket as well:

Yii AWS S3 - Buckets

As I traverse the tree of objects, here's a deeper view of my bucket contents:

Yii AWS S3 - Objects Folders and Files

Programming With S3

Again, I'll build on the hello tree from GitHub for our demonstration code (see the link on this page.) It's derived from Yii2 basic.

Obtaining Your Access Keys

You will need access keys for the AWS S3 API if you don't already have them. If not, you can get them by browsing to Security Credentials and creating a new pair:

Yii AWS S3 - Security Credentials and Access Keys

For our code demonstration, you'll need to place them in your hello.ini file with other secure keys and codes:

Installing the Yii Extension for AWS

For this tutorial, we'll use Federico Motta's AWS extension for Yii2. He's definitely the youngest Yii programmer whose code I've used for an Envato Tuts+ tutorial:

Yii AWS S3 - AWS SDK Extension for Yii2 on Github

Isn't it amazing how quickly kids are picking up programming these days?

Here's the installation process using composer:

Afterwards, I also installed the two libraries it suggests, but did not install all of the next level of suggestions for my local development machine:

I also registered the awssdk component within hello/config/web.php:

Browsing My S3 Directories

For today's demonstration, I created a hello/controllers/StorageController.php with action methods to run each example, such as http://localhost:8888/hello/storage/browse to browse directories. 

These methods in turn call the Storage.php model I created with their own methods.

Here's the controller code:

It requests that the Storage model reach up to the clouds in the "S3ky" and browse the manual directory.

Each time the Storage.php model is instantiated, it loads the AWS SDK extension and creates an S3 instance:

In my browse example, I'm just echoing the directories and files, but you can feel free to customize this code as you need:

Here are the results when I browse to http://localhost:8888/hello/storage/browse:

Yii AWS S3 - Browse S3 Objects a listing of directories and files

Uploading Files

To upload a file, you need to specify the local path and the remote destination key. Here's the controller code for upload:

And here is the Storage model method:

Browsing to http://localhost:8888/hello/storage/upload displays the returning URL from which I can view the uploaded file, because I specified public-read in my code above:

Yii AWS S3 - Results of Upload method - a URL to the new file

In turn, browsing to the S3 address above shows the contents of the uploaded file:

Downloading Files

Here's the controller code for downloading a file:

Since the browser responds to the content-type, it should display the appropriate image, which I'm requesting here.

Note: I'm downloading a cover image from my experience marrying a corporation named Corporate Person to a woman (yes, it actually happened). The marriage didn't work out long term.

Here's the Storage model code for downloading:

Here's what you see when the file is streamed to the browser—that's the bride celebrating by waving the actual marriage license to Corporate Person (I'm smiling in the background, mission accomplished).

Yii AWS S3 - Marriage Scene with Woman and Marriage License

Certainly, you could just as easily store the results on your server in a file. It's up to you. I encourage you to play with the code and customize it as you need.

What's Next?

I hope this helps you with the basics of using AWS S3 from your Yii application. 

If you like the concept of cloud-based object and file storage but want to find other providers, check out Alternatives to Amazon AWS. I've been gradually moving away from AWS for a number of reasons mentioned in the article. One of my next tasks is to migrate my S3 objects that are still partly in use to my own server, which I can mirror with KeyCDN.

Watch for upcoming tutorials in our Programming With Yii2 series as we continue diving into different aspects of the framework. You may also want to check out our Building Your Startup With PHP series which is using Yii2's advanced template as we build a real-world application. The Meeting Planner application in the startup series is now ready for use, and it's all built in Yii.

If you'd like to know when the next Yii2 tutorial arrives, follow me @reifman on Twitter or check my instructor page

Related Links



Related Articles