* exceptions could include IO failures, gzipping and encryption failures.
*/
public static S3Object createObjectForUpload(String objectKey, File dataFile,
EncryptionUtil encryptionUtil, boolean gzipFile, BytesProgressWatcher progressWatcher) throws Exception
{
S3Object s3Object = new S3Object(objectKey);
// Set object explicitly to private access by default.
s3Object.setAcl(AccessControlList.REST_CANNED_PRIVATE);
s3Object.addMetadata(Constants.METADATA_JETS3T_LOCAL_FILE_DATE,
ServiceUtils.formatIso8601Date(new Date(dataFile.lastModified())));
if (dataFile.isDirectory()) {
s3Object.setContentLength(0);
s3Object.setContentType(Mimetypes.MIMETYPE_JETS3T_DIRECTORY);
} else {
s3Object.setContentType(Mimetypes.getInstance().getMimetype(dataFile));
File uploadFile = transformUploadFile(dataFile, s3Object, encryptionUtil,
gzipFile, progressWatcher);
s3Object.setContentLength(uploadFile.length());
s3Object.setDataInputFile(uploadFile);
// Compute the upload file's MD5 hash.
InputStream inputStream = new BufferedInputStream(new FileInputStream(uploadFile));
if (progressWatcher != null) {
inputStream = new ProgressMonitoredInputStream(inputStream, progressWatcher);
}
s3Object.setMd5Hash(ServiceUtils.computeMD5Hash(inputStream));
if (!uploadFile.equals(dataFile)) {
// Compute the MD5 hash of the *original* file, if upload file has been altered
// through encryption or gzipping.
inputStream = new BufferedInputStream(new FileInputStream(dataFile));
if (progressWatcher != null) {
inputStream = new ProgressMonitoredInputStream(inputStream, progressWatcher);
}
s3Object.addMetadata(
S3Object.METADATA_HEADER_ORIGINAL_HASH_MD5,
ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(inputStream)));
}
}
return s3Object;