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 "";
}
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);
}
}
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);
}
}
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);
}
Aggregations