Skip to content

Commit

Permalink
Addressed some S3 tickets (awsdocs#4724)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon authored Apr 21, 2023
1 parent b19f957 commit f1f956d
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 21 deletions.
20 changes: 20 additions & 0 deletions .doc_gen/metadata/s3_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,9 @@ s3_GetObject:
- description: Get an object by using the S3Presigner client object using an <ulink url="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Client.html">S3Client</ulink>.
snippet_tags:
- presigned.java2.getobjectpresigned.main
- description: Get an object by using a ResponseTransformer object and <ulink url="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Client.html">S3Client</ulink>.
snippet_tags:
- s3.java2.getobjectdata.transformer.main
PHP:
versions:
- sdk_version: 3
Expand Down Expand Up @@ -862,6 +865,23 @@ s3_GetBucketLifecycleConfiguration:
- python.example_code.s3.GetBucketLifecycleConfiguration
services:
s3: {GetBucketLifecycleConfiguration}
s3_SetBucketNotification:
title: Enable notifications of specified events on an &S3; bucket using an &AWS; SDK
title_abbrev: Enable notifications
synopsis: enable notifications on an S3 bucket.
category:
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/s3
sdkguide:
excerpts:
- description:
snippet_tags:
- s3.java2.s3_enable_notifications.main
services:
s3: {PutBucketLogging}
s3_ServiceAccessLogging:
title: Enable logging on an &S3; bucket using an &AWS; SDK
title_abbrev: Enable logging
Expand Down
21 changes: 0 additions & 21 deletions javav2/example_code/s3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,6 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
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]
}
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]
}

0 comments on commit f1f956d

Please sign in to comment.