Integrating Amazon S3 in your Application

Amazon Simple Storage Service (S3), a storage service for the Internet is designed to make web scale computing easier for developers. It offers a simple web-service interface that can be used to store and retrieve data, anytime & anywhere.

Why use S3?

  • The most important thing about S3 is its reliability.  According to Amazon’s SLA, it guarantees 99.9% uptime
  • S3 is fast as compared to other storage services
  • Being economical and trustworthy, it is extensively used by developers
  • S3 is based on a very simple model ‘Pay Per Usage‘ i.e, you only pay for the usage
  • Highly scalable, it secures a solution for backing up and archiving your critical data
  • You can also host your static website on S3 with highly available(HA) hosting solution that scales automatically as per the traffic.

In this post, I will walk you through a very simple process of integrating S3 in your application.

How S3 works?

s3 arch Integrating Amazon S3 in your Application

Prerequisites

  1. Amazon Account
  2. Amazon Access and Secret keys
  3. Amazon SDK

Creating Amazon S3 Bucket

Before you start integrating S3, you need to create an S3 Bucket. Every object in Amazon S3 is stored in a bucket.

To create a bucket:

s3 create bucket Integrating Amazon S3 in your Application

  • Enter a bucket name in the Bucket Name field
  • In the Region box, select a region
  • If you want to setup-logging, then click on ‘Set Up Logging‘, Else, go to ‘Create‘ to create the bucket
  • When Amazon S3 successfully creates your bucket, the console displays the properties of your empty bucket

s3 bucket list Integrating Amazon S3 in your Application

Now, you have successfully created a Bucket. You can also manage the permissions on Bucket from the properties tab, available on console.

Integrating S3 in your Application using Amazon S3 SDKs.

Using JAVA SDK:

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
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.elastictranscoder.model.Permission;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.GroupGrantee;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.File;
 
// Instantiate the S3 client with your AWS credentials
AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(
               "Aws_Access_Key", "Aws_Secret_Key"));
 
// If you want to create bucket
// bucketName: name of the bucket
 s3.createBucket("bucketName");
 
// bucketName: Amazon Bucket Name 
// key: The file location that you want to create for your file.
// e.g, key = "user/appName/myapp.zip" 
// File_To_Save: The location of your file.
 
PutObjectRequest objectFile = new PutObjectRequest(
                    "bucketName", "key", "File_To_Save");
 
// Set Access Control List 
AccessControlList acl = new AccessControlList(); 
acl.grantPermission(GroupGrantee.AllUsers, 
            com.amazonaws.services.s3.model.Permission.Read); 
objectFile.setAccessControlList(acl); 
 
// push file to s3 
s3.putObject(objectFile);

Using PHP SDK:

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
// include the  Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
 
// Instantiate the S3 client with your AWS credentials
  $s3Client = S3Client::factory(array(
    'key'    => 'Aws_Access_Key',
    'secret' => 'Aws_Secret_Key',
  ));
 
// If you want to create bucket
// bucketName: name of the bucket
$s3Client->createBucket(array(
        'Bucket' => 'bucketName'
    ));
 
// push file to s3
// bucketName : Amazon Bucket Name 
// key: The file location that you want to create for your file.
// e.g, key = "user/appName/myapp.zip" 
// File_To_Save: The location of your file. 
 
$client->putObject(array( 
        'Bucket' => 'bucketName', 
        'Key' => 'key', 
        'SourceFile' => 'File_To_Save', 
        'ACL' => 'public-read' ));

Using RUBY SDK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require 'rubygems'
require 'aws-sdk'
 
# Instantiate the S3 client with your AWS credentials
  s3 = AWS::S3.new(
       :access_key_id => 'Aws_Access_Key',
       :secret_access_key => 'Aws_Secret_Key')
 
# If you want to create bucket
# bucketName: name of the bucket
  bucket = s3.buckets.create('bucketName', :acl => :public_read)
 
# push file to s3
# bucketName: Amazon Bucket Name 
# key: The file location that you want to create for your file.
# e.g, key = "user/appName/myapp.zip" 
# File_To_Save: The location of your file. 
 
  obj = bucket.objects['key']
  obj.write(:file => file_name)
  puts obj.public_url

In this blog, I have explained you about the Integration of Amazon S3 (using Java, PHP, Ruby SDKs) in your Application.

If you have questions or need any further assistance, do write us at support@shephertz.com

twitter Integrating Amazon S3 in your Applicationfacebook Integrating Amazon S3 in your Applicationgoogle Integrating Amazon S3 in your Applicationlinkedin Integrating Amazon S3 in your Applicationpinterest Integrating Amazon S3 in your Applicationreddit Integrating Amazon S3 in your Applicationstumbleupon Integrating Amazon S3 in your Applicationtumblr Integrating Amazon S3 in your Application

Leave a Reply

Your email address will not be published. Required fields are marked *


3 + = nine

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>