View Javadoc

1   package org.kuali.maven.wagon;
2   
3   import java.io.File;
4   
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import com.amazonaws.services.s3.AmazonS3Client;
9   import com.amazonaws.services.s3.model.AmazonS3Exception;
10  import com.amazonaws.services.s3.model.PutObjectRequest;
11  import com.amazonaws.services.s3.transfer.TransferManager;
12  import com.amazonaws.services.s3.transfer.Upload;
13  
14  public class S3Utils {
15  	private static final int KILOBYTE = 1024;
16  	private static final int MEGABYTE = 1024 * KILOBYTE;
17  	private static final int MULTI_PART_UPLOAD_THRESHOLD = 100 * MEGABYTE;
18  	private static final Logger log = LoggerFactory.getLogger(S3Utils.class);
19  
20  	public static final void upload(File file, PutObjectRequest request, AmazonS3Client client, TransferManager manager) {
21  
22  		// Store the file on S3
23  		if (file.length() > MULTI_PART_UPLOAD_THRESHOLD) {
24  			log.debug("Multi-part: " + file.getAbsolutePath());
25  			// Use multi-part upload for large files
26  			Upload upload = manager.upload(request);
27  			try {
28  				// Block and wait for the upload to finish
29  				upload.waitForCompletion();
30  			} catch (Exception e) {
31  				throw new AmazonS3Exception("Unexpected error uploading file", e);
32  			}
33  		} else {
34  			// Use normal upload for small files
35  			client.putObject(request);
36  		}
37  
38  	}
39  
40  }