Skip to content

Commit 560362f

Browse files
adding snippets to S3 Website Configuration
1 parent 41a550e commit 560362f

File tree

3 files changed

+56
-44
lines changed

3 files changed

+56
-44
lines changed

java/example_code/s3/src/main/java/aws/example/s3/DeleteWebsiteConfiguration.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020
specific language governing permissions and limitations under the License.
2121
*/
2222
package aws.example.s3;
23+
// snippet-start:[s3.java1.s3_delete_website_config.import]
24+
2325
import com.amazonaws.services.s3.AmazonS3;
2426
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
2527
import com.amazonaws.AmazonServiceException;
28+
// snippet-end:[s3.java1.s3_delete_website_config.import]
2629

2730
/**
2831
* Delete the website configuration for an S3 bucket.
29-
*
32+
* <p>
3033
* This code expects that you have AWS credentials delete up per:
3134
* http://docs.aws.amazon.com/java-sdk/latest/developer-guide/deleteup-credentials.html
3235
*/
33-
public class DeleteWebsiteConfiguration
34-
{
35-
public static void deleteWebsiteConfig(String bucket_name)
36-
{
36+
// snippet-start:[s3.java1.s3_delete_website_config.complete]
37+
public class DeleteWebsiteConfiguration {
38+
public static void deleteWebsiteConfig(String bucket_name) {
39+
// snippet-start:[s3.java1.s3_delete_website_config.main]
3740
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
3841
try {
3942
s3.deleteBucketWebsiteConfiguration(bucket_name);
@@ -42,15 +45,15 @@ public static void deleteWebsiteConfig(String bucket_name)
4245
System.out.println("Failed to delete website configuration!");
4346
System.exit(1);
4447
}
48+
// snippet-end:[s3.java1.s3_delete_website_config.main]
4549
}
4650

47-
public static void main(String[] args)
48-
{
51+
public static void main(String[] args) {
4952
final String USAGE = "\n" +
50-
"DeleteWebsiteConfiguration - delete the website configuration for an S3 bucket\n\n" +
51-
"Usage: DeleteWebsiteConfiguration <bucket>\n\n" +
52-
"Where:\n" +
53-
" bucket - the bucket to delete the website configuration from\n";
53+
"DeleteWebsiteConfiguration - delete the website configuration for an S3 bucket\n\n" +
54+
"Usage: DeleteWebsiteConfiguration <bucket>\n\n" +
55+
"Where:\n" +
56+
" bucket - the bucket to delete the website configuration from\n";
5457

5558
if (args.length < 1) {
5659
System.out.println(USAGE);
@@ -60,8 +63,9 @@ public static void main(String[] args)
6063
final String bucket_name = args[0];
6164

6265
System.out.format("Deleting website configuration for bucket: %s\n",
63-
bucket_name);
66+
bucket_name);
6467
deleteWebsiteConfig(bucket_name);
6568
System.out.println("Done!");
6669
}
6770
}
71+
// snippet-end:[s3.java1.s3_delete_website_config.complete]

java/example_code/s3/src/main/java/aws/example/s3/GetWebsiteConfiguration.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,50 @@
2121
specific language governing permissions and limitations under the License.
2222
*/
2323
package aws.example.s3;
24+
// snippet-start:[s3.java1.s3_get_website_config.import]
25+
2426
import com.amazonaws.services.s3.AmazonS3;
2527
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
2628
import com.amazonaws.services.s3.model.BucketWebsiteConfiguration;
2729
import com.amazonaws.AmazonServiceException;
30+
// snippet-end:[s3.java1.s3_get_website_config.import]
2831

2932
/**
3033
* Get the website configuration for an S3 bucket.
31-
*
34+
* <p>
3235
* This code expects that you have AWS credentials get up per:
3336
* http://docs.aws.amazon.com/java-sdk/latest/developer-guide/getup-credentials.html
3437
*/
35-
public class GetWebsiteConfiguration
36-
{
37-
public static void getWebsiteConfig(String bucket_name)
38-
{
38+
// snippet-start:[s3.java1.s3_get_website_config.complete]
39+
public class GetWebsiteConfiguration {
40+
public static void getWebsiteConfig(String bucket_name) {
41+
// snippet-start:[s3.java1.s3_get_website_config.main]
3942
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
4043
try {
4144
BucketWebsiteConfiguration config =
42-
s3.getBucketWebsiteConfiguration(bucket_name);
45+
s3.getBucketWebsiteConfiguration(bucket_name);
4346
if (config == null) {
4447
System.out.println("No website configuration found!");
4548
} else {
4649
System.out.format("Index document: %s\n",
47-
config.getIndexDocumentSuffix());
50+
config.getIndexDocumentSuffix());
4851
System.out.format("Error document: %s\n",
49-
config.getErrorDocument());
52+
config.getErrorDocument());
5053
}
5154
} catch (AmazonServiceException e) {
5255
System.err.println(e.getErrorMessage());
5356
System.out.println("Failed to get website configuration!");
5457
System.exit(1);
5558
}
59+
// snippet-end:[s3.java1.s3_get_website_config.main]
5660
}
5761

58-
public static void main(String[] args)
59-
{
62+
public static void main(String[] args) {
6063
final String USAGE = "\n" +
61-
"GetWebsiteConfiguration - get the website configuration for an S3 bucket\n\n" +
62-
"Usage: GetWebsiteConfiguration <bucket>\n\n" +
63-
"Where:\n" +
64-
" bucket - the bucket to get the website configuration from\n";
64+
"GetWebsiteConfiguration - get the website configuration for an S3 bucket\n\n" +
65+
"Usage: GetWebsiteConfiguration <bucket>\n\n" +
66+
"Where:\n" +
67+
" bucket - the bucket to get the website configuration from\n";
6568

6669
if (args.length < 1) {
6770
System.out.println(USAGE);
@@ -71,7 +74,8 @@ public static void main(String[] args)
7174
final String bucket_name = args[0];
7275

7376
System.out.format("Retrieving website configuration for bucket: %s\n",
74-
bucket_name);
77+
bucket_name);
7578
getWebsiteConfig(bucket_name);
7679
}
7780
}
81+
// snippet-end:[s3.java1.s3_get_website_config.complete]

java/example_code/s3/src/main/java/aws/example/s3/SetWebsiteConfiguration.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@
2323
specific language governing permissions and limitations under the License.
2424
*/
2525
package aws.example.s3;
26+
// snippet-start:[s3.java1.s3_set_website_config.import]
27+
2628
import com.amazonaws.services.s3.AmazonS3;
2729
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
2830
import com.amazonaws.services.s3.model.BucketWebsiteConfiguration;
2931
import com.amazonaws.AmazonServiceException;
32+
// snippet-end:[s3.java1.s3_set_website_config.import]
3033

3134
/**
3235
* Set the website configuration for an S3 bucket.
33-
*
36+
* <p>
3437
* This code expects that you have AWS credentials set up per:
3538
* http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
3639
*/
37-
public class SetWebsiteConfiguration
38-
{
40+
// snippet-start:[s3.java1.s3_set_website_config.complete]
41+
public class SetWebsiteConfiguration {
3942
public static void setWebsiteConfig(
40-
String bucket_name, String index_doc, String error_doc)
41-
{
43+
// snippet-start:[s3.java1.s3_set_website_config.main]
44+
String bucket_name, String index_doc, String error_doc) {
4245
BucketWebsiteConfiguration website_config = null;
4346

4447
if (index_doc == null) {
@@ -54,24 +57,24 @@ public static void setWebsiteConfig(
5457
s3.setBucketWebsiteConfiguration(bucket_name, website_config);
5558
} catch (AmazonServiceException e) {
5659
System.out.format(
57-
"Failed to set website configuration for bucket '%s'!\n",
58-
bucket_name);
60+
"Failed to set website configuration for bucket '%s'!\n",
61+
bucket_name);
5962
System.err.println(e.getErrorMessage());
6063
System.exit(1);
6164
}
65+
// snippet-end:[s3.java1.s3_set_website_config.main]
6266
}
6367

64-
public static void main(String[] args)
65-
{
68+
public static void main(String[] args) {
6669
final String USAGE = "\n" +
67-
"SetWebsiteConfiguration - set the website configuration for an S3 bucket\n\n" +
68-
"Usage: SetWebsiteConfiguration <bucket> [indexdoc] [errordoc]\n\n" +
69-
"Where:\n" +
70-
" bucket - the bucket to set the website configuration on\n" +
71-
" indexdoc - (optional) the index document, ex. 'index.html'\n" +
72-
" If not specified, 'index.html' will be set.\n" +
73-
" errordoc - (optional) the error document, ex. 'notfound.html'\n" +
74-
" If not specified, no error doc will be set.\n";
70+
"SetWebsiteConfiguration - set the website configuration for an S3 bucket\n\n" +
71+
"Usage: SetWebsiteConfiguration <bucket> [indexdoc] [errordoc]\n\n" +
72+
"Where:\n" +
73+
" bucket - the bucket to set the website configuration on\n" +
74+
" indexdoc - (optional) the index document, ex. 'index.html'\n" +
75+
" If not specified, 'index.html' will be set.\n" +
76+
" errordoc - (optional) the error document, ex. 'notfound.html'\n" +
77+
" If not specified, no error doc will be set.\n";
7578

7679
if (args.length < 1) {
7780
System.out.println(USAGE);
@@ -85,3 +88,4 @@ public static void main(String[] args)
8588
setWebsiteConfig(bucket_name, index_doc, error_doc);
8689
}
8790
}
91+
// snippet-end:[s3.java1.s3_set_website_config.complete]

0 commit comments

Comments
 (0)