public void execute() throws MojoExecutionException, MojoFailureException {
// Setup AWS S3 client
AWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
AmazonS3Client uploadClient = new AmazonS3Client(credentials);
TransferManager transfers = new TransferManager(credentials);
// Make sure key prefix does not start with a slash but has one at the
// end
if (keyPrefix.startsWith("/"))
keyPrefix = keyPrefix.substring(1);
if (!keyPrefix.endsWith("/"))
keyPrefix = keyPrefix + "/";
// Keep track of how much data has been transferred
long totalBytesTransferred = 0L;
int items = 0;
Queue<Upload> uploads = new LinkedBlockingQueue<Upload>();
try {
// Check if S3 bucket exists
getLog().debug("Checking whether bucket " + bucket + " exists");
if (!uploadClient.doesBucketExist(bucket)) {
getLog().error("Desired bucket '" + bucket + "' does not exist!");
return;
}
getLog().debug("Collecting files to transfer from " + resources.getDirectory());
List<File> res = getResources();
for (File file : res) {
// Make path of resource relative to resources directory
String filename = file.getName();
String extension = FilenameUtils.getExtension(filename);
String path = file.getPath().substring(resources.getDirectory().length());
String key = concat("/", keyPrefix, path).substring(1);
// Delete old file version in bucket
getLog().debug("Removing existing object at " + key);
uploadClient.deleteObject(bucket, key);
// Setup meta data
ObjectMetadata meta = new ObjectMetadata();
meta.setCacheControl("public, max-age=" + String.valueOf(valid * 3600));
FileInputStream fis = null;
GZIPOutputStream gzipos = null;
final File fileToUpload;
if (gzip && ("js".equals(extension) || "css".equals(extension))) {
try {
fis = new FileInputStream(file);
File gzFile = File.createTempFile(file.getName(), null);
gzipos = new GZIPOutputStream(new FileOutputStream(gzFile));
IOUtils.copy(fis, gzipos);
fileToUpload = gzFile;
meta.setContentEncoding("gzip");
if ("js".equals(extension))
meta.setContentType("text/javascript");
if ("css".equals(extension))
meta.setContentType("text/css");
} catch (FileNotFoundException e) {
getLog().error(e);
continue;
} catch (IOException e) {
getLog().error(e);
continue;
} finally {
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(gzipos);
}
} else {
fileToUpload = file;
}
// Do a random check for existing errors before starting the next upload
if (erroneousUpload != null)
break;
// Create put object request
long bytesToTransfer = fileToUpload.length();
totalBytesTransferred += bytesToTransfer;
PutObjectRequest request = new PutObjectRequest(bucket, key, fileToUpload);
request.setProgressListener(new UploadListener(credentials, bucket, key, bytesToTransfer));
request.setMetadata(meta);
// Schedule put object request
getLog().info("Uploading " + key + " (" + FileUtils.byteCountToDisplaySize((int) bytesToTransfer) + ")");
Upload upload = transfers.upload(request);
uploads.add(upload);
items ++;
}
} catch (AmazonServiceException e) {
getLog().error("Uploading resources failed: " + e.getMessage());