forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addressed some S3 tickets (awsdocs#4724)
- Loading branch information
Showing
4 changed files
with
203 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
javav2/example_code/s3/src/main/java/com/example/s3/GetDataResponseTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] | ||
// snippet-sourcedescription:[GetDataResponseTransformer.java demonstrates how to read data from an Amazon Simple Storage Service (Amazon S3) object using a ResponseTransformer.] | ||
//snippet-keyword:[AWS SDK for Java v2] | ||
//snippet-service:[Amazon S3] | ||
|
||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.example.s3; | ||
|
||
// snippet-start:[s3.java2.getobjectdata.transformer.import] | ||
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; | ||
import software.amazon.awssdk.core.ResponseBytes; | ||
import software.amazon.awssdk.core.sync.ResponseTransformer; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
import software.amazon.awssdk.services.s3.model.GetObjectResponse; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
// snippet-end:[s3.java2.getobjectdata.transformer.import] | ||
|
||
/** | ||
* Before running this Java V2 code example, set up your development environment, including your credentials. | ||
* | ||
* For more information, see the following documentation topic: | ||
* | ||
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | ||
*/ | ||
|
||
public class GetDataResponseTransformer { | ||
|
||
public static void main(String[] args) { | ||
|
||
final String usage = "\n" + | ||
"Usage:\n" + | ||
" <bucketName> <keyName> <path>\n\n" + | ||
"Where:\n" + | ||
" bucketName - The Amazon S3 bucket name. \n\n"+ | ||
" keyName - The key name. \n\n"+ | ||
" path - The path where the file is written to. \n\n"; | ||
|
||
if (args.length != 3) { | ||
System.out.println(usage); | ||
System.exit(1); | ||
} | ||
|
||
String bucketName = args[0]; | ||
String keyName = args[1]; | ||
String path = args[2]; | ||
|
||
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create(); | ||
Region region = Region.US_EAST_1; | ||
S3Client s3 = S3Client.builder() | ||
.region(region) | ||
.credentialsProvider(credentialsProvider) | ||
.build(); | ||
|
||
getObjectBytes(s3,bucketName,keyName, path); | ||
s3.close(); | ||
} | ||
|
||
// snippet-start:[s3.java2.getobjectdata.transformer.main] | ||
public static void getObjectBytes (S3Client s3, String bucketName, String keyName, String path) { | ||
try { | ||
GetObjectRequest objectRequest = GetObjectRequest | ||
.builder() | ||
.key(keyName) | ||
.bucket(bucketName) | ||
.build(); | ||
|
||
ResponseBytes<GetObjectResponse> objectBytes = s3.getObject(objectRequest, ResponseTransformer.toBytes()); | ||
byte[] data = objectBytes.asByteArray(); | ||
|
||
// Write the data to a local file. | ||
File myFile = new File(path ); | ||
OutputStream os = new FileOutputStream(myFile); | ||
os.write(data); | ||
System.out.println("Successfully obtained bytes from an S3 object"); | ||
os.close(); | ||
|
||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} catch (S3Exception e) { | ||
System.err.println(e.awsErrorDetails().errorMessage()); | ||
System.exit(1); | ||
} | ||
} | ||
// snippet-end:[s3.java2.getobjectdata.transformer.main] | ||
} |
88 changes: 88 additions & 0 deletions
88
javav2/example_code/s3/src/main/java/com/example/s3/SetBucketEventBridgeNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//snippet-sourcedescription:[SetBucketEventBridgeNotification.java demonstrates how to enable notifications of specified events for an Amazon Simple Storage Service (Amazon S3) bucket.] | ||
//snippet-keyword:[AWS SDK for Java v2] | ||
//snippet-service:[Amazon S3] | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.example.s3; | ||
|
||
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.Event; | ||
import software.amazon.awssdk.services.s3.model.NotificationConfiguration; | ||
import software.amazon.awssdk.services.s3.model.PutBucketNotificationConfigurationRequest; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
import software.amazon.awssdk.services.s3.model.TopicConfiguration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class SetBucketEventBridgeNotification { | ||
|
||
public static void main(String[] args) { | ||
|
||
final String usage = "\n" + | ||
"Usage:\n" + | ||
" <bucketName> \n\n" + | ||
"Where:\n" + | ||
" bucketName - The Amazon S3 bucket. \n\n" + | ||
" topicArn - The Simple Notification Service topic ARN. \n\n" + | ||
" id - An id value used for the topic configuration. This value is displayed in the AWS Management Console. \n\n" ; | ||
|
||
if (args.length != 3) { | ||
System.out.println(usage); | ||
System.exit(1); | ||
} | ||
|
||
String bucketName = args[0]; | ||
String topicArn = args[1]; | ||
String id = args[2]; | ||
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create(); | ||
Region region = Region.US_EAST_1; | ||
S3Client s3Client = S3Client.builder() | ||
.region(region) | ||
.credentialsProvider(credentialsProvider) | ||
.build(); | ||
|
||
setBucketNotification(s3Client, bucketName, topicArn, id); | ||
s3Client.close(); | ||
} | ||
|
||
// snippet-start:[s3.java2.s3_enable_notifications.main] | ||
public static void setBucketNotification(S3Client s3Client, String bucketName, String topicArn, String id) { | ||
try { | ||
List<Event> events = new ArrayList<>(); | ||
events.add(Event.S3_OBJECT_CREATED_PUT); | ||
|
||
TopicConfiguration config = TopicConfiguration.builder() | ||
.topicArn(topicArn) | ||
.events(events) | ||
.id(id) | ||
.build(); | ||
|
||
List<TopicConfiguration> topics = new ArrayList<>(); | ||
topics.add(config); | ||
|
||
NotificationConfiguration configuration = NotificationConfiguration.builder() | ||
.topicConfigurations(topics) | ||
.build(); | ||
|
||
PutBucketNotificationConfigurationRequest configurationRequest = PutBucketNotificationConfigurationRequest.builder() | ||
.bucket(bucketName) | ||
.notificationConfiguration(configuration) | ||
.skipDestinationValidation(true) | ||
.build(); | ||
|
||
// Set the bucket notification configuration. | ||
s3Client.putBucketNotificationConfiguration(configurationRequest); | ||
System.out.println("Added bucket " + bucketName + " with EventBridge events enabled."); | ||
|
||
} catch (S3Exception e) { | ||
System.err.println(e.awsErrorDetails().errorMessage()); | ||
System.exit(1); | ||
} | ||
} | ||
// snippet-end:[s3.java2.s3_enable_notifications.main] | ||
} |