use of com.amazonaws.services.s3.model.AmazonS3Exception in project aws-doc-sdk-examples by awsdocs.
the class ImportSegment method uploadToS3.
private static void uploadToS3(File endpointsFile, String s3BucketName) {
// Initializes Amazon S3 client.
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
// Checks whether the specified bucket exists. If not, attempts to create one.
if (!s3.doesBucketExistV2(s3BucketName)) {
try {
s3.createBucket(s3BucketName);
System.out.format("Created S3 bucket %s.\n", s3BucketName);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
}
// Uploads the endpoints file to the bucket.
String endpointsFileName = endpointsFile.getName();
System.out.format("Uploading %s to S3 bucket %s . . .\n", endpointsFileName, s3BucketName);
try {
s3.putObject(s3BucketName, "imports/" + endpointsFileName, endpointsFile);
System.out.println("Finished uploading to S3.");
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
}
use of com.amazonaws.services.s3.model.AmazonS3Exception in project aws-doc-sdk-examples by awsdocs.
the class S3EncryptV2 method main.
public static void main(String[] args) throws NoSuchAlgorithmException {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
// of this example
try {
s3Client.createBucket(bucketName);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
putEncryptedData1();
putEncryptedData2();
putEncryptedData3_Kms();
try {
ObjectListing objectListing = s3Client.listObjects(bucketName);
while (true) {
for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext(); ) {
S3ObjectSummary summary = (S3ObjectSummary) iterator.next();
s3Client.deleteObject(bucketName, summary.getKey());
}
if (objectListing.isTruncated()) {
objectListing = s3Client.listNextBatchOfObjects(objectListing);
} else {
break;
}
}
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
// delete test bucket
try {
s3Client.deleteBucket(bucketName);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
s3Client.shutdown();
System.out.println("Done");
}
use of com.amazonaws.services.s3.model.AmazonS3Exception in project stocator by SparkTC.
the class COSAPIClient method getFileStatusKeyBased.
private FileStatus getFileStatusKeyBased(String key, Path path) throws AmazonS3Exception {
LOG.trace("internal method - get file status by key {}, path {}", key, path);
FileStatus cachedFS = memoryCache.getFileStatus(path.toString());
if (cachedFS != null) {
return cachedFS;
}
ObjectMetadata meta = mClient.getObjectMetadata(mBucket, key);
String sparkOrigin = meta.getUserMetaDataOf("data-origin");
boolean stocatorCreated = false;
if (sparkOrigin != null) {
String tmp = (String) sparkOrigin;
if (tmp.equals("stocator")) {
stocatorCreated = true;
}
}
mCachedSparkOriginated.put(key, Boolean.valueOf(stocatorCreated));
FileStatus fs = createFileStatus(meta.getContentLength(), key, meta.getLastModified(), path);
LOG.trace("getFileStatusKeyBased: key {} fs.path {}", key, fs.getPath());
memoryCache.putFileStatus(path.toString(), fs);
return fs;
}
use of com.amazonaws.services.s3.model.AmazonS3Exception in project elasticsearch by elastic.
the class TestAmazonS3 method uploadPart.
@Override
public UploadPartResult uploadPart(UploadPartRequest request) throws AmazonClientException, AmazonServiceException {
if (shouldFail(request.getBucketName(), request.getKey(), writeFailureRate)) {
long length = request.getPartSize();
long partToRead = (long) (length * randomDouble());
byte[] buffer = new byte[1024];
for (long cur = 0; cur < partToRead; cur += buffer.length) {
try (InputStream input = request.getInputStream()) {
input.read(buffer, 0, (int) (partToRead - cur > buffer.length ? buffer.length : partToRead - cur));
} catch (IOException ex) {
throw new ElasticsearchException("cannot read input stream", ex);
}
}
logger.info("--> random write failure on uploadPart method: throwing an exception for [bucket={}, key={}]", request.getBucketName(), request.getKey());
AmazonS3Exception ex = new AmazonS3Exception("Random S3 write exception");
ex.setStatusCode(400);
ex.setErrorCode("RequestTimeout");
throw ex;
} else {
return super.uploadPart(request);
}
}
use of com.amazonaws.services.s3.model.AmazonS3Exception in project elasticsearch by elastic.
the class MockAmazonS3 method listObjects.
@Override
public ObjectListing listObjects(ListObjectsRequest listObjectsRequest) throws AmazonClientException, AmazonServiceException {
MockObjectListing list = new MockObjectListing();
list.setTruncated(false);
String blobName;
String prefix = listObjectsRequest.getPrefix();
ArrayList<S3ObjectSummary> mockObjectSummaries = new ArrayList<>();
for (Map.Entry<String, InputStream> blob : blobs.entrySet()) {
blobName = blob.getKey();
S3ObjectSummary objectSummary = new S3ObjectSummary();
if (prefix.isEmpty() || blobName.startsWith(prefix)) {
objectSummary.setKey(blobName);
try {
objectSummary.setSize(getSize(blob.getValue()));
} catch (IOException e) {
throw new AmazonS3Exception("Object listing " + "failed for blob [" + blob.getKey() + "]");
}
mockObjectSummaries.add(objectSummary);
}
}
list.setObjectSummaries(mockObjectSummaries);
return list;
}
Aggregations