Search in sources :

Example 46 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class DeleteObjects method deleteBucketObjects.

// snippet-start:[s3.java2.delete_objects.main]
public static void deleteBucketObjects(S3Client s3, String bucketName, String objectName) {
    ArrayList<ObjectIdentifier> toDelete = new ArrayList<ObjectIdentifier>();
    toDelete.add(ObjectIdentifier.builder().key(objectName).build());
    try {
        DeleteObjectsRequest dor = DeleteObjectsRequest.builder().bucket(bucketName).delete(Delete.builder().objects(toDelete).build()).build();
        s3.deleteObjects(dor);
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) ObjectIdentifier(software.amazon.awssdk.services.s3.model.ObjectIdentifier) DeleteObjectsRequest(software.amazon.awssdk.services.s3.model.DeleteObjectsRequest)

Example 47 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class GetObjectUrl method getURL.

// snippet-start:[s3.java2.getobjecturl.main]
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.toString());
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetUrlRequest(software.amazon.awssdk.services.s3.model.GetUrlRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) URL(java.net.URL)

Example 48 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class LifecycleConfiguration method setLifecycleConfig.

// snippet-start:[s3.java2.manage_lifecycle.main]
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);
    }
}
Also used : BucketLifecycleConfiguration(software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration) LifecycleRuleFilter(software.amazon.awssdk.services.s3.model.LifecycleRuleFilter) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) Transition(software.amazon.awssdk.services.s3.model.Transition) ArrayList(java.util.ArrayList) LifecycleRule(software.amazon.awssdk.services.s3.model.LifecycleRule) PutBucketLifecycleConfigurationRequest(software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest)

Example 49 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class LifecycleConfiguration method getLifecycleConfig.

// 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);
    }
}
Also used : GetBucketLifecycleConfigurationRequest(software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationRequest) BucketLifecycleConfiguration(software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration) LifecycleRuleFilter(software.amazon.awssdk.services.s3.model.LifecycleRuleFilter) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Transition(software.amazon.awssdk.services.s3.model.Transition) GetBucketLifecycleConfigurationResponse(software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationResponse) LifecycleRule(software.amazon.awssdk.services.s3.model.LifecycleRule) PutBucketLifecycleConfigurationRequest(software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest)

Example 50 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class LifecycleConfiguration method deleteLifecycleConfig.

// 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);
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) DeleteBucketLifecycleRequest(software.amazon.awssdk.services.s3.model.DeleteBucketLifecycleRequest)

Aggregations

S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)56 ArrayList (java.util.ArrayList)13 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)12 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)11 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)9 IOException (java.io.IOException)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)6 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)5 OutputStream (java.io.OutputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)4 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)4 Tag (software.amazon.awssdk.services.s3.model.Tag)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 HashMap (java.util.HashMap)3 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)3 HeadBucketRequest (software.amazon.awssdk.services.s3.model.HeadBucketRequest)3