Amazon S3 examples using SDK for Java 2.x - AWS SDK for Java

You can now use the Amazon S3 Transfer Manager (Developer Preview) in the AWS SDK for Java 2.x for accelerated file transfers. Give it a try and let us know what you think! By the way, the AWS SDK for Java team is hiring software development engineers!

Amazon S3 examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Java 2.x with Amazon S3.

Actions are code excerpts that show you how to call individual Amazon S3 functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Amazon S3 functions.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Actions

The following code example shows how to add cross-origin resource sharing (CORS) rules to an Amazon S3 bucket.

SDK for Java 2.x

public static void deleteBucketCorsInformation(S3Client s3, String bucketName, String accountId) { try { DeleteBucketCorsRequest bucketCorsRequest = DeleteBucketCorsRequest.builder() .bucket(bucketName) .expectedBucketOwner(accountId) .build(); s3.deleteBucketCors(bucketCorsRequest) ; } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void getBucketCorsInformation(S3Client s3, String bucketName, String accountId) { try { GetBucketCorsRequest bucketCorsRequest = GetBucketCorsRequest.builder() .bucket(bucketName) .expectedBucketOwner(accountId) .build(); GetBucketCorsResponse corsResponse = s3.getBucketCors(bucketCorsRequest); List<CORSRule> corsRules = corsResponse.corsRules(); for (CORSRule rule: corsRules) { System.out.println("allowOrigins: "+rule.allowedOrigins()); System.out.println("AllowedMethod: "+rule.allowedMethods()); } } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void setCorsInformation(S3Client s3, String bucketName, String accountId) { List<String> allowMethods = new ArrayList(); allowMethods.add("PUT"); allowMethods.add("POST"); allowMethods.add("DELETE"); List<String> allowOrigins = new ArrayList(); allowOrigins.add("http://example.com"); try { // Define CORS rules. CORSRule corsRule = CORSRule.builder() .allowedMethods(allowMethods) .allowedOrigins(allowOrigins) .build(); List<CORSRule> corsRules = new ArrayList<>(); corsRules.add(corsRule); CORSConfiguration configuration = CORSConfiguration.builder() .corsRules(corsRules) .build(); PutBucketCorsRequest putBucketCorsRequest = PutBucketCorsRequest.builder() .bucket(bucketName) .corsConfiguration(configuration) .expectedBucketOwner(accountId) .build(); s3.putBucketCors(putBucketCorsRequest); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see PutBucketCors in AWS SDK for Java 2.x API Reference.

The following code example shows how to add a lifecycle configuration to an Amazon S3 bucket.

SDK for Java 2.x

public static void setLifecycleConfig(S3Client s3, String bucketName, String accountId) { try { // Create a rule to archive objects with the "glacierobjects/" prefix to Amazon S3 Glacier. LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder() .prefix("glacierobjects/") .build(); Transition transition = Transition.builder() .storageClass(TransitionStorageClass.GLACIER) .days(0) .build(); LifecycleRule rule1 = LifecycleRule.builder() .id("Archive immediately rule") .filter(ruleFilter) .transitions(transition) .status(ExpirationStatus.ENABLED) .build(); // Create a second rule. Transition transition2 = Transition.builder() .storageClass(TransitionStorageClass.GLACIER) .days(0) .build(); List<Transition> transitionList = new ArrayList<>(); transitionList.add(transition2); LifecycleRuleFilter ruleFilter2 = LifecycleRuleFilter.builder() .prefix("glacierobjects/") .build(); LifecycleRule rule2 = LifecycleRule.builder() .id("Archive and then delete rule") .filter(ruleFilter2) .transitions(transitionList) .status(ExpirationStatus.ENABLED) .build(); // Add the LifecycleRule objects to an ArrayList. ArrayList<LifecycleRule> ruleList = new ArrayList<>(); ruleList.add(rule1); ruleList.add(rule2); BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder() .rules(ruleList) .build(); PutBucketLifecycleConfigurationRequest putBucketLifecycleConfigurationRequest = PutBucketLifecycleConfigurationRequest.builder() .bucket(bucketName) .lifecycleConfiguration(lifecycleConfiguration) .expectedBucketOwner(accountId) .build(); s3.putBucketLifecycleConfiguration(putBucketLifecycleConfigurationRequest); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } // Retrieve the configuration and add a new rule. public static void getLifecycleConfig(S3Client s3, String bucketName, String accountId){ try { GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest = GetBucketLifecycleConfigurationRequest.builder() .bucket(bucketName) .expectedBucketOwner(accountId) .build(); GetBucketLifecycleConfigurationResponse response = s3.getBucketLifecycleConfiguration(getBucketLifecycleConfigurationRequest); // Create a new List. List<LifecycleRule> newList = new ArrayList<>(); List<LifecycleRule> rules = response.rules(); for (LifecycleRule rule: rules) { newList.add(rule); } // Add a new rule with both a prefix predicate and a tag predicate. LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder() .prefix("YearlyDocuments/") .build(); Transition transition = Transition.builder() .storageClass(TransitionStorageClass.GLACIER) .days(3650) .build(); LifecycleRule rule1 = LifecycleRule.builder() .id("NewRule") .filter(ruleFilter) .transitions(transition) .status(ExpirationStatus.ENABLED) .build(); // Add the new rule to the list. newList.add(rule1); BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder() .rules(newList) .build(); PutBucketLifecycleConfigurationRequest putBucketLifecycleConfigurationRequest = PutBucketLifecycleConfigurationRequest.builder() .bucket(bucketName) .lifecycleConfiguration(lifecycleConfiguration) .expectedBucketOwner(accountId) .build(); s3.putBucketLifecycleConfiguration(putBucketLifecycleConfigurationRequest); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } // Delete the configuration from the Amazon S3 bucket. public static void deleteLifecycleConfig(S3Client s3, String bucketName, String accountId) { try { DeleteBucketLifecycleRequest deleteBucketLifecycleRequest = DeleteBucketLifecycleRequest.builder() .bucket(bucketName) .expectedBucketOwner(accountId) .build(); s3.deleteBucketLifecycle(deleteBucketLifecycleRequest); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

The following code example shows how to add a policy to an Amazon S3 bucket.

SDK for Java 2.x

public static void setPolicy(S3Client s3, String bucketName, String policyText) { System.out.println("Setting policy:"); System.out.println("----"); System.out.println(policyText); System.out.println("----"); System.out.format("On Amazon S3 bucket: \"%s\"\n", bucketName); try { PutBucketPolicyRequest policyReq = PutBucketPolicyRequest.builder() .bucket(bucketName) .policy(policyText) .build(); s3.putBucketPolicy(policyReq); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.println("Done!"); } // Loads a JSON-formatted policy from a file public static String getBucketPolicyFromFile(String policyFile) { StringBuilder fileText = new StringBuilder(); try { List<String> lines = Files.readAllLines( Paths.get(policyFile), Charset.forName("UTF-8")); for (String line : lines) { fileText.append(line); } } catch (IOException e) { System.out.format("Problem reading file: \"%s\"", policyFile); System.out.println(e.getMessage()); } try { final JsonParser parser = new ObjectMapper().getFactory().createParser(fileText.toString()); while (parser.nextToken() != null) { } } catch (JsonParseException jpe) { jpe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } return fileText.toString(); }
  • Find instructions and more code on GitHub.

  • For API details, see PutBucketPolicy in AWS SDK for Java 2.x API Reference.

The following code example shows how to copy an Amazon S3 object from one bucket to another.

SDK for Java 2.x

public static String copyBucketObject (S3Client s3, String fromBucket, String objectKey, String toBucket) { String encodedUrl = ""; try { encodedUrl = URLEncoder.encode(fromBucket + "/" + objectKey, StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException e) { System.out.println("URL could not be encoded: " + e.getMessage()); } CopyObjectRequest copyReq = CopyObjectRequest.builder() .copySource(encodedUrl) .destinationBucket(toBucket) .destinationKey(objectKey) .build(); try { CopyObjectResponse copyRes = s3.copyObject(copyReq); return copyRes.copyObjectResult().toString(); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • Find instructions and more code on GitHub.

  • For API details, see CopyObject in AWS SDK for Java 2.x API Reference.

The following code example shows how to create an Amazon S3 bucket.

SDK for Java 2.x

public static void createBucket( S3Client s3Client, String bucketName) { try { S3Waiter s3Waiter = s3Client.waiter(); CreateBucketRequest bucketRequest = CreateBucketRequest.builder() .bucket(bucketName) .build(); s3Client.createBucket(bucketRequest); HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder() .bucket(bucketName) .build(); // Wait until the bucket is created and print out the response. WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println(bucketName +" is ready"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see CreateBucket in AWS SDK for Java 2.x API Reference.

The following code example shows how to delete a policy from an Amazon S3 bucket.

SDK for Java 2.x

// Delete the bucket policy. public static void deleteS3BucketPolicy(S3Client s3, String bucketName) { DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder() .bucket(bucketName) .build(); try { s3.deleteBucketPolicy(delReq); System.out.println("Done!"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see DeleteBucketPolicy in AWS SDK for Java 2.x API Reference.

The following code example shows how to delete an empty Amazon S3 bucket.

SDK for Java 2.x

public static void listAllObjects(S3Client s3, String bucket) { try { // To delete a bucket, all the objects in the bucket must be deleted first ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket).build(); ListObjectsV2Response listObjectsV2Response; do { listObjectsV2Response = s3.listObjectsV2(listObjectsV2Request); for (S3Object s3Object : listObjectsV2Response.contents()) { s3.deleteObject(DeleteObjectRequest.builder() .bucket(bucket) .key(s3Object.key()) .build()); } listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket) .continuationToken(listObjectsV2Response.nextContinuationToken()) .build(); } while(listObjectsV2Response.isTruncated());
  • Find instructions and more code on GitHub.

  • For API details, see DeleteBucket in AWS SDK for Java 2.x API Reference.

The following code example shows how to delete multiple objects from an Amazon S3 bucket.

SDK for Java 2.x

public static void deleteBucketObjects(S3Client s3, String bucketName) { // Upload three sample objects to the specfied Amazon S3 bucket. ArrayList<ObjectIdentifier> keys = new ArrayList<>(); PutObjectRequest putOb; ObjectIdentifier objectId; for (int i = 0; i < 3; i++) { String keyName = "delete object example " + i; objectId = ObjectIdentifier.builder() .key(keyName) .build(); putOb = PutObjectRequest.builder() .bucket(bucketName) .key(keyName) .build(); s3.putObject(putOb, RequestBody.fromString(keyName)); keys.add(objectId); } System.out.println(keys.size() + " objects successfully created."); // Delete multiple objects in one request. Delete del = Delete.builder() .objects(keys) .build(); try { DeleteObjectsRequest multiObjectDeleteRequest = DeleteObjectsRequest.builder() .bucket(bucketName) .delete(del) .build(); s3.deleteObjects(multiObjectDeleteRequest); System.out.println("Multiple objects are deleted!"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see DeleteObjects in AWS SDK for Java 2.x API Reference.

The following code example shows how to delete the website configuration from an Amazon S3 bucket.

SDK for Java 2.x

public static void deleteBucketWebsiteConfig(S3Client s3,String bucketName ) { DeleteBucketWebsiteRequest delReq = DeleteBucketWebsiteRequest.builder() .bucket(bucketName) .build(); try { s3.deleteBucketWebsite(delReq); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.out.println("Failed to delete website configuration!"); System.exit(1); } }

The following code example shows how to determine the existence and content type of an object in an Amazon S3 bucket.

SDK for Java 2.x

public static void getContentType (S3Client s3, String bucketName, String keyName) { try { HeadObjectRequest objectRequest = HeadObjectRequest.builder() .key(keyName) .bucket(bucketName) .build(); HeadObjectResponse objectHead = s3.headObject(objectRequest); String type = objectHead.contentType(); System.out.println("The object content type is "+type); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see HeadObject in AWS SDK for Java 2.x API Reference.

The following code example shows how to read data from an object in an Amazon S3 bucket.

SDK for Java 2.x

Read data as a byte array.

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.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"); os.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

Read tags that belong to an object.

public static void listTags(S3Client s3, String bucketName, String keyName ) { try { GetObjectTaggingRequest getTaggingRequest = GetObjectTaggingRequest .builder() .key(keyName) .bucket(bucketName) .build(); GetObjectTaggingResponse tags = s3.getObjectTagging(getTaggingRequest); List<Tag> tagSet= tags.tagSet(); for (Tag tag : tagSet) { System.out.println(tag.key()); System.out.println(tag.value()); } } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

Get a URL for an object.

public static void getURL(S3Client s3, String bucketName, String keyName ) { try { GetUrlRequest request = GetUrlRequest.builder() .bucket(bucketName) .key(keyName) .build(); URL url = s3.utilities().getUrl(request); System.out.println("The URL for "+keyName +" is "+ url); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

Get an object by using the S3Presigner client object.

public static void getPresignedUrl(S3Presigner presigner, String bucketName, String keyName ) { try { GetObjectRequest getObjectRequest = GetObjectRequest.builder() .bucket(bucketName) .key(keyName) .build(); GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder() .signatureDuration(Duration.ofMinutes(60)) .getObjectRequest(getObjectRequest) .build(); PresignedGetObjectRequest presignedGetObjectRequest = presigner.presignGetObject(getObjectPresignRequest); String theUrl = presignedGetObjectRequest.url().toString(); System.out.println("Presigned URL: " + theUrl); HttpURLConnection connection = (HttpURLConnection) presignedGetObjectRequest.url().openConnection(); presignedGetObjectRequest.httpRequest().headers().forEach((header, values) -> { values.forEach(value -> { connection.addRequestProperty(header, value); }); }); // Send any request payload that the service needs (not needed when isBrowserExecutable is true) if (presignedGetObjectRequest.signedPayload().isPresent()) { connection.setDoOutput(true); try (InputStream signedPayload = presignedGetObjectRequest.signedPayload().get().asInputStream(); OutputStream httpOutputStream = connection.getOutputStream()) { IoUtils.copy(signedPayload, httpOutputStream); } } // Download the result of executing the request try (InputStream content = connection.getInputStream()) { System.out.println("Service returned response: "); IoUtils.copy(content, System.out); } } catch (S3Exception | IOException e) { e.getStackTrace(); } }
  • Find instructions and more code on GitHub.

  • For API details, see GetObject in AWS SDK for Java 2.x API Reference.

The following code example shows how to get the access control list (ACL) of an Amazon S3 bucket.

SDK for Java 2.x

public static String getBucketACL(S3Client s3, String objectKey,String bucketName) { try { GetObjectAclRequest aclReq = GetObjectAclRequest.builder() .bucket(bucketName) .key(objectKey) .build(); GetObjectAclResponse aclRes = s3.getObjectAcl(aclReq); List<Grant> grants = aclRes.grants(); String grantee = ""; for (Grant grant : grants) { System.out.format(" %s: %s\n", grant.grantee().id(), grant.permission()); grantee = grant.grantee().id(); } return grantee; } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • Find instructions and more code on GitHub.

  • For API details, see GetBucketAcl in AWS SDK for Java 2.x API Reference.

The following code example shows how to get the policy for an Amazon S3 bucket.

SDK for Java 2.x

public static String getPolicy(S3Client s3, String bucketName) { String policyText; System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName); GetBucketPolicyRequest policyReq = GetBucketPolicyRequest.builder() .bucket(bucketName) .build(); try { GetBucketPolicyResponse policyRes = s3.getBucketPolicy(policyReq); policyText = policyRes.policy(); return policyText; } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • Find instructions and more code on GitHub.

  • For API details, see GetBucketPolicy in AWS SDK for Java 2.x API Reference.

The following code example shows how to list in-progress multipart uploads to an Amazon S3 bucket.

SDK for Java 2.x

public static void listUploads( S3Client s3, String bucketName) { try { ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder() .bucket(bucketName) .build(); ListMultipartUploadsResponse response = s3.listMultipartUploads(listMultipartUploadsRequest); List<MultipartUpload> uploads = response.uploads(); for (MultipartUpload upload: uploads) { System.out.println("Upload in progress: Key = \"" + upload.key() + "\", id = " + upload.uploadId()); } } catch (S3Exception e) { System.err.println(e.getMessage()); System.exit(1); } }

The following code example shows how to list objects in an Amazon S3 bucket.

SDK for Java 2.x

public static void listBucketObjects(S3Client s3, String bucketName ) { try { ListObjectsRequest listObjects = ListObjectsRequest .builder() .bucket(bucketName) .build(); ListObjectsResponse res = s3.listObjects(listObjects); List<S3Object> objects = res.contents(); for (S3Object myValue : objects) { 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); } } //convert bytes to kbs private static long calKb(Long val) { return val/1024; }
  • Find instructions and more code on GitHub.

  • For API details, see ListObjects in AWS SDK for Java 2.x API Reference.

The following code example shows how to restore an archived copy of an object back into an Amazon S3 bucket.

SDK for Java 2.x

public static void restoreS3Object(S3Client s3, String bucketName, String keyName, String expectedBucketOwner) { try { RestoreRequest restoreRequest = RestoreRequest.builder() .days(10) .glacierJobParameters(GlacierJobParameters.builder().tier(Tier.STANDARD).build()) .build(); RestoreObjectRequest objectRequest = RestoreObjectRequest.builder() .expectedBucketOwner(expectedBucketOwner) .bucket(bucketName) .key(keyName) .restoreRequest(restoreRequest) .build(); s3.restoreObject(objectRequest); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } s3.close(); }
  • Find instructions and more code on GitHub.

  • For API details, see RestoreObject in AWS SDK for Java 2.x API Reference.

The following code example shows how to set a new access control list (ACL) for an Amazon S3 bucket.

SDK for Java 2.x

public static void setBucketAcl(S3Client s3, String bucketName, String id) { try { Grant ownerGrant = Grant.builder() .grantee(builder -> { builder.id(id) .type(Type.CANONICAL_USER); }) .permission(Permission.FULL_CONTROL) .build(); List<Grant> grantList2 = new ArrayList<>(); grantList2.add(ownerGrant); AccessControlPolicy acl = AccessControlPolicy.builder() .owner(builder -> builder.id(id)) .grants(grantList2) .build(); PutBucketAclRequest putAclReq = PutBucketAclRequest.builder() .bucket(bucketName) .accessControlPolicy(acl) .build(); s3.putBucketAcl(putAclReq); } catch (S3Exception e) { e.printStackTrace(); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see PutBucketAcl in AWS SDK for Java 2.x API Reference.

The following code example shows how to set the website configuration for an Amazon S3 bucket.

SDK for Java 2.x

public static void setWebsiteConfig( S3Client s3, String bucketName, String indexDoc) { try { WebsiteConfiguration websiteConfig = WebsiteConfiguration.builder() .indexDocument(IndexDocument.builder().suffix(indexDoc).build()) .build(); PutBucketWebsiteRequest pubWebsiteReq = PutBucketWebsiteRequest.builder() .bucket(bucketName) .websiteConfiguration(websiteConfig) .build(); s3.putBucketWebsite(pubWebsiteReq); System.out.println("The call was successful"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see PutBucketWebsite in AWS SDK for Java 2.x API Reference.

The following code example shows how to upload an object to an Amazon S3 bucket.

SDK for Java 2.x

Upload an object to a bucket.

public static String putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) { try { Map<String, String> metadata = new HashMap<>(); metadata.put("x-amz-meta-myVal", "test"); PutObjectRequest putOb = PutObjectRequest.builder() .bucket(bucketName) .key(objectKey) .metadata(metadata) .build(); PutObjectResponse response = s3.putObject(putOb, RequestBody.fromBytes(getObjectFile(objectPath))); return response.eTag(); } catch (S3Exception e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } // Return a byte array. private static byte[] getObjectFile(String filePath) { FileInputStream fileInputStream = null; byte[] bytesArray = null; try { File file = new File(filePath); bytesArray = new byte[(int) file.length()]; fileInputStream = new FileInputStream(file); fileInputStream.read(bytesArray); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return bytesArray; }

Upload an object to a bucket and set tags.

public static void putS3ObjectTags(S3Client s3, String bucketName, String objectKey, String objectPath) { try { // Define the tags. Tag tag1 = Tag.builder() .key("Tag 1") .value("This is tag 1") .build(); Tag tag2 = Tag.builder() .key("Tag 2") .value("This is tag 2") .build(); List<Tag> tags = new ArrayList<>(); tags.add(tag1); tags.add(tag2); Tagging allTags = Tagging.builder() .tagSet(tags) .build(); PutObjectRequest putOb = PutObjectRequest.builder() .bucket(bucketName) .key(objectKey) .tagging(allTags) .build(); s3.putObject(putOb, RequestBody.fromBytes(getObjectFile(objectPath))); } catch (S3Exception e) { System.err.println(e.getMessage()); System.exit(1); } } public static void updateObjectTags(S3Client s3, String bucketName, String objectKey) { try { // Retrieve the object's tags. GetObjectTaggingRequest taggingRequest = GetObjectTaggingRequest.builder() .bucket(bucketName) .key(objectKey) .build(); GetObjectTaggingResponse getTaggingRes = s3.getObjectTagging(taggingRequest); // Write out the tags. List<Tag> obTags = getTaggingRes.tagSet(); for (Tag sinTag: obTags) { System.out.println("The tag key is: "+sinTag.key()); System.out.println("The tag value is: "+sinTag.value()); } // Replace the object's tags with two new tags. Tag tag3 = Tag.builder() .key("Tag 3") .value("This is tag 3") .build(); Tag tag4 = Tag.builder() .key("Tag 4") .value("This is tag 4") .build(); List<Tag> tags = new ArrayList<>(); tags.add(tag3); tags.add(tag4); Tagging updatedTags = Tagging.builder() .tagSet(tags) .build(); PutObjectTaggingRequest taggingRequest1 = PutObjectTaggingRequest.builder() .bucket(bucketName) .key(objectKey) .tagging(updatedTags) .build(); s3.putObjectTagging(taggingRequest1); // Write out the modified tags. GetObjectTaggingResponse getTaggingRes2 = s3.getObjectTagging(taggingRequest); List<Tag> modTags = getTaggingRes2.tagSet(); for (Tag sinTag: modTags) { System.out.println("The tag key is: "+sinTag.key()); System.out.println("The tag value is: "+sinTag.value()); } } catch (S3Exception e) { System.err.println(e.getMessage()); System.exit(1); } }

Upload an object to a bucket and set metadata.

public static String putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) { try { // Define the metadata Map<String, String> metadata = new HashMap<>(); metadata.put("author", "Mary Doe"); metadata.put("version", "1.0.0.0"); PutObjectRequest putOb = PutObjectRequest.builder() .bucket(bucketName) .key(objectKey) .metadata(metadata) .build(); PutObjectResponse response = s3.putObject(putOb, RequestBody.fromBytes(getObjectFile(objectPath))); return response.eTag(); } catch (S3Exception e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } // Return a byte array private static byte[] getObjectFile(String filePath) { FileInputStream fileInputStream = null; byte[] bytesArray = null; try { File file = new File(filePath); bytesArray = new byte[(int) file.length()]; fileInputStream = new FileInputStream(file); fileInputStream.read(bytesArray); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return bytesArray; }

Upload an object to a bucket and set an object retention value.

public static void setRentionPeriod(S3Client s3, String key, String bucket) { try{ LocalDate localDate = LocalDate.parse("2020-07-17"); LocalDateTime localDateTime = localDate.atStartOfDay(); Instant instant = localDateTime.toInstant(ZoneOffset.UTC); ObjectLockRetention lockRetention = ObjectLockRetention.builder() .mode("COMPLIANCE") .retainUntilDate(instant) .build(); PutObjectRetentionRequest retentionRequest = PutObjectRetentionRequest.builder() .bucket(bucket) .key(key) .bypassGovernanceRetention(true) .retention(lockRetention) .build(); /** * To set Retention on an object, the Amazon S3 bucket must support object locking, otherwise an exception is thrown. */ s3.putObjectRetention(retentionRequest); System.out.print("An object retention configuration was successfully placed on the object"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see PutObject in AWS SDK for Java 2.x API Reference.

Scenarios

The following code example shows how to create a presigned URL for Amazon S3 and upload an object.

SDK for Java 2.x

public static void signBucket(S3Presigner presigner, String bucketName, String keyName) { try { PutObjectRequest objectRequest = PutObjectRequest.builder() .bucket(bucketName) .key(keyName) .contentType("text/plain") .build(); PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder() .signatureDuration(Duration.ofMinutes(10)) .putObjectRequest(objectRequest) .build(); PresignedPutObjectRequest presignedRequest = presigner.presignPutObject(presignRequest); String myURL = presignedRequest.url().toString(); System.out.println("Presigned URL to upload a file to: " +myURL); System.out.println("Which HTTP method needs to be used when uploading a file: " + presignedRequest.httpRequest().method()); // Upload content to the Amazon S3 bucket by using this URL. URL url = presignedRequest.url(); // Create the connection and use it to upload the new object by using the presigned URL. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type","text/plain"); connection.setRequestMethod("PUT"); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write("This text was uploaded as an object by using a presigned URL."); out.close(); connection.getResponseCode(); System.out.println("HTTP response code is " + connection.getResponseCode()); } catch (S3Exception | IOException e) { e.getStackTrace(); } }
  • Find instructions and more code on GitHub.

The following code example shows how to:

  • Create a bucket.

  • Upload a file to the bucket.

  • Download an object from a bucket.

  • Copy an object to a subfolder in a bucket.

  • List the objects in a bucket.

  • Delete the objects in a bucket.

  • Delete a bucket.

SDK for Java 2.x

/** * 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 S3Scenario { public static void main(String[] args) throws IOException { final String usage = "\n" + "Usage:\n" + " <bucketName> <key> <objectPath> <savePath> <toBucket>\n\n" + "Where:\n" + " bucketName - The Amazon S3 bucket to create.\n\n" + " key - The key to use.\n\n" + " objectPath - The path where the file is located (for example, C:/AWS/book2.pdf). "+ " savePath - The path where the file is saved after it's downloaded (for example, C:/AWS/book2.pdf). " + " toBucket - An Amazon S3 bucket to where an object is copied to (for example, C:/AWS/book2.pdf). "; if (args.length != 5) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String key = args[1]; String objectPath = args[2]; String savePath = args[3]; String toBucket = args[4] ; ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create(); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .credentialsProvider(credentialsProvider) .build(); // Create an Amazon S3 bucket. createBucket(s3, bucketName); // Update a local file to the Amazon S3 bucket. uploadLocalFile(s3, bucketName, key, objectPath); // Download the object to another local file. getObjectBytes (s3, bucketName, key, savePath); // Perform a multipart upload. String multipartKey = "multiPartKey"; multipartUpload(s3, toBucket, multipartKey); // List all objects located in the Amazon S3 bucket. // Show 2 ways listAllObjects(s3, bucketName); anotherListExample(s3, bucketName) ; // Copy the object to another Amazon S3 bucket copyBucketObject (s3, bucketName, key, toBucket); // Delete the object from the Amazon S3 bucket. deleteObjectFromBucket(s3, bucketName, key); // Delete the Amazon S3 bucket deleteBucket(s3, bucketName); System.out.println("All Amazon S3 operations were successfully performed"); s3.close(); } // Create a bucket by using a S3Waiter object public static void createBucket( S3Client s3Client, String bucketName) { try { S3Waiter s3Waiter = s3Client.waiter(); CreateBucketRequest bucketRequest = CreateBucketRequest.builder() .bucket(bucketName) .build(); s3Client.createBucket(bucketRequest); HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder() .bucket(bucketName) .build(); // Wait until the bucket is created and print out the response. WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println(bucketName +" is ready"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void deleteBucket(S3Client client, String bucket) { DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() .bucket(bucket) .build(); client.deleteBucket(deleteBucketRequest); System.out.println(bucket +" was deleted."); } /** * Upload an object in parts */ private static void multipartUpload(S3Client s3, String bucketName, String key) { int mB = 1024 * 1024; // First create a multipart upload and get the upload id CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() .bucket(bucketName) .key(key) .build(); CreateMultipartUploadResponse response = s3.createMultipartUpload(createMultipartUploadRequest); String uploadId = response.uploadId(); System.out.println(uploadId); // Upload all the different parts of the object UploadPartRequest uploadPartRequest1 = UploadPartRequest.builder() .bucket(bucketName) .key(key) .uploadId(uploadId) .partNumber(1).build(); String etag1 = s3.uploadPart(uploadPartRequest1, RequestBody.fromByteBuffer(getRandomByteBuffer(5 * mB))).eTag(); CompletedPart part1 = CompletedPart.builder().partNumber(1).eTag(etag1).build(); UploadPartRequest uploadPartRequest2 = UploadPartRequest.builder().bucket(bucketName).key(key) .uploadId(uploadId) .partNumber(2).build(); String etag2 = s3.uploadPart(uploadPartRequest2, RequestBody.fromByteBuffer(getRandomByteBuffer(3 * mB))).eTag(); CompletedPart part2 = CompletedPart.builder().partNumber(2).eTag(etag2).build(); // Finally call completeMultipartUpload operation to tell S3 to merge all uploaded // parts and finish the multipart operation. CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() .parts(part1, part2) .build(); CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder() .bucket(bucketName) .key(key) .uploadId(uploadId) .multipartUpload(completedMultipartUpload) .build(); s3.completeMultipartUpload(completeMultipartUploadRequest); } private static ByteBuffer getRandomByteBuffer(int size) { byte[] b = new byte[size]; new Random().nextBytes(b); return ByteBuffer.wrap(b); } // Return a byte array private static byte[] getObjectFile(String filePath) { FileInputStream fileInputStream = null; byte[] bytesArray = null; try { File file = new File(filePath); bytesArray = new byte[(int) file.length()]; fileInputStream = new FileInputStream(file); fileInputStream.read(bytesArray); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return bytesArray; } 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.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"); os.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void uploadLocalFile(S3Client s3, String bucketName, String key, String objectPath) { PutObjectRequest objectRequest = PutObjectRequest.builder() .bucket(bucketName) .key(key) .build(); s3.putObject(objectRequest, RequestBody.fromBytes(getObjectFile(objectPath))); } public static void listAllObjects(S3Client s3, String bucketName) { ListObjectsV2Request listObjectsReqManual = ListObjectsV2Request.builder() .bucket(bucketName) .maxKeys(1) .build(); boolean done = false; while (!done) { ListObjectsV2Response listObjResponse = s3.listObjectsV2(listObjectsReqManual); for (S3Object content : listObjResponse.contents()) { System.out.println(content.key()); } if (listObjResponse.nextContinuationToken() == null) { done = true; } listObjectsReqManual = listObjectsReqManual.toBuilder() .continuationToken(listObjResponse.nextContinuationToken()) .build(); } } public static void anotherListExample(S3Client s3, String bucketName) { ListObjectsV2Request listReq = ListObjectsV2Request.builder() .bucket(bucketName) .maxKeys(1) .build(); ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq); // Process response pages listRes.stream() .flatMap(r -> r.contents().stream()) .forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size())); // Helper method to work with paginated collection of items directly listRes.contents().stream() .forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size())); for (S3Object content : listRes.contents()) { System.out.println(" Key: " + content.key() + " size = " + content.size()); } } public static void deleteObjectFromBucket(S3Client s3, String bucketName, String key) { DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder() .bucket(bucketName) .key(key) .build(); s3.deleteObject(deleteObjectRequest); System.out.println(key +" was deleted"); } public static String copyBucketObject (S3Client s3, String fromBucket, String objectKey, String toBucket) { String encodedUrl = null; try { encodedUrl = URLEncoder.encode(fromBucket + "/" + objectKey, StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException e) { System.out.println("URL could not be encoded: " + e.getMessage()); } CopyObjectRequest copyReq = CopyObjectRequest.builder() .copySource(encodedUrl) .destinationBucket(toBucket) .destinationKey(objectKey) .build(); try { CopyObjectResponse copyRes = s3.copyObject(copyReq); System.out.println("The "+ objectKey +" was copied to "+toBucket); return copyRes.copyObjectResult().toString(); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }