Search in sources :

Example 56 with S3Exception

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

the class PutObject method putS3Object.

// snippet-start:[s3.java2.s3_object_upload.main]
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 "";
}
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)

Example 57 with S3Exception

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

the class PutObjectRetention method setRentionPeriod.

// snippet-start:[s3.java2.retention_object.main]
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);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) PutObjectRetentionRequest(software.amazon.awssdk.services.s3.model.PutObjectRetentionRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) Instant(java.time.Instant) LocalDate(java.time.LocalDate) ObjectLockRetention(software.amazon.awssdk.services.s3.model.ObjectLockRetention)

Example 58 with S3Exception

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

the class SetWebsiteConfiguration method setWebsiteConfig.

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

Example 59 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project data-transfer-project by google.

the class BackblazeDataTransferClient method init.

public void init(String keyId, String applicationKey, String exportService) throws BackblazeCredentialsException, IOException {
    // Fetch all the available buckets and use that to find which region the user is in
    ListBucketsResponse listBucketsResponse = null;
    String userRegion = null;
    // The Key ID starts with the region identifier number, so reorder the regions such that
    // the first region is most likely the user's region
    String regionId = keyId.substring(0, 3);
    BACKBLAZE_REGIONS.sort((String region1, String region2) -> {
        if (region1.endsWith(regionId)) {
            return -1;
        }
        return 0;
    });
    Throwable s3Exception = null;
    for (String region : BACKBLAZE_REGIONS) {
        try {
            s3Client = backblazeS3ClientFactory.createS3Client(keyId, applicationKey, region);
            listBucketsResponse = s3Client.listBuckets();
            userRegion = region;
            break;
        } catch (S3Exception e) {
            s3Exception = e;
            if (s3Client != null) {
                s3Client.close();
            }
            if (e.statusCode() == 403) {
                monitor.debug(() -> String.format("User is not in region %s", region));
            }
        }
    }
    if (listBucketsResponse == null || userRegion == null) {
        throw new BackblazeCredentialsException("User's credentials or permissions are not valid for any regions available", s3Exception);
    }
    bucketName = getOrCreateBucket(s3Client, listBucketsResponse, userRegion, exportService);
}
Also used : ListBucketsResponse(software.amazon.awssdk.services.s3.model.ListBucketsResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) BackblazeCredentialsException(org.datatransferproject.datatransfer.backblaze.exception.BackblazeCredentialsException)

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