Skip to content

Commit 4b559e0

Browse files
committed
Merge branch 'master' into py_create_bucket
2 parents 47d87fe + 7ea7151 commit 4b559e0

File tree

8 files changed

+138
-50
lines changed

8 files changed

+138
-50
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/S3Encrypt.java

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
package aws.example.s3;
2424

25+
// snippet-start:[s3.java1.s3_encrypt.complete]
26+
// snippet-start:[s3.java1.s3_encrypt.import]
27+
2528
import com.amazonaws.regions.Region;
2629
import com.amazonaws.regions.Regions;
2730
import com.amazonaws.services.s3.AmazonS3;
@@ -34,28 +37,28 @@
3437
import com.amazonaws.services.s3.model.GetObjectRequest;
3538
import com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider;
3639
import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider;
40+
3741
import java.security.KeyPair;
3842
import java.security.KeyPairGenerator;
3943
import java.security.NoSuchAlgorithmException;
4044
import javax.crypto.KeyGenerator;
4145
import javax.crypto.SecretKey;
46+
// snippet-end:[s3.java1.s3_encrypt.import]
4247

4348
/**
4449
* Test out various cryptography settings for S3.
45-
*
50+
* <p>
4651
* This code expects that you have AWS credentials set up per:
4752
* http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
4853
* This code also requires you to install the Unlimited Strength Java(TM) Cryptography Extension Policy Files (JCE)
4954
* You can install this from the oracle site: http://www.oracle.com
5055
*/
51-
public class S3Encrypt
52-
{
56+
public class S3Encrypt {
5357
public static final String BUCKET_NAME = "s3EncryptTestBucket"; //add your bucket name
5458
public static final String ENCRYPTED_KEY = "enc-key";
5559
public static final String NON_ENCRYPTED_KEY = "some-key";
5660

57-
public static void main(String[] args)
58-
{
61+
public static void main(String[] args) {
5962
System.out.println("calling encryption with customer managed keys");
6063
S3Encrypt encrypt = new S3Encrypt();
6164

@@ -72,7 +75,9 @@ public static void main(String[] args)
7275
* encryption requires the bouncy castle provider to be on the classpath. Also, for authenticated encryption the size
7376
* of the data can be no longer than 64 GB.
7477
*/
78+
// snippet-start:[s3.java1.s3_encrypt.authenticated_encryption]
7579
public void authenticatedEncryption_CustomerManagedKey() throws NoSuchAlgorithmException {
80+
// snippet-start:[s3.java1.s3_encrypt.authenticated_encryption_build]
7681
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
7782
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
7883
.standard()
@@ -82,19 +87,23 @@ public void authenticatedEncryption_CustomerManagedKey() throws NoSuchAlgorithmE
8287
.build();
8388

8489
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
90+
// snippet-end:[s3.java1.s3_encrypt.authenticated_encryption_build]
8591

8692
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
8793
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
8894
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
8995
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
9096
}
97+
// snippet-end:[s3.java1.s3_encrypt.authenticated_encryption]
9198

9299
/**
93100
* For ranged GET we do not use authenticated encryption since we aren't reading the entire message and can't produce the
94101
* MAC. Instead we use AES/CTR, an unauthenticated encryption algorithm. If {@link CryptoMode#StrictAuthenticatedEncryption}
95102
* is enabled, ranged GETs will not be allowed since they do not use authenticated encryption..
96103
*/
104+
// snippet-start:[s3.java1.s3_encrypt.strict_authenticated_encryption]
97105
public void authenticatedEncryption_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmException {
106+
// snippet-start:[s3.java1.s3_encrypt.strict_authenticated_encryption_build]
98107
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
99108
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
100109
.standard()
@@ -104,12 +113,14 @@ public void authenticatedEncryption_RangeGet_CustomerManagedKey() throws NoSuchA
104113
.build();
105114

106115
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
116+
// snippet-end:[s3.java1.s3_encrypt.strict_authenticated_encryption_build]
107117

108118
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
109119
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
110120
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
111121
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
112122
}
123+
// snippet-end:[s3.java1.s3_encrypt.strict_authenticated_encryption]
113124

114125
/**
115126
* Same as {@link #authenticatedEncryption_CustomerManagedKey()} except uses an asymmetric key pair and
@@ -204,6 +215,7 @@ public void encryptionOnly_CustomerManagedKey() throws NoSuchAlgorithmException
204215
/**
205216
* Non-authenticated encryption schemes can do range GETs without an issue.
206217
*/
218+
// snippet-start:[s3.java1.s3_encrypt.encryption_only]
207219
public void encryptionOnly_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmException {
208220
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
209221
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
@@ -215,15 +227,18 @@ public void encryptionOnly_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmE
215227

216228
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
217229
System.out.println(s3Encryption.getObject(new GetObjectRequest(BUCKET_NAME, ENCRYPTED_KEY)
218-
.withRange(0, 2)));
230+
.withRange(0, 2)));
219231
}
232+
// snippet-end:[s3.java1.s3_encrypt.encryption_only]
220233

221234
/**
222235
* Uses an asymmetric key pair instead of a symmetric key. Note this does not change the algorithm used to encrypt
223236
* the content, that will still be a symmetric key algorithm (AES/CBC in this case) using the derived CEK. It does impact
224237
* the algorithm used to encrypt the CEK, in this case we use RSA/ECB/OAEPWithSHA-256AndMGF1Padding.
225238
*/
239+
// snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key]
226240
public void encryptionOnly_CustomerManagedAsymetricKey() throws NoSuchAlgorithmException {
241+
// snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_build]
227242
KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
228243
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
229244
.standard()
@@ -233,17 +248,25 @@ public void encryptionOnly_CustomerManagedAsymetricKey() throws NoSuchAlgorithmE
233248
.build();
234249

235250
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
251+
// snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_build]
236252

253+
// snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_put_object]
237254
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
238255
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
256+
// snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_put_object]
257+
// snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_retrieve]
239258
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
240259
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
260+
// snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_retrieve]
241261
}
262+
// snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key]
242263

243264
/**
244265
* This uses the V2 metadata schema with a key wrap algorithm of 'kms' and a CEK algorithm of AES/CBC/PKCS5Padding.
245266
*/
267+
// snippet-start:[s3.java1.s3_encrypt.kms_encryption_only]
246268
public void encryptionOnly_KmsManagedKey() throws NoSuchAlgorithmException {
269+
// snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_build]
247270
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
248271
.standard()
249272
.withRegion(Regions.US_WEST_2)
@@ -253,12 +276,18 @@ public void encryptionOnly_KmsManagedKey() throws NoSuchAlgorithmException {
253276
.build();
254277

255278
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
279+
// snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_build]
256280

281+
// snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_put_object]
257282
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
258283
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
284+
// snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_put_object]
285+
// snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_retrieve]
259286
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
260287
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
288+
// snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_retrieve]
261289
}
290+
// snippet-end:[s3.java1.s3_encrypt.kms_encryption_only]
262291

263292
/**
264293
* This uses the V2 metadata schema with a key wrap algorithm of 'kms' and a CEK algorithm of AES/GCM/NoPadding.
@@ -284,7 +313,9 @@ public void authenticatedEncryption_KmsManagedKey() throws NoSuchAlgorithmExcept
284313
* Same as authenticatedEncryption_KmsManagedKey except throws an exception when trying to get objects not encrypted with
285314
* AES/GCM.
286315
*/
316+
// snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption]
287317
public void strictAuthenticatedEncryption_KmsManagedKey() throws NoSuchAlgorithmException {
318+
// snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_builder]
288319
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
289320
.standard()
290321
.withRegion(Regions.US_WEST_2)
@@ -294,14 +325,23 @@ public void strictAuthenticatedEncryption_KmsManagedKey() throws NoSuchAlgorithm
294325
.build();
295326

296327
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
328+
// snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_builder]
297329

330+
// snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_put_object]
298331
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
299332
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
333+
// snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_put_object]
334+
// snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_exception]
300335
try {
301336
s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY);
302337
} catch (SecurityException e) {
303338
// Strict authenticated encryption will throw an exception if an object is not encrypted with AES/GCM
304339
System.err.println(NON_ENCRYPTED_KEY + " was not encrypted with AES/GCM");
305340
}
341+
342+
// snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_exception]
306343
}
344+
// snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption]
345+
307346
}
347+
// snippet-end:[s3.java1.s3_encrypt.complete]

0 commit comments

Comments
 (0)