DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Why and How to Diversify Your Data Storage

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.

Łukasz Mądrzak user avatar by
Łukasz Mądrzak
CORE ·
Jan. 16, 19 · Tutorial
Like (2)
Save
Tweet
Share
7.71K Views

Join the DZone community and get the full member experience.

Join For Free

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

Conclusion

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.

Data (computing) AWS Data storage Upload

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Agile Scrum and the New Way of Work in 2023
  • Top 12 Technical Skills Every Software Tester Must Have
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • GitOps: Flux vs Argo CD

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: