Why and How to Diversify Your Data Storage

DZone 's Guide to

Why and How to Diversify Your Data Storage

Making sure you have more than one storage system can come in handy in case of failure. Read on to learn how to diversify like a techy Warren Buffet.

· Web Dev Zone ·
Free Resource

Why Use Online Storage Rather Than Your Production Server?

No matter what you do in life, it’s important to diversify. Just ask Warren Buffet. You should diversify your investment portfolio, income streams, and your online storage, too. Diversification reduces stress.

Imagine having four streams of income on top of having your regular nine-to-five. You wouldn’t be as stressed out if you lost your job compared to having only one stream of revenue.

When you backup your data to another server, you don’t have to lose any sleep over the possibility of your production server burning down. Even if it does burn down, it’s fine. After all, your backups are stored on another server. You can easily pull them out and restore your server without much hassle.

The below tutorial will show you how to store any data on Amazon’s S3 using PHP.

In my previous article, I talked about how to backup your websites. You could use the steps below to store your backups in the cloud and therefore improve your sleep by diversifying!

What Is Amazon S3?

Amazon S3 (Simple Storage Service), put very simply, allows you to store unlimited data in a cloud at a very competitive cost. Amazon also suggests that this service provides a 99.999999999% (11 nines) durability. Which means it’s quite unlikely that your data will become corrupted. And hey, that’s a plus.

Where to Begin

1. Create a Bucket

Buckets are containers in which your data will sit. In order to create one, login to the AWS console and navigate to the S3 Service. Click the ‘Create bucket’ button and follow the steps to create a bucket. All you have to do is set the name and use all of the default configuration settings. Once your bucket is ready, it will be listed in a table.

Image title

2. Upload Data

Once your bucket is created you are ready to upload data. Amazon allows you to upload up to 5GB of data in a single operation. If you are looking to upload files that are larger than that (what would you be uploading?!) you’ll have to use the Multipart Upload API.

Let’s just assume your files are less than 5GB for simplicity’s sake…

At this stage, you can upload files by dragging and dropping them into the bucket in your browser but that’s not fun. Let’s code it!

3. Manage Access

In the AWS console, head to IAM (Identity and Access Management), click on ‘Groups’ tab, and then ‘Create New Group’ button

Image title

Give the group a name and attach AmazonS3FullAccess policy to it.

Now head to the user tab and hit ‘Add user.’

Image title

Create a user and assign it to the group you just created. Also make sure to tick Programmatic access when choosing the Access type.

Image title

IMPORTANT: Note your key and secret you’ll receive upon successful creation of a user, which we’ll need when we are uploading files to S3.

4. Write (a Small Bit of) Code

The example I’ll show you will be based on a small Lumen project I created specifically for this tutorial. I will naturally upload the working demo to my GitHub account and link it in the conclusion. It accepts an image from a simple form and uploads the file to the S3 Bucket we’ve created.

Image title

First, let’s get the AWS PHP SDK using composer.

composer require aws/aws-sdk-php

This is the code for uploading the image to the bucket. It’s super simple. Make sure you insert the name of your bucket as well as the credentials for the user you created above.

$file = $request->image;
$fileName = $_FILES['image']['name'];

$bucket = 'dzone-example';

$s3 = new S3Client([
    'version' => 'latest',
    'region' => 'eu-west-1',
    'credentials' => [
        'key' => env('S3_KEY'),
        'secret' => env('S3_SECRET'),

try {
    // Upload data.
    $result = $s3->putObject([
        'Bucket' => $bucket,
        'Key' => $fileName,
        'SourceFile' => $file

    // Print the URL to the object.
    echo $result['ObjectURL'] . PHP_EOL;
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;

5. Test

As a test, I have uploaded a file to the bucket and it worked like a charm. You can then preview your files in the bucket and it will look something like this.

Image title


The point of this tutorial was to illustrate how easy it is to store data in an S3 bucket. Hopefully you will use this knowledge to keep your backups in a safe place.

Here’s the repository with sample code I promised you. As always, if you have any questions, feel free to ask in the comments section.

amazon s3 php sdk, cloud storage, s3 tutorial, web application data, web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}