/** * <p> * Populates the specified request with the numerous attributes available in * <code>SSEWithCustomerKeyRequest</code>. * </p> * * @param request * The request to populate with headers to represent all the * options expressed in the * <code>ServerSideEncryptionWithCustomerKeyRequest</code> * object. * @param sseKey * The request object for an S3 operation that allows server-side * encryption using customer-provided keys. */ private static void populateSSE_C(Request<?> request, SSECustomerKey sseKey) { 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()); // 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.SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, Md5Utils.md5AsBase64(encryptionKey)); } }
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)); } }
@Test public void correctMD5Test() throws Exception { String payload = "Hello, World"; String messageBody = messageMarshaller.serialize(MessageBuilder .withPayload(payload).build()); com.amazonaws.services.sqs.model.Message sqsMessage = new com.amazonaws.services.sqs.model.Message(); sqsMessage.setBody(messageBody); sqsMessage.setMD5OfBody(new String(Hex.encodeHex(Md5Utils .computeMD5Hash(messageBody.getBytes("UTF-8"))))); ReceiveMessageResult result = new ReceiveMessageResult(); result.setMessages(Collections.singletonList(sqsMessage)); when(mockSQS.receiveMessage(any(ReceiveMessageRequest.class))) .thenReturn(result); Message<?> recvMessage = executor.poll(); assertNotNull("message is not null", recvMessage); Message<?> enclosed = messageMarshaller .deserialize((String) recvMessage.getPayload()); String recvPayload = (String) enclosed.getPayload(); assertEquals("payload must match", payload, recvPayload); }
@Test public void testUploadManyFiles() throws Exception { init("s3.default.properties"); for (int i = 0; i < 8; i++) { sink.append(new Record(Integer.toString(i), Integer.toString(i).getBytes())); } List<String> md5s = new ArrayList<>(); for (String file : sink.getBuffer().getFiles()) { md5s.add(new String(Md5Utils.md5AsBase64(new File(file)))); } sink.prepareCommit(); sink.commit(); ObjectListing list = s3.listObjects(s3SinkConfig.getBucketName(), "10"); assertTrue(list.getObjectSummaries().size() == 4); for (int i = 0; i < 8; i += 2) { S3Object s3object = s3.getObject(s3SinkConfig.getBucketName(), "10/" + i + "_" + (i + 1)); assertNotNull(s3object); } }
@Override public PutObjectResult putObject(String bucketName, String key, File file) throws AmazonClientException, AmazonServiceException { throwException(putObjectException); List<String> keys = files.get(bucketName); if (keys == null) { throw new AmazonClientException("Bucket do not exist"); } keys.add(key); files.put(bucketName, keys); PutObjectResult result = new PutObjectResult(); try { result.setContentMd5(new String(Md5Utils.md5AsBase64(file))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; }
private String getObjectMD5(byte[] objectData) { byte[] expectedMd5 = null; try { expectedMd5 = Md5Utils.computeMD5Hash(objectData); } catch (Exception e) {} return BinaryUtils.toHex(expectedMd5); }
private String getAwsMessageMD5(String message) { byte[] expectedMd5 = null; try { expectedMd5 = Md5Utils.computeMD5Hash(message.getBytes(UTF8)); } catch (Exception e) {} return BinaryUtils.toHex(expectedMd5); }
@Override void writeFile(final String path, final byte[] data) throws IOException { ObjectMetadata bucketMetadata = new ObjectMetadata(); bucketMetadata.setContentMD5(Md5Utils.md5AsBase64(data)); bucketMetadata.setContentLength(data.length); // Give Tika a few hints for the content detection Metadata tikaMetadata = new Metadata(); tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path))); // Fire! try (InputStream bis = TikaInputStream.get(data, tikaMetadata)) { bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString()); s3.putObject(bucket, resolveKey(path), bis, bucketMetadata); } }
@Override public void setBucketCrossOriginConfiguration( SetBucketCrossOriginConfigurationRequest setBucketCrossOriginConfigurationRequest) { setBucketCrossOriginConfigurationRequest = beforeClientExecution(setBucketCrossOriginConfigurationRequest); rejectNull(setBucketCrossOriginConfigurationRequest, "The set bucket cross origin configuration request object must be specified."); String bucketName = setBucketCrossOriginConfigurationRequest.getBucketName(); BucketCrossOriginConfiguration bucketCrossOriginConfiguration = setBucketCrossOriginConfigurationRequest.getCrossOriginConfiguration(); rejectNull(bucketName, "The bucket name parameter must be specified when setting bucket cross origin configuration."); rejectNull(bucketCrossOriginConfiguration, "The cross origin configuration parameter must be specified when setting bucket cross origin configuration."); Request<SetBucketCrossOriginConfigurationRequest> request = createRequest(bucketName, null, setBucketCrossOriginConfigurationRequest, HttpMethodName.PUT); request.addParameter("cors", null); byte[] content = new BucketConfigurationXmlFactory().convertToXmlByteArray(bucketCrossOriginConfiguration); 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); }
@Override public void setBucketTaggingConfiguration( SetBucketTaggingConfigurationRequest setBucketTaggingConfigurationRequest) { setBucketTaggingConfigurationRequest = beforeClientExecution(setBucketTaggingConfigurationRequest); rejectNull(setBucketTaggingConfigurationRequest, "The set bucket tagging configuration request object must be specified."); String bucketName = setBucketTaggingConfigurationRequest.getBucketName(); BucketTaggingConfiguration bucketTaggingConfiguration = setBucketTaggingConfigurationRequest.getTaggingConfiguration(); rejectNull(bucketName, "The bucket name parameter must be specified when setting bucket tagging configuration."); rejectNull(bucketTaggingConfiguration, "The tagging configuration parameter must be specified when setting bucket tagging configuration."); Request<SetBucketTaggingConfigurationRequest> request = createRequest(bucketName, null, setBucketTaggingConfigurationRequest, HttpMethodName.PUT); request.addParameter("tagging", null); byte[] content = new BucketConfigurationXmlFactory().convertToXmlByteArray(bucketTaggingConfiguration); 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); }
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); } } }
@Test @Override public void test() throws Exception { AmazonS3 amazonS3Client = TestUtils.getPropertyValue(this.s3MessageHandler, "transferManager.s3", AmazonS3.class); InputStream payload = new StringInputStream("a"); Message<?> message = MessageBuilder.withPayload(payload) .setHeader("key", "myInputStream") .build(); this.channels.input().send(message); ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); verify(amazonS3Client, atLeastOnce()).putObject(putObjectRequestArgumentCaptor.capture()); PutObjectRequest putObjectRequest = putObjectRequestArgumentCaptor.getValue(); assertThat(putObjectRequest.getBucketName(), equalTo(S3_BUCKET)); assertThat(putObjectRequest.getKey(), equalTo("myInputStream")); assertNull(putObjectRequest.getFile()); assertNotNull(putObjectRequest.getInputStream()); ObjectMetadata metadata = putObjectRequest.getMetadata(); assertThat(metadata.getContentMD5(), equalTo(Md5Utils.md5AsBase64(payload))); assertThat(metadata.getContentLength(), equalTo(1L)); assertThat(metadata.getContentType(), equalTo(MediaType.APPLICATION_JSON_VALUE)); assertThat(metadata.getContentDisposition(), equalTo("test.json")); }
private String etag(File file) { try { FileInputStream fileInputStream = new FileInputStream(file); byte[] md5Hash = Md5Utils.computeMD5Hash(fileInputStream); return BinaryUtils.toHex(md5Hash); } catch (IOException e) { throw new IllegalStateException(e); } }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) { deleteObjectsRequest = beforeClientExecution(deleteObjectsRequest); Request<DeleteObjectsRequest> request = createRequest(deleteObjectsRequest.getBucketName(), null, deleteObjectsRequest, HttpMethodName.POST); request.addParameter("delete", null); if ( deleteObjectsRequest.getMfa() != null ) { populateRequestWithMfaDetails(request, deleteObjectsRequest.getMfa()); } populateRequesterPaysHeader(request, deleteObjectsRequest.isRequesterPays()); byte[] content = new MultiObjectDeleteXmlFactory().convertToXmlByteArray(deleteObjectsRequest); 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); } @SuppressWarnings("unchecked") ResponseHeaderHandlerChain<DeleteObjectsResponse> responseHandler = new ResponseHeaderHandlerChain<DeleteObjectsResponse>( new Unmarshallers.DeleteObjectsResultUnmarshaller(), new S3RequesterChargedHeaderHandler<DeleteObjectsResponse>()); DeleteObjectsResponse response = invoke(request, responseHandler, deleteObjectsRequest.getBucketName(), null); /* * If the result was only partially successful, throw an exception */ if ( !response.getErrors().isEmpty() ) { Map<String, String> headers = responseHandler.getResponseHeaders(); MultiObjectDeleteException ex = new MultiObjectDeleteException( response.getErrors(), response.getDeletedObjects()); ex.setStatusCode(200); ex.setRequestId(headers.get(Headers.REQUEST_ID)); ex.setExtendedRequestId(headers.get(Headers.EXTENDED_REQUEST_ID)); ex.setCloudFrontId(headers.get(Headers.CLOUD_FRONT_ID)); throw ex; } DeleteObjectsResult result = new DeleteObjectsResult(response.getDeletedObjects(), response.isRequesterCharged()); return result; }
@Test @Override public void test() throws Exception { AmazonS3 amazonS3Client = TestUtils.getPropertyValue(this.s3MessageHandler, "transferManager.s3", AmazonS3.class); File file = this.temporaryFolder.newFile("foo.mp3"); Message<?> message = MessageBuilder.withPayload(file) .build(); this.channels.input().send(message); ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); verify(amazonS3Client, atLeastOnce()).putObject(putObjectRequestArgumentCaptor.capture()); PutObjectRequest putObjectRequest = putObjectRequestArgumentCaptor.getValue(); assertThat(putObjectRequest.getBucketName(), equalTo(S3_BUCKET)); assertThat(putObjectRequest.getKey(), equalTo("foo.mp3")); assertNotNull(putObjectRequest.getFile()); assertNull(putObjectRequest.getInputStream()); ObjectMetadata metadata = putObjectRequest.getMetadata(); assertThat(metadata.getContentMD5(), equalTo(Md5Utils.md5AsBase64(file))); assertThat(metadata.getContentLength(), equalTo(0L)); assertThat(metadata.getContentType(), equalTo("audio/mpeg")); ProgressListener listener = putObjectRequest.getGeneralProgressListener(); S3ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT); assertTrue(this.transferCompletedLatch.await(10, TimeUnit.SECONDS)); assertTrue(this.aclLatch.await(10, TimeUnit.SECONDS)); ArgumentCaptor<SetObjectAclRequest> setObjectAclRequestArgumentCaptor = ArgumentCaptor.forClass(SetObjectAclRequest.class); verify(amazonS3Client).setObjectAcl(setObjectAclRequestArgumentCaptor.capture()); SetObjectAclRequest setObjectAclRequest = setObjectAclRequestArgumentCaptor.getValue(); assertThat(setObjectAclRequest.getBucketName(), equalTo(S3_BUCKET)); assertThat(setObjectAclRequest.getKey(), equalTo("foo.mp3")); assertNull(setObjectAclRequest.getAcl()); assertThat(setObjectAclRequest.getCannedAcl(), equalTo(CannedAccessControlList.PublicReadWrite)); }
private String etag(byte[] bytes) { byte[] md5Hash = Md5Utils.computeMD5Hash(bytes); return BinaryUtils.toHex(md5Hash); }
private String getMD5(byte[] bytes) { return BinaryUtils.toHex(Md5Utils.computeMD5Hash(bytes)); }
/** * Calculates the MD5 hex code of a string (using the util class<br> * that the AWS SDK uses for MD5 calculation). * * @param message Any text * @return Upper-case MD5 checksum in HEX string format */ public String getMD5(String message) { byte[] expectedMd5 = Md5Utils.computeMD5Hash(message.getBytes(UTF8)); return BinaryUtils.toHex(expectedMd5); }