use of com.amazonaws.AmazonServiceException in project aws-doc-sdk-examples by awsdocs.
the class XferMgrProgress method uploadFileWithListener.
public static void uploadFileWithListener(String file_path, String bucket_name, String key_prefix, boolean pause) {
System.out.println("file: " + file_path + (pause ? " (pause)" : ""));
String key_name = null;
if (key_prefix != null) {
key_name = key_prefix + '/' + file_path;
} else {
key_name = file_path;
}
File f = new File(file_path);
TransferManager xfer_mgr = new TransferManager();
try {
Upload u = xfer_mgr.upload(bucket_name, key_name, f);
// print an empty progress bar...
printProgressBar(0.0);
u.addProgressListener(new ProgressListener() {
public void progressChanged(ProgressEvent e) {
double pct = e.getBytesTransferred() * 100.0 / e.getBytes();
eraseProgressBar();
printProgressBar(pct);
}
});
// block with Transfer.waitForCompletion()
XferMgrProgress.waitForCompletion(u);
// print the final state of the transfer.
TransferState xfer_state = u.getState();
System.out.println(": " + xfer_state);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
xfer_mgr.shutdownNow();
}
use of com.amazonaws.AmazonServiceException in project aws-doc-sdk-examples by awsdocs.
the class GetBucketPolicy method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " GetBucketPolicy <bucket>\n\n" + "Where:\n" + " bucket - the bucket to get the policy from.\n\n" + "Example:\n" + " GetBucketPolicy testbucket\n\n";
if (args.length < 1) {
System.out.println(USAGE);
System.exit(1);
}
String bucket_name = args[0];
String policy_text = null;
System.out.format("Getting policy for bucket: \"%s\"\n\n", bucket_name);
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
try {
BucketPolicy bucket_policy = s3.getBucketPolicy(bucket_name);
policy_text = bucket_policy.getPolicyText();
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
if (policy_text == null) {
System.out.println("The specified bucket has no bucket policy.");
} else {
System.out.println("Returned policy:");
System.out.println("----");
System.out.println(policy_text);
System.out.println("----\n");
}
System.out.println("Done!");
}
use of com.amazonaws.AmazonServiceException in project aws-doc-sdk-examples by awsdocs.
the class GetObject method main.
public static void main(String[] args) {
final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and object to\n" + "download from it.\n" + "\n" + "Ex: GetObject <bucketname> <filename>\n";
if (args.length < 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucket_name = args[0];
String key_name = args[1];
System.out.format("Downloading %s from S3 bucket %s...\n", key_name, bucket_name);
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
try {
S3Object o = s3.getObject(bucket_name, key_name);
S3ObjectInputStream s3is = o.getObjectContent();
FileOutputStream fos = new FileOutputStream(new File(key_name));
byte[] read_buf = new byte[1024];
int read_len = 0;
while ((read_len = s3is.read(read_buf)) > 0) {
fos.write(read_buf, 0, read_len);
}
s3is.close();
fos.close();
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
System.out.println("Done!");
}
use of com.amazonaws.AmazonServiceException in project aws-doc-sdk-examples by awsdocs.
the class GetWebsiteConfiguration method getWebsiteConfig.
public static void getWebsiteConfig(String bucket_name) {
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
try {
BucketWebsiteConfiguration config = s3.getBucketWebsiteConfiguration(bucket_name);
if (config == null) {
System.out.println("No website configuration found!");
} else {
System.out.format("Index document: %s\n", config.getIndexDocumentSuffix());
System.out.format("Error document: %s\n", config.getErrorDocument());
}
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.out.println("Failed to get website configuration!");
System.exit(1);
}
}
use of com.amazonaws.AmazonServiceException in project aws-doc-sdk-examples by awsdocs.
the class CopyObject method main.
public static void main(String[] args) {
final String USAGE = "\n" + "To run this example, supply the name (key) of an S3 object, the bucket name\n" + "that it's contained within, and the bucket to copy it to.\n" + "\n" + "Ex: CopyObject <objectname> <frombucket> <tobucket>\n";
if (args.length < 3) {
System.out.println(USAGE);
System.exit(1);
}
String object_key = args[0];
String from_bucket = args[1];
String to_bucket = args[2];
System.out.format("Copying object %s from bucket %s to %s\n", object_key, from_bucket, to_bucket);
final AmazonS3 s3 = new AmazonS3Client();
try {
s3.copyObject(from_bucket, object_key, to_bucket, object_key);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
Aggregations