Java源码示例:com.amazonaws.util.Md5Utils
示例1
@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);
}
示例2
@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);
}
}
示例3
@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;
}
示例4
private void addDeduplicationAttributes(MessageContent messageContent, Object task) {
if (task instanceof Deduplicated) {
String deduplicationID = ((Deduplicated)task).deduplicationID();
if (deduplicationID == null) {
String body = messageContent.getMessageBody();
deduplicationID = BinaryUtils.toHex(Md5Utils.computeMD5Hash(body.getBytes(UTF8)));
}
messageContent.setMessageAttributesEntry(SQSFutureTask.DEDUPLICATION_ID_ATTRIBUTE_NAME,
stringMessageAttributeValue(deduplicationID));
}
messageContent.setMessageAttributesEntry(SQSFutureTask.UUID_ATTRIBUTE_NAME,
stringMessageAttributeValue(UUID.randomUUID().toString()));
}
示例5
@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);
}
}
示例6
@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);
}
}
示例7
@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"));
}
示例8
@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));
}