GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add and update README. Copy sample from SDK samples · ccarrylab/aws-java-sample@9988382

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • pom.xml
  • src/main/java/com/amazonaws/samples
    • QuickStart.java
    • S3Sample.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboard
+53Lines changed: 53 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# AWS SDK for Java Sample Project
2+
3+
A simple Java application illustrating usage of the AWS SDK for Java.
4+
5+
## Requirements
6+
7+
The only requirement of this application is Maven. All other dependencies can
8+
be installed by building the maven package:
9+
10+
mvn package
11+
12+
## Basic Configuration
13+
14+
You need to set your AWS security credentials in `src/main/AwsCredentials.properties` before the sample
15+
is able to connect to AWS. This file should contain:
16+
17+
accessKey = <your access key id>
18+
secretKey = <your secret key>
19+
20+
See the [Security Credentials](http://aws.amazon.com/security-credentials) page
21+
for more information on getting your keys.
22+
23+
## Running the S3 sample
24+
25+
This sample application connects to Amazon's [Simple Storage Service (S3)](http://aws.amazon.com/s3),
26+
creates a bucket, and uploads a file to that bucket. The code will generate a
27+
bucket name for you, as well as an example file to upload. When you start making
28+
your own buckets, the S3 documentation has a good overview of the [restrictions for bucket names](http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html).
29+
30+
To compile and run the sample:
31+
32+
mvn clean compile exec:java
33+
34+
## License
35+
36+
The SDK and this sample application are distributed under the
37+
[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
38+
39+
```no-highlight
40+
Copyright 2013. Amazon Web Services, Inc. All Rights Reserved.
41+
42+
Licensed under the Apache License, Version 2.0 (the "License");
43+
you may not use this file except in compliance with the License.
44+
You may obtain a copy of the License at
45+
46+
http://www.apache.org/licenses/LICENSE-2.0
47+
48+
Unless required by applicable law or agreed to in writing, software
49+
distributed under the License is distributed on an "AS IS" BASIS,
50+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51+
See the License for the specific language governing permissions and
52+
limitations under the License.
53+
```
Collapse file

‎pom.xml‎

Copy file name to clipboardExpand all lines: pom.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
</execution>
4343
</executions>
4444
<configuration>
45-
<mainClass>com.amazonaws.samples.QuickStart</mainClass>
45+
<mainClass>com.amazonaws.samples.S3Sample</mainClass>
4646
</configuration>
4747
</plugin>
4848
</plugins>
Collapse file

‎src/main/java/com/amazonaws/samples/QuickStart.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/amazonaws/samples/QuickStart.java
-24Lines changed: 0 additions & 24 deletions
This file was deleted.
Collapse file

‎src/main/java/com/amazonaws/samples/S3Sample.java‎

Copy file name to clipboard
+217Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.amazonaws.samples;
16+
17+
import java.io.BufferedReader;
18+
import java.io.File;
19+
import java.io.FileOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
23+
import java.io.OutputStreamWriter;
24+
import java.io.Writer;
25+
import java.util.UUID;
26+
27+
import com.amazonaws.AmazonClientException;
28+
import com.amazonaws.AmazonServiceException;
29+
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
30+
import com.amazonaws.regions.Region;
31+
import com.amazonaws.regions.Regions;
32+
import com.amazonaws.services.s3.AmazonS3;
33+
import com.amazonaws.services.s3.AmazonS3Client;
34+
import com.amazonaws.services.s3.model.Bucket;
35+
import com.amazonaws.services.s3.model.GetObjectRequest;
36+
import com.amazonaws.services.s3.model.ListObjectsRequest;
37+
import com.amazonaws.services.s3.model.ObjectListing;
38+
import com.amazonaws.services.s3.model.PutObjectRequest;
39+
import com.amazonaws.services.s3.model.S3Object;
40+
import com.amazonaws.services.s3.model.S3ObjectSummary;
41+
42+
/**
43+
* This sample demonstrates how to make basic requests to Amazon S3 using
44+
* the AWS SDK for Java.
45+
* <p>
46+
* <b>Prerequisites:</b> You must have a valid Amazon Web Services developer
47+
* account, and be signed up to use Amazon S3. For more information on
48+
* Amazon S3, see http://aws.amazon.com/s3.
49+
* <p>
50+
* <b>Important:</b> Be sure to fill in your AWS access credentials in the
51+
* AwsCredentials.properties file before you try to run this
52+
* sample.
53+
* http://aws.amazon.com/security-credentials
54+
*/
55+
public class S3Sample {
56+
57+
public static void main(String[] args) throws IOException {
58+
/*
59+
* This credentials provider implementation loads your AWS credentials
60+
* from a properties file at the root of your classpath.
61+
*
62+
* Important: Be sure to fill in your AWS access credentials in the
63+
* AwsCredentials.properties file before you try to run this
64+
* sample.
65+
* http://aws.amazon.com/security-credentials
66+
*/
67+
AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
68+
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
69+
s3.setRegion(usWest2);
70+
71+
String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
72+
String key = "MyObjectKey";
73+
74+
System.out.println("===========================================");
75+
System.out.println("Getting Started with Amazon S3");
76+
System.out.println("===========================================\n");
77+
78+
try {
79+
/*
80+
* Create a new S3 bucket - Amazon S3 bucket names are globally unique,
81+
* so once a bucket name has been taken by any user, you can't create
82+
* another bucket with that same name.
83+
*
84+
* You can optionally specify a location for your bucket if you want to
85+
* keep your data closer to your applications or users.
86+
*/
87+
System.out.println("Creating bucket " + bucketName + "\n");
88+
s3.createBucket(bucketName);
89+
90+
/*
91+
* List the buckets in your account
92+
*/
93+
System.out.println("Listing buckets");
94+
for (Bucket bucket : s3.listBuckets()) {
95+
System.out.println(" - " + bucket.getName());
96+
}
97+
System.out.println();
98+
99+
/*
100+
* Upload an object to your bucket - You can easily upload a file to
101+
* S3, or upload directly an InputStream if you know the length of
102+
* the data in the stream. You can also specify your own metadata
103+
* when uploading to S3, which allows you set a variety of options
104+
* like content-type and content-encoding, plus additional metadata
105+
* specific to your applications.
106+
*/
107+
System.out.println("Uploading a new object to S3 from a file\n");
108+
s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
109+
110+
/*
111+
* Download an object - When you download an object, you get all of
112+
* the object's metadata and a stream from which to read the contents.
113+
* It's important to read the contents of the stream as quickly as
114+
* possibly since the data is streamed directly from Amazon S3 and your
115+
* network connection will remain open until you read all the data or
116+
* close the input stream.
117+
*
118+
* GetObjectRequest also supports several other options, including
119+
* conditional downloading of objects based on modification times,
120+
* ETags, and selectively downloading a range of an object.
121+
*/
122+
System.out.println("Downloading an object");
123+
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
124+
System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
125+
displayTextInputStream(object.getObjectContent());
126+
127+
/*
128+
* List objects in your bucket by prefix - There are many options for
129+
* listing the objects in your bucket. Keep in mind that buckets with
130+
* many objects might truncate their results when listing their objects,
131+
* so be sure to check if the returned object listing is truncated, and
132+
* use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve
133+
* additional results.
134+
*/
135+
System.out.println("Listing objects");
136+
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
137+
.withBucketName(bucketName)
138+
.withPrefix("My"));
139+
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
140+
System.out.println(" - " + objectSummary.getKey() + " " +
141+
"(size = " + objectSummary.getSize() + ")");
142+
}
143+
System.out.println();
144+
145+
/*
146+
* Delete an object - Unless versioning has been turned on for your bucket,
147+
* there is no way to undelete an object, so use caution when deleting objects.
148+
*/
149+
System.out.println("Deleting an object\n");
150+
s3.deleteObject(bucketName, key);
151+
152+
/*
153+
* Delete a bucket - A bucket must be completely empty before it can be
154+
* deleted, so remember to delete any objects from your buckets before
155+
* you try to delete them.
156+
*/
157+
System.out.println("Deleting bucket " + bucketName + "\n");
158+
s3.deleteBucket(bucketName);
159+
} catch (AmazonServiceException ase) {
160+
System.out.println("Caught an AmazonServiceException, which means your request made it "
161+
+ "to Amazon S3, but was rejected with an error response for some reason.");
162+
System.out.println("Error Message: " + ase.getMessage());
163+
System.out.println("HTTP Status Code: " + ase.getStatusCode());
164+
System.out.println("AWS Error Code: " + ase.getErrorCode());
165+
System.out.println("Error Type: " + ase.getErrorType());
166+
System.out.println("Request ID: " + ase.getRequestId());
167+
} catch (AmazonClientException ace) {
168+
System.out.println("Caught an AmazonClientException, which means the client encountered "
169+
+ "a serious internal problem while trying to communicate with S3, "
170+
+ "such as not being able to access the network.");
171+
System.out.println("Error Message: " + ace.getMessage());
172+
}
173+
}
174+
175+
/**
176+
* Creates a temporary file with text data to demonstrate uploading a file
177+
* to Amazon S3
178+
*
179+
* @return A newly created temporary file with text data.
180+
*
181+
* @throws IOException
182+
*/
183+
private static File createSampleFile() throws IOException {
184+
File file = File.createTempFile("aws-java-sdk-", ".txt");
185+
file.deleteOnExit();
186+
187+
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
188+
writer.write("abcdefghijklmnopqrstuvwxyz\n");
189+
writer.write("01234567890112345678901234\n");
190+
writer.write("!@#$%^&*()-=[]{};':',.<>/?\n");
191+
writer.write("01234567890112345678901234\n");
192+
writer.write("abcdefghijklmnopqrstuvwxyz\n");
193+
writer.close();
194+
195+
return file;
196+
}
197+
198+
/**
199+
* Displays the contents of the specified input stream as text.
200+
*
201+
* @param input
202+
* The input stream to display as text.
203+
*
204+
* @throws IOException
205+
*/
206+
private static void displayTextInputStream(InputStream input) throws IOException {
207+
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
208+
while (true) {
209+
String line = reader.readLine();
210+
if (line == null) break;
211+
212+
System.out.println(" " + line);
213+
}
214+
System.out.println();
215+
}
216+
217+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.