def __call__(self, req):
        """
        Interface used by Requests module to apply authentication to HTTP
        requests.

        Add x-amz-content-sha256 and Authorization headers to the request. Add
        x-amz-date header to request if not already present and req does not
        contain a Date header.

        Check request date matches date in the current signing key. If not,
        regenerate signing key to match request date.

        If request body is not already encoded to bytes, encode to charset
        specified in Content-Type header, or UTF-8 if not specified.

        req -- Requests PreparedRequest object

        """
        # check request date matches scope date
        req_date = self.get_request_date(req)
        if req_date is None:
            # no date headers or none in recognisable format
            # replace them with x-amz-header with current date and time
            if 'date' in req.headers: del req.headers['date']
            if 'x-amz-date' in req.headers: del req.headers['x-amz-date']
            now = datetime.datetime.utcnow()
            req_date = now.date()
            req.headers['x-amz-date'] = now.strftime('%Y%m%dT%H%M%SZ')
        req_scope_date = req_date.strftime('%Y%m%d')
        if req_scope_date != self.date:
            self.handle_date_mismatch(req)

        # encode body and generate body hash
        if hasattr(req, 'body') and req.body is not None:
            self.encode_body(req)
            content_hash = hashlib.sha256(req.body)
        else:
            content_hash = hashlib.sha256(b'')
        req.headers['x-amz-content-sha256'] = content_hash.hexdigest()
        if self.session_token:
            req.headers['x-amz-security-token'] = self.session_token

        # generate signature
        result = self.get_canonical_headers(req, self.include_hdrs)
        cano_headers, signed_headers = result
        cano_req = self.get_canonical_request(req, cano_headers,
                                              signed_headers)
        sig_string = self.get_sig_string(req, cano_req, self.signing_key.scope)
        sig_string = sig_string.encode('utf-8')
        hsh = hmac.new(self.signing_key.key, sig_string, hashlib.sha256)
        sig = hsh.hexdigest()
        auth_str = 'AWS4-HMAC-SHA256 '
        auth_str += 'Credential={}/{}, '.format(self.access_id,
                                                self.signing_key.scope)
        auth_str += 'SignedHeaders={}, '.format(signed_headers)
        auth_str += 'Signature={}'.format(sig)
        req.headers['Authorization'] = auth_str
        return req
def __call__ ( self , req ) : req_date = self . get_request_date ( req ) if req_date is None : if 'date' in req . headers : del req . headers [ 'date' ] if 'x-amz-date' in req . headers : del req . headers [ 'x-amz-date' ] now = datetime . datetime . utcnow ( ) req_date = now . date ( ) req . headers [ 'x-amz-date' ] = now . strftime ( '%Y%m%dT%H%M%SZ' ) req_scope_date = req_date . strftime ( '%Y%m%d' ) if req_scope_date != self . date : self . handle_date_mismatch ( req ) if hasattr ( req , 'body' ) and req . body is not None : self . encode_body ( req ) content_hash = hashlib . sha256 ( req . body ) else : content_hash = hashlib . sha256 ( b'' ) req . headers [ 'x-amz-content-sha256' ] = content_hash . hexdigest ( ) if self . session_token : req . headers [ 'x-amz-security-token' ] = self . session_token result = self . get_canonical_headers ( req , self . include_hdrs ) cano_headers , signed_headers = result cano_req = self . get_canonical_request ( req , cano_headers , signed_headers ) sig_string = self . get_sig_string ( req , cano_req , self . signing_key . scope ) sig_string = sig_string . encode ( 'utf-8' ) hsh = hmac . new ( self . signing_key . key , sig_string , hashlib . sha256 ) sig = hsh . hexdigest ( ) auth_str = 'AWS4-HMAC-SHA256 ' auth_str += 'Credential={}/{}, ' . format ( self . access_id , self . signing_key . scope ) auth_str += 'SignedHeaders={}, ' . format ( signed_headers ) auth_str += 'Signature={}' . format ( sig ) req . headers [ 'Authorization' ] = auth_str return req
Interface used by Requests module to apply authentication to HTTP
        requests.

        Add x-amz-content-sha256 and Authorization headers to the request. Add
        x-amz-date header to request if not already present and req does not
        contain a Date header.

        Check request date matches date in the current signing key. If not,
        regenerate signing key to match request date.

        If request body is not already encoded to bytes, encode to charset
        specified in Content-Type header, or UTF-8 if not specified.

        req -- Requests PreparedRequest object