priorChunkSignature + "\n" + AbstractAWSSigner.EMPTY_STRING_SHA256_HEX + "\n" + BinaryUtils.toHex(sha256.digest(chunkData)); final String chunkSignature = BinaryUtils.toHex(aws4Signer.signWithMac(chunkStringToSign, hmacSha256)); priorChunkSignature = chunkSignature; chunkHeader.append(CHUNK_SIGNATURE_HEADER) return signedChunk; } catch (Exception e) { throw new SdkClientException("Unable to sign the chunked data. " + e.getMessage(), e);
/** * The decoded console output. * * @return The decoded console output. */ public String getDecodedOutput() { byte[] bytes = com.amazonaws.util.BinaryUtils.fromBase64(output); return new String(bytes, com.amazonaws.util.StringUtils.UTF8); }
@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); }
/** * Wraps a ByteBuffer in an InputStream. If the input {@code byteBuffer} * is null, returns an empty stream. * * @param byteBuffer The ByteBuffer to wrap. * * @return An InputStream wrapping the ByteBuffer content. */ public static ByteArrayInputStream toStream(ByteBuffer byteBuffer) { if (byteBuffer == null) { return new ByteArrayInputStream(new byte[0]); } return new ByteArrayInputStream(copyBytesFrom(byteBuffer)); }
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); } } }
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: "
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());
@Ignore @Test public void test() throws Exception { PutRecordRequest putRecordRequest = new PutRecordRequest(); putRecordRequest.setStreamName("stream name"); putRecordRequest.setSequenceNumberForOrdering("sequence number for ordering"); putRecordRequest.setPartitionKey("partition key"); String randomStr = RandomStringUtils.random(128 * 1024); putRecordRequest.setData(ByteBuffer.wrap(randomStr.getBytes(StringUtils.UTF8))); PutRecordRequestMarshaller marshaller = new PutRecordRequestMarshaller(); Request<PutRecordRequest> request = marshaller.marshall(putRecordRequest); assertEquals("content encoding", "gzip", request.getHeaders().get("Content-Encoding")); byte[] content = IOUtils.toByteArray(request.getContent()); assertEquals("content length", request.getHeaders().get("Content-Length"), String.valueOf(content.length)); GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content)); String str = IOUtils.toString(gis); Map<String, String> map = JsonUtils.jsonToMap(str); assertEquals("StreamName", "stream name", map.get("StreamName")); String data = new String(BinaryUtils.fromBase64(map.get("Data")), StringUtils.UTF8); assertEquals("same data", randomStr, data); } }
public Request<UpdateThingShadowRequest> marshall( UpdateThingShadowRequest updateThingShadowRequest) { if (updateThingShadowRequest == null) { throw new AmazonClientException( "Invalid argument passed to marshall(UpdateThingShadowRequest)"); } Request<UpdateThingShadowRequest> request = new DefaultRequest<UpdateThingShadowRequest>( updateThingShadowRequest, "AWSIotData"); request.setHttpMethod(HttpMethodName.POST); String uriResourcePath = "/things/{thingName}/shadow"; uriResourcePath = uriResourcePath.replace( "{thingName}", (updateThingShadowRequest.getThingName() == null) ? "" : StringUtils .fromString(updateThingShadowRequest.getThingName())); request.setResourcePath(uriResourcePath); request.addHeader("Content-Length", Integer.toString(updateThingShadowRequest.getPayload().remaining())); request.setContent(BinaryUtils.toStream(updateThingShadowRequest.getPayload())); if (!request.getHeaders().containsKey("Content-Type")) { request.addHeader("Content-Type", "application/x-amz-json-1.0"); } return request; } }
@Override public S3Object getObject(GetObjectRequest getObjectRequest) throws SdkClientException, AmazonServiceException { getObjectRequest = beforeClientExecution(getObjectRequest); assertNotNull(getObjectRequest, "GetObjectRequest"); assertStringNotEmpty(getObjectRequest.getBucketName(), "BucketName"); assertStringNotEmpty(getObjectRequest.getKey(), "Key"); Request<GetObjectRequest> request = createRequest(getObjectRequest.getBucketName(), getObjectRequest.getKey(), getObjectRequest, HttpMethodName.GET); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "GetObject"); request.addParameter("versionId", getObjectRequest.getVersionId()); addPartNumberIfNotNull(request, getObjectRequest.getPartNumber()); request.addHeader(Headers.RANGE, "bytes=" + Long.toString(range[0]) + "-" + Long.toString(range[1])); byte[] serverSideHash = BinaryUtils.fromHex(s3Object.getObjectMetadata().getETag()); try {
final ProgressListener listener) { try { request.setContent(inputStream); ObjectMetadata metadata = invoke(request, new S3MetadataResponseHandler(), bucketName, key); final String etag = metadata.getETag(); && !skipMd5CheckStrategy.skipClientSideValidationPerUploadPartResponse(metadata)) { byte[] clientSideHash = md5DigestStream.getMd5Digest(); byte[] serverSideHash = BinaryUtils.fromHex(etag); + ", partNumber: " + partNumber + ", partSize: " + partSize; throw new SdkClientException( "Unable to verify integrity of data upload. " + "Client calculated content hash (contentMD5: "
@Override public StructuredJsonGenerator writeValue(ByteBuffer bytes) { try { writer.writeBlob(BinaryUtils.copyAllBytesFrom(bytes)); } catch (IOException e) { throw new SdkClientException(e); } return this; }
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 String computedMd5() { FileInputStream fileStream = null; try { fileStream = new FileInputStream(request.getFile()); skipBytes(fileStream, request.getFileOffset()); return BinaryUtils.toBase64(computeMd5Bytes(fileStream, request.getPartSize())); } catch (IOException e) { throw new SdkClientException(e); } finally { if (fileStream != null) { try { fileStream.close(); } catch (IOException ignored) { } } } }
"The part size parameter must be specified when uploading a part"); final Request<UploadPartRequest> request = createRequest(bucketName, key, uploadPartRequest, HttpMethodName.PUT); request.addParameter("uploadId", uploadId); request.addParameter("partNumber", Integer.toString(partNumber)); populateRequestMetadata(request, objectMetadata); addHeaderIfNotNull(request, Headers.CONTENT_MD5, uploadPartRequest.getMd5Digest()); request.addHeader(Headers.CONTENT_LENGTH, Long.toString(partSize)); && !ServiceUtils.skipMd5CheckPerResponse(metadata)) { final byte[] clientSideHash = md5DigestStream.getMd5Digest(); final byte[] serverSideHash = BinaryUtils.fromHex(metadata.getETag());
"The key parameter must be specified when requesting an object"); final Request<GetObjectRequest> request = createRequest(getObjectRequest.getBucketName(), getObjectRequest.getKey(), getObjectRequest, HttpMethodName.GET); request.addParameter("versionId", getObjectRequest.getVersionId()); request.addHeader(Headers.RANGE, rangeHeader); populateRequesterPaysHeader(request, getObjectRequest.isRequesterPays()); addResponseHeaderParameters(request, getObjectRequest.getResponseHeaders()); addDateHeader(request, Headers.GET_OBJECT_IF_MODIFIED_SINCE, final String etag = s3Object.getObjectMetadata().getETag(); if (etag != null && ServiceUtils.isMultipartUploadETag(etag) == false) { serverSideHash = BinaryUtils.fromHex(s3Object.getObjectMetadata().getETag()); try {
/** * Binary data should be placed as is, directly into the content. */ private void marshallBinaryPayload(Object val) { if (val instanceof ByteBuffer) { request.setContent(BinaryUtils.toStream((ByteBuffer) val)); } else if (val instanceof InputStream) { request.setContent((InputStream) val); } }
/** * If necessary, creates a chunk-encoding wrapper on the request payload. */ @Override protected void processRequestPayload(Request<?> request, HeaderSigningResult headerSigningResult) { if (useChunkEncoding(request)) { InputStream payloadStream = request.getContent(); String dateTime = headerSigningResult.getDateTime(); String keyPath = headerSigningResult.getScope(); byte[] kSigning = headerSigningResult.getKSigning(); String signature = BinaryUtils.toHex(headerSigningResult .getSignature()); AwsChunkedEncodingInputStream chunkEncodededStream = new AwsChunkedEncodingInputStream( payloadStream, kSigning, dateTime, keyPath, signature, this); request.setContent(chunkEncodededStream); } }
/** * Creates the authorization header to be included in the request. */ private String buildAuthorizationHeader(SignableRequest<?> request, byte[] signature, AWSCredentials credentials, AWS4SignerRequestParams signerParams) { final String signingCredentials = credentials.getAWSAccessKeyId() + "/" + signerParams.getScope(); final String credential = "Credential=" + signingCredentials; final String signerHeaders = "SignedHeaders=" + getSignedHeadersString(request); final String signatureHeader = "Signature=" + BinaryUtils.toHex(signature); final StringBuilder authHeaderBuilder = new StringBuilder(); authHeaderBuilder.append(AWS4_SIGNING_ALGORITHM) .append(" ") .append(credential) .append(", ") .append(signerHeaders) .append(", ") .append(signatureHeader); return authHeaderBuilder.toString(); }
/** * 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; }