34
1 Programming Amazon Web Services with Java & Eclipse - Short Tutorial - by Markus Klems [email protected]

Programming Amazon Web Services for Beginners (1)

Embed Size (px)

Citation preview

Page 1: Programming Amazon Web Services for Beginners (1)

1

ProgrammingAmazon Web Serviceswith Java & Eclipse- Short Tutorial -

by Markus [email protected]

Page 2: Programming Amazon Web Services for Beginners (1)

2

Amazon Web Service Developer Ecosystem

Java

• AWS Java libraries

• AWS toolkit for Eclipse

• …

Ruby

• Ruby gems (RightScale, amazon-ec2, AWS::S3)

• …

PHP

• AWS PHP libraries

• Zend Web development framework supports S3, EC2 & SQS

• …

.NET

• AWS SDK for .NET (libraries, Visual Studio templates)

• Windows on EC2

• …

FOCUS

… and more.

Page 3: Programming Amazon Web Services for Beginners (1)

3

AWS Software Development with Java

AWS Java librariesJets3t (James Murty)

Open-source Java toolkit and application suite for Amazon S3 and CloudFront

http://bitbucket.org/jmurty/jets3t/wiki/Home

Typica (D. Kavanagh, Xerox Corporation)Open-Source Java libraries for a broad variety of Amazon Web Services

http://code.google.com/p/typica/

Amazon SDK for JavaOpen-source Java libraries for a broad variety of Amazon Web Services

http://aws.amazon.com/sdkforjava/

AWS toolkit for EclipseEclipse Plug-in

AWS perspective with views “EC2 Instances”, “EC2 AMIs”, et cetera

AWS Project Wizard

Page 4: Programming Amazon Web Services for Beginners (1)

4

AWS Toolkit for Eclipse (1)

Sou

rce:

ww

w.e

clip

se.o

rg/d

ownl

oads

/, A

pril

2010

Page 5: Programming Amazon Web Services for Beginners (1)

5

AWS Toolkit for Eclipse (2)

1) Go to “Help” > “Install New Software…”2) Enter http://aws.amazon.com/eclipse

Sou

rce:

aw

s.am

azon

.com

/ecl

ipse

/, A

pril

2010

Page 6: Programming Amazon Web Services for Beginners (1)

6

AWS Toolkit for Eclipse (3)

Sou

rce:

aw

s.am

azon

.com

/ecl

ipse

/, A

pril

2010

Page 7: Programming Amazon Web Services for Beginners (1)

7

AWS Toolkit for Eclipse (4)

Sou

rce:

aw

s.am

azon

.com

/ecl

ipse

/, A

pril

2010

Page 8: Programming Amazon Web Services for Beginners (1)

8

CodingIntroduction

Page 9: Programming Amazon Web Services for Beginners (1)

9

Amazon Web Service Credentials

Page 10: Programming Amazon Web Services for Beginners (1)

10

Programming S3Introduction

Page 11: Programming Amazon Web Services for Beginners (1)

11

Basic S3 Concepts

BucketsHTTP PUT

HTTP GET

ObjectsHTTP PUT

HTTP GET

Page 12: Programming Amazon Web Services for Beginners (1)

12

S3 Buckets

DNS-compatible bucket namesBucket names must be unique in the S3 universe

Bucket names should not contain underscores (_)

Bucket names should be between 3 and 63 characters long

Bucket names should not end with a dash

Bucket names cannot contain two, adjacent periods

Bucket names cannot contain dashes next to periods (e.g., "my-.bucket.com" and "my.-bucket" are invalid)

Page 13: Programming Amazon Web Services for Beginners (1)

13

Operations on Buckets: PUT

PUT: Create a new bucketPUT / HTTP/1.1Host: eorg-exercise1.s3.amazonaws.comContent-Length: 0Date: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRepdf…

s3.createBucket(“eorg-exercise1");

Page 14: Programming Amazon Web Services for Beginners (1)

14

Operations on Buckets: PUT (2)

Set the Bucket RegionPUT / HTTP/1.1Host: eorg-exercise2.s3.amazonaws.comDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRepdf3Y…Content-Type: text/plainContent-Length: 124<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <LocationConstraint>EU</LocationConstraint></CreateBucketConfiguration >

CreateBucketRequest request = new CreateBucketRequest("eorg-exercise2");request.setRegion("EU");s3.createBucket(request);

Page 15: Programming Amazon Web Services for Beginners (1)

15

Operations on Buckets: PUT (3)

Set Access Control

String bucketName = "eorg-exercise3";CannedAccessControlList acl = CannedAccessControlList.PublicRead;s3.createBucket(bucketName);s3.setBucketAcl(bucketName, acl);

PUT / HTTP/1.1Host: eorg-exercise3.s3.amazonaws.comContent-Length: 0x-amz-acl: public-readDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRep…

Page 16: Programming Amazon Web Services for Beginners (1)

16

Operations on Buckets: GET (1)

Return all (up to 1000) of the objects in a bucket

GET / HTTP/1.1Host: eorg-exercise3.s3.amazonaws.comDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRep…Content-Type: text/plain

String bucketName = "eorg-exercise3";ObjectListing list = s3.listObjects(new

ListObjectsRequest().withBucketName(bucketName));for (S3ObjectSummary s : list.getObjectSummaries()) {

System.out.println(" - " + s.getKey() + “ (size = " + s.getSize() + ")");

}

Page 17: Programming Amazon Web Services for Beginners (1)

17

Operations on S3 Objects: PUT (1)

PUT /isbn-12345 HTTP/1.1Host: eorg-exercise3.s3.amazonaws.comDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRep…Content-Type: text/plainContent-Length: 135Content-MD5: JBVusP8u0QVhBvsvxNDthQ==[…data…]

String bucketName = "eorg-exercise3";String key = "isbn-12345";s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile(“Program-AWS")));

Page 18: Programming Amazon Web Services for Beginners (1)

18

Operations on S3 Objects: PUT (2)

Object Versioning

BucketVersioningConfiguration config = new BucketVersioningConfiguration();config.setStatus(BucketVersioningConfiguration.ENABLED);SetBucketVersioningConfigurationRequest configReq = new SetBucketVersioningConfigurationRequest(bucketName, config);s3.setBucketVersioningConfiguration(configReq);

PutObjectResult result = s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile("Program-

AWS-2")));System.out.println("Version ID: "+result.getVersionId());

Page 19: Programming Amazon Web Services for Beginners (1)

19

Operations on S3 Objects: GET

GET /isbn-12345 HTTP/1.1Host: eorg-exercise3.s3.amazonaws.comDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 02236Q3V0WHVSRW0EXG2:0RQf4/cR…

String bucketName = "eorg-exercise3";String key = "isbn-12345";S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());displayTextInputStream(object.getObjectContent());

Page 20: Programming Amazon Web Services for Beginners (1)

20

Note: Access Control Lists

“Note: Bucket and object ACLs are completely independent; an object does not inherit the ACL from its bucket. For example, if you create a bucket and grant write access to another user, you will not be able to access the user's objects unless the user explicitly grants access. This also applies if you grant anonymous write access to a bucket. Only the user "anonymous" will be able to access objects the user created unless permission is explicitly granted to the bucket owner.

Important: We highly recommend that you do not grant the anonymous group write access to your buckets as you will have no control over the objects others can store and their associated charges.”

Page 21: Programming Amazon Web Services for Beginners (1)

21

Note: Mediated Access with Signed URLs

Gatekeeper1.) Get signed URL

S32.) Retrieve S3 object

Cf. James Murty: “Programming Amazon Web Services”, fig. 4-2

Page 22: Programming Amazon Web Services for Beginners (1)

22

Programming EC2Introduction

Page 23: Programming Amazon Web Services for Beginners (1)

23

Basic EC2 Concepts

EC2 Flow

Amazon Machine Images (AMIs)

Regions & Availability Zones

Run EC2 Instances

Terminate EC2 Instances

Page 24: Programming Amazon Web Services for Beginners (1)

24

EC2 Flow

Source: EC2 Developer Guide 2010-08-3

Page 25: Programming Amazon Web Services for Beginners (1)

25

Amazon Machine Images (AMIs)

Amazon Machine Images (AMIs) are virtual machine images with a root device which is stored either in

Amazon S3, or

Amazon Elastic Block Store (EBS)

Pre-configured public AMIs are provided by the AWS community

Page 26: Programming Amazon Web Services for Beginners (1)

26

Amazon Machine Images (2)

Page 27: Programming Amazon Web Services for Beginners (1)

27

Regions

“Amazon EC2 provides multiple Regions so you can launch Amazon EC2 instances in locations that meet your requirements. Each Amazon EC2 Region is designed to be completely isolated from the other Amazon EC2 Regions. This achieves the greatest possible failure independence and stability, and it makes the locality of each EC2 resource unambiguous.”

Page 28: Programming Amazon Web Services for Beginners (1)

28

Availability Zones

“[F]ailures can occur that affect the availability of instances that are in the same location. Although this is rare, if you host all your Amazon EC2 instances in a single location that is affected by such a failure, your instances will be unavailable.

For example, if you have instances distributed across three Availability Zones and one of the instances fails, you can design your application so the instances in the remaining Availability Zones handle any requests.”

Page 29: Programming Amazon Web Services for Beginners (1)

29

Run EC2 Instances (1)

ec2 = new AmazonEC2Client(credentials);RunInstancesRequest req = new RunInstancesRequest();req.setImageId("ami-480df921");req.setInstanceType("t1.micro");

req.setMinCount(1);req.setMaxCount(1);

RunInstancesResult res = ec2.runInstances(req);System.out.println(res.toString());

Page 30: Programming Amazon Web Services for Beginners (1)

30

Run EC2 Instances (2)

HTTP POST Request

https://ec2.amazonaws.com/?Action=RunInstances&ImageId=ami-480df921&MaxCount=1&MinCount=1&Placement.AvailabilityZone=eu-west-1b&SignatureMethod=HmacSHA256& AWSAccessKeyId=123…

Page 31: Programming Amazon Web Services for Beginners (1)

31

Run EC2 Instances (3)

AWS Management Console

Eclipse Management Console

Page 32: Programming Amazon Web Services for Beginners (1)

32

Run EC2 Instances (4)

Run EC2 Instances in the EU Region

ec2 = new AmazonEC2Client(credentials);ec2.setEndpoint("https://eu-west-1.ec2.amazonaws.com");...Placement p = new Placement();p.setAvailabilityZone("eu-west-1b");req.setPlacement(p);

Page 33: Programming Amazon Web Services for Beginners (1)

33

Don’t forget to terminate…

TerminateInstancesRequest req = new TerminateInstancesRequest().withInstanceIds(id);ec2.terminateInstances(req);

Page 34: Programming Amazon Web Services for Beginners (1)

34

References

S3 Developer Guide 2006-03-01

S3 API Reference 2006-03-01

EC2 Developer Guide 2010-08-31

EC2 API Reference 2010-08-31