Search in sources :

Example 6 with S3Exception

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

the class SetAcl method setBucketAcl.

// snippet-start:[s3.java2.set_acl.main]
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);
    }
}
Also used : Grant(software.amazon.awssdk.services.s3.model.Grant) AccessControlPolicy(software.amazon.awssdk.services.s3.model.AccessControlPolicy) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PutBucketAclRequest(software.amazon.awssdk.services.s3.model.PutBucketAclRequest) ArrayList(java.util.ArrayList)

Example 7 with S3Exception

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

the class SetBucketPolicy method setPolicy.

// snippet-start:[s3.java2.set_bucket_policy.main]
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!");
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PutBucketPolicyRequest(software.amazon.awssdk.services.s3.model.PutBucketPolicyRequest)

Example 8 with S3Exception

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

the class ManagingObjectTags method putS3ObjectTags.

// snippet-start:[s3.java2.s3_object_manage_tags.main]
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<Tag>();
        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);
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Tagging(software.amazon.awssdk.services.s3.model.Tagging) Tag(software.amazon.awssdk.services.s3.model.Tag) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

Example 9 with S3Exception

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

the class PutBucketLogging method setlogRequest.

// snippet-start:[s3.java2.s3_put_log.main]
public static void setlogRequest(S3Client s3, String bucketName, String targetBucket) {
    try {
        GetBucketAclRequest aclRequest = GetBucketAclRequest.builder().bucket(targetBucket).build();
        GetBucketAclResponse response = s3.getBucketAcl(aclRequest);
        // Get the Id
        String id = response.owner().id();
        // Create a Grantee object
        Grantee grantee = Grantee.builder().type(Type.GROUP).uri("http://acs.amazonaws.com/groups/s3/LogDelivery").build();
        TargetGrant targetGrant = TargetGrant.builder().grantee(grantee).permission(BucketLogsPermission.FULL_CONTROL).build();
        List granteeList = new ArrayList<Grantee>();
        granteeList.add(targetGrant);
        LoggingEnabled loggingEnabled = LoggingEnabled.builder().targetBucket(targetBucket).targetGrants(granteeList).build();
        BucketLoggingStatus loggingStatus = BucketLoggingStatus.builder().loggingEnabled(loggingEnabled).build();
        PutBucketLoggingRequest loggingRequest = PutBucketLoggingRequest.builder().bucket(bucketName).expectedBucketOwner("814548047983").bucketLoggingStatus(loggingStatus).build();
        s3.putBucketLogging(loggingRequest);
        System.out.println("Enabling logging for the target bucket " + targetBucket);
    } catch (S3Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TargetGrant(software.amazon.awssdk.services.s3.model.TargetGrant)

Example 10 with S3Exception

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

the class PutObjectMetadata method putS3Object.

// snippet-start:[s3.java2.s3_object_upload.metadata.main]
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 "";
}
Also used : HashMap(java.util.HashMap) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

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