Scrapy, a fast high-level web crawling & scraping framework for Python.
support s3 signing on pre and post boto v2.0
Daniel Grana436ad6393005
diff --git a/scrapy/core/downloader/handlers/s3.py b/scrapy/core/downloader/handlers/s3.pyindex b9c877a9d..9033126c6 100644--- a/scrapy/core/downloader/handlers/s3.py+++ b/scrapy/core/downloader/handlers/s3.py@@ -4,6 +4,29 @@ from scrapy.utils.httpobj import urlparse_cachedfrom scrapy.conf import settingsfrom .http import HttpDownloadHandler+try:+ from boto.s3.connection import S3Connection+except ImportError:+ S3Connection = object++class _v19_S3Connection(S3Connection):+ """A dummy S3Connection wrapper that doesn't do any syncronous download"""+ def _mexe(self, method, bucket, key, headers, *args, **kwargs):+ return headers++class _v20_S3Connection(S3Connection):+ """A dummy S3Connection wrapper that doesn't do any syncronous download"""+ def _mexe(self, http_request, *args):+ http_request.authorize(connection=self)+ return http_request.headers++try:+ import boto.auth+except ImportError:+ _S3Connection = _v19_S3Connection+else:+ _S3Connection = _v20_S3Connection+class S3DownloadHandler(object):@@ -17,9 +40,8 @@ class S3DownloadHandler(object):if not aws_secret_access_key:aws_secret_access_key = settings['AWS_SECRET_ACCESS_KEY']- from boto import connect_s3try:- self.conn = connect_s3(aws_access_key_id, aws_secret_access_key)+ self.conn = _S3Connection(aws_access_key_id, aws_secret_access_key)except Exception, ex:raise NotConfigured(str(ex))self._download_http = httpdownloadhandler().download_request@@ -30,7 +52,12 @@ class S3DownloadHandler(object):bucket = p.hostnamepath = p.path + '?' + p.query if p.query else p.pathurl = '%s://%s.s3.amazonaws.com%s' % (scheme, bucket, path)- httpreq = request.replace(url=url)- self.conn.add_aws_auth_header(httpreq.headers, httpreq.method, \- '%s/%s' % (bucket, path))+ signed_headers = self.conn.make_request(+ method=request.method,+ bucket=bucket,+ key=p.path,+ query_args=p.query,+ headers=request.headers,+ data=request.body)+ httpreq = request.replace(url=url, headers=signed_headers)return self._download_http(httpreq, spider)