@Override public void setBucketLifecycleConfiguration( SetBucketLifecycleConfigurationRequest setBucketLifecycleConfigurationRequest) { setBucketLifecycleConfigurationRequest = beforeClientExecution(setBucketLifecycleConfigurationRequest); rejectNull(setBucketLifecycleConfigurationRequest, "The set bucket lifecycle configuration request object must be specified."); String bucketName = setBucketLifecycleConfigurationRequest.getBucketName(); BucketLifecycleConfiguration bucketLifecycleConfiguration = setBucketLifecycleConfigurationRequest.getLifecycleConfiguration(); rejectNull(bucketName, "The bucket name parameter must be specified when setting bucket lifecycle configuration."); rejectNull(bucketLifecycleConfiguration, "The lifecycle configuration parameter must be specified when setting bucket lifecycle configuration."); Request<SetBucketLifecycleConfigurationRequest> request = createRequest(bucketName, null, setBucketLifecycleConfigurationRequest, HttpMethodName.PUT); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "PutBucketLifecycleConfiguration"); request.addParameter("lifecycle", null); byte[] content = new BucketConfigurationXmlFactory().convertToXmlByteArray(bucketLifecycleConfiguration); request.addHeader("Content-Length", String.valueOf(content.length)); request.addHeader("Content-Type", "application/xml"); request.setContent(new ByteArrayInputStream(content)); try { byte[] md5 = Md5Utils.computeMD5Hash(content); String md5Base64 = BinaryUtils.toBase64(md5); request.addHeader("Content-MD5", md5Base64); } catch ( Exception e ) { throw new SdkClientException("Couldn't compute md5 sum", e); } invoke(request, voidResponseHandler, bucketName, null); }
if (metadata == null) metadata = new ObjectMetadata(); rejectNull(bucketName, "The bucket name parameter must be specified when uploading an object"); String contentMd5_b64 = Md5Utils.md5AsBase64(file); metadata.setContentMD5(contentMd5_b64); } catch (Exception e) { throw new SdkClientException( "Unable to calculate MD5 hash: " + e.getMessage(), e); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "PutObject"); AmazonWebServiceRequest awsreq = request.getOriginalRequest(); request.addHeader(Headers.S3_CANNED_ACL, putObjectRequest.getCannedAcl().toString()); input = new ByteArrayInputStream(new byte[0]); byte[] clientSideHash = BinaryUtils.fromBase64(contentMd5); byte[] serverSideHash = BinaryUtils.fromHex(etag); throw new SdkClientException( "Unable to verify integrity of data upload. " + "Client calculated content hash (contentMD5: "
private void setContent(Request<?> request, byte[] content, String contentType, boolean setMd5) { request.setContent(new ByteArrayInputStream(content)); request.addHeader("Content-Length", Integer.toString(content.length)); request.addHeader("Content-Type", contentType); if (setMd5) { try { byte[] md5 = Md5Utils.computeMD5Hash(content); String md5Base64 = BinaryUtils.toBase64(md5); request.addHeader("Content-MD5", md5Base64); } catch ( Exception e ) { throw new AmazonClientException("Couldn't compute md5 sum", e); } } }
final String contentMd5_b64 = Md5Utils.md5AsBase64(file); metadata.setContentMD5(contentMd5_b64); } catch (final Exception e) { final Request<PutObjectRequest> request = createRequest(bucketName, key, putObjectRequest, HttpMethodName.PUT); addAclHeaders(request, putObjectRequest.getAccessControlList()); } else if (putObjectRequest.getCannedAcl() != null) { request.addHeader(Headers.S3_CANNED_ACL, putObjectRequest.getCannedAcl().toString()); request.addHeader(Headers.STORAGE_CLASS, putObjectRequest.getStorageClass()); request.addHeader(Headers.REDIRECT_LOCATION, putObjectRequest.getRedirectLocation()); if (input == null) { setZeroContentLength(request); input = new ByteArrayInputStream(new byte[0]); "out of memory errors."); final ByteArrayInputStream bais = toByteArray(input); request.addHeader(Headers.CONTENT_LENGTH, String.valueOf(bais.available())); request.setStreaming(true); input = bais; contentMd5 = BinaryUtils.toBase64(md5DigestStream.getMd5Digest()); final byte[] clientSideHash = BinaryUtils.fromBase64(contentMd5); final byte[] serverSideHash = BinaryUtils.fromHex(returnedMetadata.getETag());
"For an upload InputStream with no MD5 digest metadata, the " + "markSupported() method must evaluate to true. "); String contentMd5 = Md5Utils.md5AsBase64(inputStream); metadata.setContentMD5(contentMd5); inputStream.reset(); String contentMd5 = Md5Utils.md5AsBase64(fileToUpload); metadata.setContentMD5(contentMd5); InputStream inputStream = new ByteArrayInputStream(payloadBytes); if (metadata.getContentMD5() == null) { String contentMd5 = Md5Utils.md5AsBase64(inputStream); metadata.setContentMD5(contentMd5); inputStream.reset();
@Test public void testStream() throws IOException { byte[] md5 = Md5Utils.computeMD5Hash(new ByteArrayInputStream("Testing MD5" .getBytes(StringUtils.UTF8))); assertEquals("0b4f503b8eb7714ce12402406895cf68", StringUtils.lowerCase(Base16.encodeAsString(md5))); String b64 = Md5Utils.md5AsBase64(new ByteArrayInputStream("Testing MD5" .getBytes(StringUtils.UTF8))); assertEquals("C09QO463cUzhJAJAaJXPaA==", b64); }
byte[] data = "0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=" .getBytes(StringUtils.UTF8); byte[] md5Expected = Md5Utils.computeMD5Hash(data); byte[] baExpected; new ByteArrayInputStream(data)); baExpected = IOUtils.toByteArray(is); byte[] md5 = is.getMd5Digest(); new ByteArrayInputStream(data)); is.mark(-1); baExpected = IOUtils.toByteArray(is);
try { byte[] data = "f".getBytes(StringUtils.UTF8); byte[] md5 = Md5Utils.computeMD5Hash(data); file1.getObjectMetadata().setHeader(Headers.ETAG, BinaryUtils.toHex(md5)); S3ObjectInputStream content = new S3ObjectInputStream(new ByteArrayInputStream(data), mock(HttpRequestBase.class)); file1.setObjectContent(content); try { byte[] data = "bb".getBytes(StringUtils.UTF8); byte[] md5 = Md5Utils.computeMD5Hash(data); file2.getObjectMetadata().setHeader(Headers.ETAG, BinaryUtils.toHex(md5)); S3ObjectInputStream content = new S3ObjectInputStream(new ByteArrayInputStream(data), mock(HttpRequestBase.class)); file2.setObjectContent(content);
throw new SdkClientException( "Unable to store object contents to disk: " + e.getMessage(), e); } finally { final ObjectMetadata metadata = s3Object.getObjectMetadata(); if (!skipMd5CheckStrategy.skipClientSideValidationPerGetResponse(metadata)) { clientSideHash = Md5Utils.computeMD5Hash(new FileInputStream(dstfile)); serverSideHash = BinaryUtils.fromHex(metadata.getETag()); throw new SdkClientException("Unable to verify integrity of data download. " + "Client calculated content hash didn't match hash calculated by Amazon S3. " + "The data stored in '" + dstfile.getAbsolutePath() + "' may be corrupt.");
private static void populateSourceSSE_C(Request<?> request, SSECustomerKey sseKey) { if (sseKey == null) return; // Populate the SSE-C parameters for the source object addHeaderIfNotNull(request, Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM, sseKey.getAlgorithm()); addHeaderIfNotNull(request, Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY, sseKey.getKey()); addHeaderIfNotNull(request, Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, sseKey.getMd5()); // Calculate the MD5 hash of the encryption key and fill it in the // header, if the user didn't specify it in the metadata if (sseKey.getKey() != null && sseKey.getMd5() == null) { String encryptionKey_b64 = sseKey.getKey(); byte[] encryptionKey = Base64.decode(encryptionKey_b64); request.addHeader(Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, Md5Utils.md5AsBase64(encryptionKey)); } }
/** * Computes the MD5 of the given file. */ public static byte[] computeMD5Hash(File file) throws FileNotFoundException, IOException { return computeMD5Hash(new FileInputStream(file)); }
/** * Returns the hex-encoded MD5 hash String of the given message body. */ private static String calculateMessageBodyMd5(String messageBody) { if (log.isDebugEnabled()) { log.debug("Message body: " + messageBody); } byte[] expectedMd5; try { expectedMd5 = Md5Utils.computeMD5Hash(messageBody.getBytes(UTF8)); } catch (Exception e) { throw new AmazonClientException("Unable to calculate the MD5 hash of the message body. " + e.getMessage(), e); } String expectedMd5Hex = BinaryUtils.toHex(expectedMd5); if (log.isDebugEnabled()) { log.debug("Expected MD5 of message body: " + expectedMd5Hex); } return expectedMd5Hex; }
@Test public void testFile() throws Exception { File f = File.createTempFile("Md5UtilsTest-", "txt"); f.deleteOnExit(); FileUtils.writeStringToFile(f, "Testing MD5"); byte[] md5 = Md5Utils.computeMD5Hash(f); assertEquals("0b4f503b8eb7714ce12402406895cf68", StringUtils.lowerCase(Base16.encodeAsString(md5))); String b64 = Md5Utils.md5AsBase64(f); assertEquals("C09QO463cUzhJAJAaJXPaA==", b64); } }
clientSideHash = Md5Utils.computeMD5Hash(new FileInputStream(destinationFile)); serverSideHash = BinaryUtils.fromHex(s3Object.getObjectMetadata().getETag());
@Override public void provideObjectMetadata(File file, ObjectMetadata metadata) { if (S3MessageHandler.this.uploadMetadataProvider != null) { S3MessageHandler.this.uploadMetadataProvider.populateMetadata(metadata, MessageBuilder.createMessage(file, this.messageHeaders)); } if (metadata.getContentMD5() == null) { try { String contentMd5 = Md5Utils.md5AsBase64(file); metadata.setContentMD5(contentMd5); } catch (Exception e) { throw new AmazonClientException(e); } } }
private void setContent(Request<?> request, byte[] content, String contentType, boolean setMd5) { request.setContent(new ByteArrayInputStream(content)); request.addHeader("Content-Length", Integer.toString(content.length)); request.addHeader("Content-Type", contentType); if (setMd5) { try { final byte[] md5 = Md5Utils.computeMD5Hash(content); final String md5Base64 = BinaryUtils.toBase64(md5); request.addHeader("Content-MD5", md5Base64); } catch ( final Exception e ) { throw new AmazonClientException("Couldn't compute md5 sum", e); } } }
final String contentMd5_b64 = Md5Utils.md5AsBase64(file); metadata.setContentMD5(contentMd5_b64); } catch (final Exception e) { final Request<PutObjectRequest> request = createRequest(bucketName, key, putObjectRequest, HttpMethodName.PUT); addAclHeaders(request, putObjectRequest.getAccessControlList()); } else if (putObjectRequest.getCannedAcl() != null) { request.addHeader(Headers.S3_CANNED_ACL, putObjectRequest.getCannedAcl().toString()); request.addHeader(Headers.STORAGE_CLASS, putObjectRequest.getStorageClass()); request.addHeader(Headers.REDIRECT_LOCATION, putObjectRequest.getRedirectLocation()); if (input == null) { setZeroContentLength(request); input = new ByteArrayInputStream(new byte[0]); "out of memory errors."); final ByteArrayInputStream bais = toByteArray(input); request.addHeader(Headers.CONTENT_LENGTH, String.valueOf(bais.available())); request.setStreaming(true); input = bais; contentMd5 = BinaryUtils.toBase64(md5DigestStream.getMd5Digest()); final byte[] clientSideHash = BinaryUtils.fromBase64(contentMd5); final byte[] serverSideHash = BinaryUtils.fromHex(returnedMetadata.getETag());
"For an upload InputStream with no MD5 digest metadata, " + "the markSupported() method must evaluate to true."); String contentMd5 = Md5Utils.md5AsBase64(inputStream); metadata.setContentMD5(contentMd5); inputStream.reset(); String contentMd5 = Md5Utils.md5AsBase64(fileToUpload); metadata.setContentMD5(contentMd5); InputStream inputStream = new ByteArrayInputStream(payloadBytes); if (metadata.getContentMD5() == null) { String contentMd5 = Md5Utils.md5AsBase64(inputStream); metadata.setContentMD5(contentMd5); inputStream.reset();
throw new SdkClientException( "Unable to store object contents to disk: " + e.getMessage(), e); } finally { final ObjectMetadata metadata = s3Object.getObjectMetadata(); if (!skipMd5CheckStrategy.skipClientSideValidationPerGetResponse(metadata)) { clientSideHash = Md5Utils.computeMD5Hash(new FileInputStream(dstfile)); serverSideHash = BinaryUtils.fromHex(metadata.getETag()); throw new SdkClientException("Unable to verify integrity of data download. " + "Client calculated content hash didn't match hash calculated by Amazon S3. " + "The data stored in '" + dstfile.getAbsolutePath() + "' may be corrupt.");
if (sseKey == null) return; addHeaderIfNotNull(request, Headers.SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM, sseKey.getAlgorithm()); addHeaderIfNotNull(request, Headers.SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY, sseKey.getKey()); addHeaderIfNotNull(request, Headers.SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, sseKey.getMd5()); String encryptionKey_b64 = sseKey.getKey(); byte[] encryptionKey = Base64.decode(encryptionKey_b64); request.addHeader(Headers.SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, Md5Utils.md5AsBase64(encryptionKey));