grants = convertACLtoGrants(object.getAcl());
}
MetadataEntry[] metadata = convertMetadata(object.getMetadataMap());
try {
AmazonS3SoapBindingStub s3SoapBinding = getSoapBinding();
long contentLength = object.getContentLength();
String contentType = object.getContentType();
if (contentType == null) {
// Set default content type.
contentType = Mimetypes.MIMETYPE_OCTET_STREAM;
}
if (object.getDataInputStream() != null) {
log.debug("Uploading data input stream for S3Object: " + object.getKey());
if (contentLength == 0 && object.getDataInputStream().available() > 0) {
log.warn("S3Object " + object.getKey()
+ " - Content-Length was set to 0 despite having a non-empty data"
+ " input stream. The Content-length will be determined in memory.");
// Read all data into memory to determine it's length.
BufferedInputStream bis = new BufferedInputStream(object.getDataInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
try {
byte[] buffer = new byte[8192];
int read = -1;
while ((read = bis.read(buffer)) != -1) {
bos.write(buffer, 0, read);
}
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
contentLength = baos.size();
object.setDataInputStream(new ByteArrayInputStream(baos.toByteArray()));
log.debug("Content-Length value has been reset to " + contentLength);
}
DataHandler dataHandler = new DataHandler(
new SourceDataSource(
null, contentType, new StreamSource(object.getDataInputStream())));
s3SoapBinding.addAttachment(dataHandler);
} else {
DataHandler dataHandler = new DataHandler(
new SourceDataSource(
null, contentType, new StreamSource()));
s3SoapBinding.addAttachment(dataHandler);
}
Calendar timestamp = getTimeStamp( System.currentTimeMillis() );
String signature = ServiceUtils.signWithHmacSha1(getAWSSecretKey(),
Constants.SOAP_SERVICE_NAME + "PutObject" + convertDateToString(timestamp));
PutObjectResult result =
s3SoapBinding.putObject(bucketName, object.getKey(), metadata,
contentLength, grants, null, getAWSAccessKey(),
timestamp, signature, null);
// Ensure no data was corrupted, if we have the MD5 hash available to check.
String eTag = result.getETag().substring(1, result.getETag().length() - 1);