Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2013-2015, Inc. or its affiliates. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
  * or in the "license" file accompanying this file. This file is distributed
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
 import static com.amazonaws.auth.internal.SignerConstants.X_AMZ_CONTENT_SHA256;
AWS4 signer implementation for AWS S3
 public class AWSS3V4Signer extends AWS4Signer {
     private static final String CONTENT_SHA_256 = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD";

Don't double-url-encode path elements; S3 expects path elements to be encoded only once in the canonical URI.
     public AWSS3V4Signer() {

If necessary, creates a chunk-encoding wrapper on the request payload.
     protected void processRequestPayload(SignableRequest<?> requestbyte[] signature,
             byte[] signingKeyAWS4SignerRequestParams signerRequestParams) {
         if (useChunkEncoding(request)) {
             AwsChunkedEncodingInputStream chunkEncodededStream = new AwsChunkedEncodingInputStream(
                     request.getContent(), signingKey,
                     BinaryUtils.toHex(signature), this);
     protected String calculateContentHashPresign(SignableRequest<?> request){
         return "UNSIGNED-PAYLOAD";

Returns the pre-defined header value and set other necessary headers if the request needs to be chunk-encoded. Otherwise calls the superclass method which calculates the hash of the whole content for signing.
     protected String calculateContentHash(SignableRequest<?> request) {
         // To be consistent with other service clients using sig-v4,
         // we just set the header as "required", and AWS4Signer.sign() will be
         // notified to pick up the header value returned by this method.
         if (useChunkEncoding(request)) {
             final String contentLength =
             final long originalContentLength;
             if (contentLength != null) {
                 originalContentLength = Long.parseLong(contentLength);
             } else {
"Content-Length" header could be missing if the caller is uploading a stream without setting Content-Length in ObjectMetadata. Before using sigv4, we rely on HttpClient to add this header by using BufferedHttpEntity when creating the HttpRequest object. But now, we need this information immediately for the signing process, so we have to cache the stream here.
                 try {
                     originalContentLength = getContentLength(request);
                 } catch (IOException e) {
                     throw new AmazonClientException(
                            "Cannot get the content-length of the request content.",
            // Make sure "Content-Length" header is not empty so that HttpClient
            // won't cache the stream again to recover Content-Length
            return ;
        return super.calculateContentHash(request);

Determine whether to use aws-chunked for signing
    private static boolean useChunkEncoding(SignableRequest<?> request) {
        // Whether to use chunked encoding for signing the request
        boolean chunkedEncodingEnabled = false;
        if (request.getOriginalRequestObject() instanceof PutObjectRequest
                || request.getOriginalRequestObject() instanceof UploadPartRequest) {
            chunkedEncodingEnabled = true;
        return chunkedEncodingEnabled;

Read the content of the request to get the length of the stream. This method will wrap the stream by SdkBufferedInputStream if it is not mark-supported.
    static long getContentLength(SignableRequest<?> requestthrows IOException {
        final InputStream content = request.getContent();
        if (!content.markSupported())
            throw new IllegalStateException("Bug: request input stream must have been made mark-and-resettable at this point");
        ReadLimitInfo info = request.getReadLimitInfo();
        final int readLimit = info.getReadLimit();
        long contentLength = 0;
        byte[] tmp = new byte[4096];
        int read;
        while ((read = != -1) {
            contentLength += read;
        try {
        } catch(IOException ex) {
            throw new ResetException("Failed to reset the input stream"ex);
        return contentLength;
New to GrepCode? Check out our FAQ X