result = getS3Client().putObject( bucketName, uploadFileName, awsInputStream, om ); String md5sum = Hex.encodeHexString( Base64.decodeBase64(result.getContentMd5()) ); String eTag = result.getETag();
protected void doUpload(S3BlobStore blobStore, String bucketName, String blobName, InputStream is, int length, boolean serverSideEncryption) throws AmazonS3Exception { ObjectMetadata md = new ObjectMetadata(); if (serverSideEncryption) { md.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); } md.setContentLength(length); InputStream inputStream = is; // We try to compute a MD5 while reading it MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("MD5"); inputStream = new DigestInputStream(is, messageDigest); } catch (NoSuchAlgorithmException impossible) { // Every implementation of the Java platform is required to support MD5 (see MessageDigest) throw new RuntimeException(impossible); } PutObjectResult putObjectResult = blobStore.client().putObject(bucketName, blobName, inputStream, md); String localMd5 = Base64.encodeAsString(messageDigest.digest()); String remoteMd5 = putObjectResult.getContentMd5(); if (!localMd5.equals(remoteMd5)) { logger.debug("MD5 local [{}], remote [{}] are not equal...", localMd5, remoteMd5); throw new AmazonS3Exception("MD5 local [" + localMd5 + "], remote [" + remoteMd5 + "] are not equal..."); } }
public String putFile(File file, String targetFilePath, boolean calcMD5) throws NoSuchAlgorithmException, IOException, DecoderException { InputStream is = new FileInputStream(file); String md5Received = "MD5 not received"; try { S3PutRequestBuilder putRequest = s3ClientHelper.newPutRequest(bucketName, targetFilePath, is).length(file.length()) .useBucketAcl(); String localMd5 = null; if (calcMD5) { localMd5 = FileUtils.calculateMD5(file); putRequest.withMD5(localMd5); } PutObjectResult putResult = s3Client.putObject(putRequest); md5Received = (putResult == null ? null : putResult.getContentMd5()); LOGGER.debug("S3Client put request returned MD5: " + md5Received); if (calcMD5) { // Also upload the hex encoded (ie normal) md5 digest in a file String md5TargetPath = targetFilePath + ".md5"; File md5File = FileUtils.createMD5File(file, localMd5); InputStream isMD5 = new FileInputStream(md5File); S3PutRequestBuilder md5PutRequest = s3ClientHelper.newPutRequest(bucketName, md5TargetPath, isMD5).length(md5File.length()) .useBucketAcl(); s3Client.putObject(md5PutRequest); } } finally { IOUtils.closeQuietly(is); } return md5Received; }
public static PutObjectResult copyOf(com.amazonaws.services.s3.model.PutObjectResult mutable) { Builder builder = builder(); builder.withEtag(mutable.getETag()); builder.withMd5(fromTrimToNull(mutable.getContentMd5())); builder.withVersionId(fromTrimToNull(mutable.getVersionId())); builder.withExpirationTime(fromNullableDate(mutable.getExpirationTime())); builder.withExpirationTimeRuleId(fromTrimToNull(mutable.getExpirationTimeRuleId())); builder.withSseResult(fromNullableSSE(mutable)); return builder.build(); }