Skip to content

Commit

Permalink
Merge pull request awsdocs#1245 from scmacdon/master
Browse files Browse the repository at this point in the history
Updated from S3 Java V2 examples
  • Loading branch information
brmur authored Jun 26, 2020
2 parents 2c7b6af + 3992418 commit 6bae2ae
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ public static void main(String[] args) {
//Create the S3Client object
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();

CopyBucketObject (s3, fromBucket, objectKey, toBucket);
copyBucketObject (s3, fromBucket, objectKey, toBucket);
}

// snippet-start:[s3.java2.copy_object.main]
public static String CopyBucketObject (S3Client s3, String fromBucket, String objectKey, String toBucket) {
public static String copyBucketObject (S3Client s3, String fromBucket, String objectKey, String toBucket) {

String encodedUrl = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public static void main(String[] args) {
S3Client s3 = S3Client.builder().region(region).build();

//Delete the bucket policy
DeleteS3BucketPolicy(s3, bucketName);
deleteS3BucketPolicy(s3, bucketName);
}
// snippet-start:[s3.java2.delete_bucket_policy.main]
public static void DeleteS3BucketPolicy(S3Client s3, String bucketName) {
public static void deleteS3BucketPolicy(S3Client s3, String bucketName) {

//Create a DeleteBucketPolicyRequest object
DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest;
import java.util.ArrayList;
import java.util.Arrays;
// snippet-end:[s3.java2.delete_objects.import]

/**
Expand Down Expand Up @@ -60,12 +59,11 @@ public static void main(String[] args) {
// Create a S3Client object
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();

DeleteBucketObjects(s3, bucketName, objectName);
deleteBucketObjects(s3, bucketName, objectName);
}

// snippet-start:[s3.java2.delete_objects.main]
public static void DeleteBucketObjects(S3Client s3, String bucketName, String objectName) {
public static void deleteBucketObjects(S3Client s3, String bucketName, String objectName) {

// Create a S3Client object
ArrayList<ObjectIdentifier> toDelete = new ArrayList<ObjectIdentifier>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public static void main(String[] args) {
// Create a S3Client object
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();

deleteBucketWebsiteConfig(s3, bucketName);
}

// snippet-start:[s3.java2.delete_website_configuration.main]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void main(String[] args) {
}

// snippet-start:[presigned.java2.generatepresignedurl.main]
public static void signBucket( S3Presigner presigner, String bucketName, String keyName) {
public static void signBucket(S3Presigner presigner, String bucketName, String keyName) {

try {
PresignedPutObjectRequest presignedRequest =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class GetAcl {

public static void main(String[] args) {
final String USAGE = "\n" +
"Usage:\n" +
"Usage: \n" +
" GetAcl <bucket> [object]\n\n" +
"Where:\n" +
" bucket - the bucket to get the access control list (ACL) for\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public static void main(String[] args) {
.region(region)
.build();

String polText = GetPolicy(s3, bucketName );
String polText = getPolicy(s3, bucketName );
System.out.println("Policy Text: "+polText);
}

// snippet-start:[s3.java2.get_bucket_policy.main]
public static String GetPolicy(S3Client s3, String bucketName) {
public static String getPolicy(S3Client s3, String bucketName) {

String policyText = "";
System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
// snippet-sourcedescription:[GetObjectData.java demonstrates how to read data from an Amazon S3 object.]
// snippet-service:[Amazon S3]
// snippet-keyword:[SDK for Java 2.0]
// snippet-keyword:[Amazon S3]
// snippet-keyword:[Code Sample]
// snippet-sourcetype:[full-example]
//snippet-sourcedate:[4/29/2020]
//snippet-sourceauthor:[scmacdon-aws]

/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This file is licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/
package com.example.s3;

// snippet-start:[s3.java2.getobjectdata.import]
import software.amazon.awssdk.core.ResponseBytes;
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.import]

public class GetObjectData {

public static void main(String[] args) {

if (args.length < 3) {
System.out.println("Please specify a bucket name, a key name that represents a PDF file (ie, book.pdf), and a path (ie, C:\\AWS\\AdobePDF.pdf)");
System.exit(1);
}

String bucketName = args[0];
String keyName = args[1];
String path = args[2];

Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder()
.region(region)
.build();

getObjectBytes(s3,bucketName,keyName, path);
}

// snippet-start:[s3.java2.getobjectdata.main]
public static void getObjectBytes (S3Client s3, String bucketName, String keyName, String path ) {

try {
// create a GetObjectRequest instance
GetObjectRequest objectRequest = GetObjectRequest
.builder()
.key(keyName)
.bucket(bucketName)
.build();

// get the byte[] this AWS S3 object
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
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");

// Close the file
os.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
// snippet-end:[s3.java2.getobjectdata.main]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class GetObjectPresignedUrl {

public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Please specify a bucket name and a key name");
System.out.println("Please specify a bucket name and key name");
System.exit(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public static void main(String[] args) {
.region(region)
.build();

ListTags(s3,bucketName,keyName );
listTags(s3,bucketName,keyName );
}

// snippet-start:[s3.java2.getobjecttags.main]
public static void ListTags (S3Client s3, String bucketName, String keyName ) {
public static void listTags(S3Client s3, String bucketName, String keyName ) {

try {
// create a GetObjectTaggingRequest instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ public static void main(String[] args) {
.region(region)
.build();

ListBucketObjects(s3, bucketName);

listBucketObjects(s3, bucketName);
}

// snippet-start:[s3.java2.list_objects.main]
public static void ListBucketObjects( S3Client s3, String bucketName ) {
public static void listBucketObjects(S3Client s3, String bucketName ) {

try {
ListObjectsRequest listObjects = ListObjectsRequest
Expand All @@ -69,7 +68,7 @@ public static void ListBucketObjects( S3Client s3, String bucketName ) {
System.out.print("\n The name of the key is " + myValue.key());
System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
System.out.print("\n The owner is " + myValue.owner());
}
}
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
Expand All @@ -78,7 +77,6 @@ public static void ListBucketObjects( S3Client s3, String bucketName ) {
//convert bytes to kbs
private static long calKb(Long val) {
return val/1024;

}
// snippet-end:[s3.java2.list_objects.main]
// snippet-end:[s3.java2.list_objects.main]
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void main(String[] args) {
}

String bucketName = args[0];
String objectKey = args[1];
String objectKey = args[1];
String objectPath = args[2];

System.out.println("Putting object " + objectKey +" into bucket "+bucketName);
Expand All @@ -66,7 +66,7 @@ public static void main(String[] args) {
}

// snippet-start:[s3.java2.s3_object_upload.main]
public static String putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) {
public static String putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) {

try {
//Put a file into the bucket
Expand All @@ -77,7 +77,6 @@ public static String putS3Object(S3Client s3, String bucketName, String objectK
RequestBody.fromBytes(getObjectFile(objectPath)));

return response.eTag();

} catch (S3Exception | FileNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void main(String[] args) {
"Examples:\n" +
" SetAcl testbucket testobject <uda0cb5d2c39f310136ead6278...> WRITE\n\n";

if (args.length < 3) {
if (args.length < 4) {
System.out.println(USAGE);
System.exit(1);
}
Expand All @@ -70,12 +70,12 @@ public static void main(String[] args) {
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();

SetBucketAcl(s3, bucketName, objectKey, id,access );
setBucketAcl(s3, bucketName, id,access);
System.out.println("Done!");
}

// snippet-start:[s3.java2.set_acl.main]
public static void SetBucketAcl(S3Client s3, String bucketName, String objectKey, String id,String access ) {
public static void setBucketAcl(S3Client s3, String bucketName, String id, String access) {

try {
// set access for the grantee in acl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public static void main(String[] args) {
}

String bucketName = args[0];
String policyText = "";
policyText = getBucketPolicyFromFile(args[1]);
String policyText = getBucketPolicyFromFile(args[1]);

System.out.println("Setting policy:");
System.out.println("----");
Expand All @@ -78,15 +77,15 @@ public static void main(String[] args) {
.build();

// Set the Bucket Policy
SetPolicy(s3, bucketName, policyText);
setPolicy(s3, bucketName, policyText);
}

// snippet-start:[s3.java2.set_bucket_policy.main]
public static void SetPolicy(S3Client s3, String bucketName, String policyText) {
public static void setPolicy(S3Client s3, String bucketName, String polText) {

try {

policyText = getBucketPolicyFromFile(policyText);
String policyText = getBucketPolicyFromFile(polText);
PutBucketPolicyRequest policyReq = PutBucketPolicyRequest.builder()
.bucket(bucketName)
.policy(policyText)
Expand All @@ -96,11 +95,9 @@ public static void SetPolicy(S3Client s3, String bucketName, String policyText)
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}

System.out.println("Done!");
}


// Loads a JSON-formatted policy from a file, verifying it with the Policy
// class.
private static String getBucketPolicyFromFile(String policFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void setWebsiteConfig(
s3.putBucketWebsite(pubWebsiteReq);
} catch (S3Exception e) {
System.out.format(
"Failed to set website configuration for bucket '%s'!\n",
"Failed to set the website configuration for bucket '%s'!\n",
bucketName);
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void putObject() {
@Order(4)
public void copyBucketObject() {

String result = CopyObject.CopyBucketObject(s3,bucketName,objectKey,toBucket);
String result = CopyObject.copyBucketObject(s3,bucketName,objectKey,toBucket);
assertTrue(!result.isEmpty());
System.out.println("Test 4 passed");
}
Expand All @@ -92,15 +92,15 @@ public void copyBucketObject() {
@Order(5)
public void setBucketPolicy() {

SetBucketPolicy.SetPolicy(s3, bucketName, policyText);
SetBucketPolicy.setPolicy(s3, bucketName, policyText);
System.out.println("Test 5 passed");
}

@Test
@Order(6)
public void getBucketPolicy() {

String polText = GetBucketPolicy.GetPolicy(s3, bucketName );
String polText = GetBucketPolicy.getPolicy(s3, bucketName );
assertTrue(!polText.isEmpty());
System.out.println("Test 6 passed");
}
Expand All @@ -109,7 +109,7 @@ public void getBucketPolicy() {
@Order(7)
public void deleteBucketPolicy() {

DeleteBucketPolicy.DeleteS3BucketPolicy(s3,bucketName );
DeleteBucketPolicy.deleteS3BucketPolicy(s3,bucketName );
System.out.println("Test 7 passed");
}

Expand All @@ -120,7 +120,7 @@ public void setBucketACL()
System.out.format("Running Amazon S3 Test 8");
System.out.println("for object: " + objectKey);
System.out.println(" in bucket: " + bucketName);
SetAcl.SetBucketAcl(s3, bucketName, objectKey, id,access );
SetAcl.setBucketAcl(s3, bucketName, id,access );
System.out.println("Test 8 passed");
}

Expand All @@ -137,7 +137,7 @@ public void getACL(){
@Order(10)
public void deleteObjects() {

DeleteObjects.DeleteBucketObjects(s3,bucketName,objectKey);
DeleteObjects.deleteBucketObjects(s3,bucketName,objectKey);
System.out.println("Test 10 passed");
}

Expand Down

0 comments on commit 6bae2ae

Please sign in to comment.