Skip to content

Instantly share code, notes, and snippets.

@harshavardhana
Created August 7, 2020 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshavardhana/2df5e8b512a40924226c2ebb98401f41 to your computer and use it in GitHub Desktop.
Save harshavardhana/2df5e8b512a40924226c2ebb98401f41 to your computer and use it in GitHub Desktop.
# This version makes a GET request and passes the signature
# in the Authorization header.
import sys, os, base64, datetime, hashlib, hmac, urllib
import requests
import logging
from minio.signer import presign_v4
from minio.helpers import get_target_url
from minio.credentials import Credentials, Static
# These two lines enable debugging at httplib level (requests->urllib3->http.client)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
httpclient_logger = logging.getLogger("http.client")
class S3HeaderVerification(object):
def validateRequest(self, method, url, credentials, region=None, headers=None, expires=None,
response_headers=None, request_date=None):
if method == 'GET':
print(presign_v4(method, url, credentials, region, headers, expires, response_headers, request_date))
requests.get(presign_v4(method, url, credentials, region, headers, expires, response_headers, request_date))
elif method == 'HEAD':
requests.head(presign_v4(method, url, credentials, region, headers, expires, response_headers, request_date))
# add more methods here...
s3headerVerify = S3HeaderVerification()
method = 'GET'
region = 'us-east-1'
endpoint = 'https://play.minio.io'
credentials = Credentials(
provider=Static(os.environ.get('AWS_ACCESS_KEY_ID', 'minio'),
os.environ.get('AWS_SECRET_ACCESS_KEY', 'minio123')),
)
url = get_target_url(endpoint, bucket_name='mybucket', object_name='myobject', bucket_region=region)
s3headerVerify.validateRequest(method, url, credentials, region=region)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment