requests-aws-0.1.5/0000755000076500000240000000000012242737117014767 5ustar paultaxstaff00000000000000requests-aws-0.1.5/awsauth.py0000644000076500000240000000765112242736400017020 0ustar paultaxstaff00000000000000# -*- coding: utf-8 -*- import base64 import hmac from hashlib import sha1 as sha py3k = False try: from urlparse import urlparse from base64 import encodestring except: py3k = True from urllib.parse import urlparse from base64 import encodebytes as encodestring from email.utils import formatdate from requests.auth import AuthBase class S3Auth(AuthBase): """Attaches AWS Authentication to the given Request object.""" service_base_url = 's3.amazonaws.com' # List of Query String Arguments of Interest special_params = [ 'acl', 'location', 'logging', 'partNumber', 'policy', 'requestPayment', 'torrent', 'versioning', 'versionId', 'versions', 'website', 'uploads', 'uploadId', 'response-content-type', 'response-content-language', 'response-expires', 'response-cache-control', 'delete', 'lifecycle', 'response-content-disposition', 'response-content-encoding' ] def __init__(self, access_key, secret_key, service_url=None): if service_url: self.service_base_url = service_url self.access_key = str(access_key) self.secret_key = str(secret_key) def __call__(self, r): # Create date header if it is not created yet. if not 'date' in r.headers and not 'x-amz-date' in r.headers: r.headers['date'] = formatdate( timeval=None, localtime=False, usegmt=True) signature = self.get_signature(r) if py3k: signature = signature.decode('utf-8') r.headers['Authorization'] = 'AWS %s:%s' % (self.access_key, signature) return r def get_signature(self, r): canonical_string = self.get_canonical_string( r.url, r.headers, r.method) if py3k: key = self.secret_key.encode('utf-8') msg = canonical_string.encode('utf-8') else: key = self.secret_key msg = canonical_string h = hmac.new(key, msg, digestmod=sha) return encodestring(h.digest()).strip() def get_canonical_string(self, url, headers, method): parsedurl = urlparse(url) objectkey = parsedurl.path[1:] query_args = sorted(parsedurl.query.split('&')) bucket = parsedurl.netloc[:-len(self.service_base_url)] if len(bucket) > 1: # remove last dot bucket = bucket[:-1] interesting_headers = { 'content-md5': '', 'content-type': '', 'date': ''} for key in headers: lk = key.lower() try: lk = lk.decode('utf-8') except: pass if headers[key] and (lk in interesting_headers.keys() or lk.startswith('x-amz-')): interesting_headers[lk] = headers[key].strip() # If x-amz-date is used it supersedes the date header. if not py3k: if 'x-amz-date' in interesting_headers: interesting_headers['date'] = '' else: if 'x-amz-date' in interesting_headers: interesting_headers['date'] = '' buf = '%s\n' % method for key in sorted(interesting_headers.keys()): val = interesting_headers[key] if key.startswith('x-amz-'): buf += '%s:%s\n' % (key, val) else: buf += '%s\n' % val # append the bucket if it exists if bucket != '': buf += '/%s' % bucket # add the objectkey. even if it doesn't exist, add the slash buf += '/%s' % objectkey params_found = False # handle special query string arguments for q in query_args: k = q.split('=')[0] if k in self.special_params: if params_found: buf += '&%s' % q else: buf += '?%s' % q params_found = True return buf requests-aws-0.1.5/example.py0000644000076500000240000000253612242736400016774 0ustar paultaxstaff00000000000000#!/usr/bin/env python import requests from awsauth import S3Auth import StringIO import gzip import urllib ACCESS_KEY = "ACCESSKEYXXXXXXXXXXXX" SECRET_KEY = "AWSSECRETKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" acceptableAccessCodes = (200, 204) # # https://forums.aws.amazon.com/thread.jspa?threadID=28799: http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectDELETE.html if __name__ == '__main__': confirmIt = u'Sam is sweet' # Data needs to be in unicode, or it will fail bucketName = 'mybucket' objectName = ['myfile.txt', 'my+file.txt'] for o in objectName: # Creating a file r = requests.put(('http://%s.s3.amazonaws.com/%s' % (bucketName, o)), data=confirmIt, auth=S3Auth(ACCESS_KEY, SECRET_KEY)) if r.status_code not in acceptableAccessCodes: r.raise_for_status() # Downloading a file r = requests.get(('http://%s.s3.amazonaws.com/%s' % (bucketName, o)), auth=S3Auth(ACCESS_KEY, SECRET_KEY)) if r.status_code not in acceptableAccessCodes: r.raise_for_status() if r.content == confirmIt: print 'Hala Madrid!' # Removing a file r = requests.delete(('http://%s.s3.amazonaws.com/%s' % (bucketName, o)), auth=S3Auth(ACCESS_KEY, SECRET_KEY)) if r.status_code not in acceptableAccessCodes: r.raise_for_status() requests-aws-0.1.5/LICENSE.txt0000644000076500000240000000275212242736400016612 0ustar paultaxstaff00000000000000Copyright (c) 2012-2013 Paul Tax All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Infrae nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INFRAE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. requests-aws-0.1.5/MANIFEST.in0000644000076500000240000000004212242736573016526 0ustar paultaxstaff00000000000000include LICENSE.txt README.md *.pyrequests-aws-0.1.5/PKG-INFO0000644000076500000240000000320012242737117016057 0ustar paultaxstaff00000000000000Metadata-Version: 1.0 Name: requests-aws Version: 0.1.5 Summary: AWS authentication for Amazon S3 for the python requests module Home-page: https://github.com/tax/python-requests-aws Author: Paul Tax Author-email: paultax@gmail.com License: BSD licence, see LICENCE.txt Description: #S3 using python-requests AWS authentication for Amazon S3 for the wonderful [pyhon requests library](http://python-requests.org) - Tested with python 2.6 and python 3.3.2 - At the moment only S3 is supported ## Usage ```python import requests from awsauth import S3Auth ACCESS_KEY = 'ACCESSKEYXXXXXXXXXXXX' SECRET_KEY = 'AWSSECRETKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' s = 'Sam is sweet' # Creating a file r = requests.put('http://mybuck.s3.amazonaws.com/file.txt', data=s, auth=S3Auth(ACCESS_KEY, SECRET_KEY)) # Downloading a file r = requests.get('http://mybuck.s3.amazonaws.com/file.txt', auth=S3Auth(ACCESS_KEY, SECRET_KEY)) if r.text == 'Sam is sweet': print "It works" # Removing a file r = requests.delete('http://mybuck.s3.amazonaws.com/file.txt', auth=S3Auth(ACCESS_KEY, SECRET_KEY)) ``` ## Installation Installing requests-aws is simple with pip: ``` $ pip install requests-aws ``` [![Build Status](https://travis-ci.org/tax/python-requests-aws.png?branch=master)](https://travis-ci.org/tax/python-requests-aws) Platform: UNKNOWN requests-aws-0.1.5/README.md0000644000076500000240000000203212242736400016235 0ustar paultaxstaff00000000000000#S3 using python-requests AWS authentication for Amazon S3 for the wonderful [pyhon requests library](http://python-requests.org) - Tested with python 2.6 and python 3.3.2 - At the moment only S3 is supported ## Usage ```python import requests from awsauth import S3Auth ACCESS_KEY = 'ACCESSKEYXXXXXXXXXXXX' SECRET_KEY = 'AWSSECRETKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' s = 'Sam is sweet' # Creating a file r = requests.put('http://mybuck.s3.amazonaws.com/file.txt', data=s, auth=S3Auth(ACCESS_KEY, SECRET_KEY)) # Downloading a file r = requests.get('http://mybuck.s3.amazonaws.com/file.txt', auth=S3Auth(ACCESS_KEY, SECRET_KEY)) if r.text == 'Sam is sweet': print "It works" # Removing a file r = requests.delete('http://mybuck.s3.amazonaws.com/file.txt', auth=S3Auth(ACCESS_KEY, SECRET_KEY)) ``` ## Installation Installing requests-aws is simple with pip: ``` $ pip install requests-aws ``` [![Build Status](https://travis-ci.org/tax/python-requests-aws.png?branch=master)](https://travis-ci.org/tax/python-requests-aws) requests-aws-0.1.5/requests_aws.egg-info/0000755000076500000240000000000012242737117021206 5ustar paultaxstaff00000000000000requests-aws-0.1.5/requests_aws.egg-info/dependency_links.txt0000644000076500000240000000000112242737117025254 0ustar paultaxstaff00000000000000 requests-aws-0.1.5/requests_aws.egg-info/PKG-INFO0000644000076500000240000000320012242737117022276 0ustar paultaxstaff00000000000000Metadata-Version: 1.0 Name: requests-aws Version: 0.1.5 Summary: AWS authentication for Amazon S3 for the python requests module Home-page: https://github.com/tax/python-requests-aws Author: Paul Tax Author-email: paultax@gmail.com License: BSD licence, see LICENCE.txt Description: #S3 using python-requests AWS authentication for Amazon S3 for the wonderful [pyhon requests library](http://python-requests.org) - Tested with python 2.6 and python 3.3.2 - At the moment only S3 is supported ## Usage ```python import requests from awsauth import S3Auth ACCESS_KEY = 'ACCESSKEYXXXXXXXXXXXX' SECRET_KEY = 'AWSSECRETKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' s = 'Sam is sweet' # Creating a file r = requests.put('http://mybuck.s3.amazonaws.com/file.txt', data=s, auth=S3Auth(ACCESS_KEY, SECRET_KEY)) # Downloading a file r = requests.get('http://mybuck.s3.amazonaws.com/file.txt', auth=S3Auth(ACCESS_KEY, SECRET_KEY)) if r.text == 'Sam is sweet': print "It works" # Removing a file r = requests.delete('http://mybuck.s3.amazonaws.com/file.txt', auth=S3Auth(ACCESS_KEY, SECRET_KEY)) ``` ## Installation Installing requests-aws is simple with pip: ``` $ pip install requests-aws ``` [![Build Status](https://travis-ci.org/tax/python-requests-aws.png?branch=master)](https://travis-ci.org/tax/python-requests-aws) Platform: UNKNOWN requests-aws-0.1.5/requests_aws.egg-info/requires.txt0000644000076500000240000000002012242737117023576 0ustar paultaxstaff00000000000000requests>=0.14.0requests-aws-0.1.5/requests_aws.egg-info/SOURCES.txt0000644000076500000240000000037312242737117023075 0ustar paultaxstaff00000000000000LICENSE.txt MANIFEST.in README.md awsauth.py example.py setup.py test.py requests_aws.egg-info/PKG-INFO requests_aws.egg-info/SOURCES.txt requests_aws.egg-info/dependency_links.txt requests_aws.egg-info/requires.txt requests_aws.egg-info/top_level.txtrequests-aws-0.1.5/requests_aws.egg-info/top_level.txt0000644000076500000240000000001012242737117023727 0ustar paultaxstaff00000000000000awsauth requests-aws-0.1.5/setup.cfg0000644000076500000240000000007312242737117016610 0ustar paultaxstaff00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 requests-aws-0.1.5/setup.py0000644000076500000240000000131112242736611016473 0ustar paultaxstaff00000000000000# -*- coding: utf-8 -*- import os import sys try: from setuptools import setup # hush pyflakes setup except ImportError: from distutils.core import setup if sys.argv[-1] == 'publish': os.system('python setup.py sdist upload') sys.exit() setup( name='requests-aws', version='0.1.5', author='Paul Tax', author_email='paultax@gmail.com', include_package_data=True, install_requires = ['requests>=0.14.0'], py_modules=['awsauth'], url='https://github.com/tax/python-requests-aws', license='BSD licence, see LICENCE.txt', description='AWS authentication for Amazon S3 for the python requests module', long_description=open('README.md').read(), ) requests-aws-0.1.5/test.py0000644000076500000240000000441512242736400016316 0ustar paultaxstaff00000000000000import unittest import os import requests from awsauth import S3Auth TEST_BUCKET = 'testpolpol' ACCESS_KEY = 'ACCESSKEYXXXXXXXXXXXX' SECRET_KEY = 'AWSSECRETKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' if 'AWS_ACCESS_KEY' in os.environ: ACCESS_KEY = os.environ['AWS_ACCESS_KEY'] if 'AWS_SECRET_KEY' in os.environ: SECRET_KEY = os.environ['AWS_SECRET_KEY'] class TestAWS(unittest.TestCase): def setUp(self): self.auth=S3Auth(ACCESS_KEY, SECRET_KEY) def test_put_get_delete(self): testdata = 'Sam is sweet' r = requests.put('http://'+ TEST_BUCKET + '.s3.amazonaws.com/myfile.txt', data=testdata, auth=self.auth) self.assertEqual(r.status_code, 200) # Downloading a file r = requests.get('http://'+ TEST_BUCKET + '.s3.amazonaws.com/myfile.txt', auth=self.auth) self.assertEqual(r.status_code, 200) self.assertEqual(r.text, 'Sam is sweet') # Removing a file r = requests.delete('http://'+ TEST_BUCKET + '.s3.amazonaws.com/myfile.txt', auth=self.auth) self.assertEqual(r.status_code, 204) def test_put_get_delete_filnamehasplus(self): testdata = 'Sam is sweet' filename = 'my+file.txt' url = 'http://'+ TEST_BUCKET + '.s3.amazonaws.com/%s'%(filename) r = requests.put(url, data=testdata, auth=self.auth) self.assertEqual(r.status_code, 200) # Downloading a file r = requests.get(url, auth=self.auth) self.assertEqual(r.status_code, 200) self.assertEqual(r.text, testdata) # Removing a file r = requests.delete(url, auth=self.auth) self.assertEqual(r.status_code, 204) def test_put_get_delete_filname_encoded(self): testdata = 'Sam is sweet' filename = 'my%20file.txt' url = 'http://'+ TEST_BUCKET + '.s3.amazonaws.com/%s'%(filename) r = requests.put(url, data=testdata, auth=self.auth) self.assertEqual(r.status_code, 200) # Downloading a file r = requests.get(url, auth=self.auth) self.assertEqual(r.status_code, 200) self.assertEqual(r.text, testdata) # Removing a file r = requests.delete(url, auth=self.auth) self.assertEqual(r.status_code, 204) if __name__ == '__main__': unittest.main()