Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 511 lines (420 sloc) 18.874 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
# Copyright (c) 2006-2011 Mitch Garnaat http://garnaat.org/
# Copyright (c) 2010-2011, Eucalyptus Systems, Inc.
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
import boto
from boto.pyami.config import Config, BotoConfigLocations
from boto.storage_uri import BucketStorageUri, FileStorageUri
import boto.plugin
import os, re, sys
import logging
import logging.config
from boto.exception import InvalidUriError

__version__ = '2.0b4'
Version = __version__ # for backware compatibility

UserAgent = 'Boto/%s (%s)' % (__version__, sys.platform)
config = Config()

def init_logging():
    for file in BotoConfigLocations:
        try:
            logging.config.fileConfig(os.path.expanduser(file))
        except:
            pass

class NullHandler(logging.Handler):
    def emit(self, record):
        pass

log = logging.getLogger('boto')
log.addHandler(NullHandler())
init_logging()

# convenience function to set logging to a particular file
def set_file_logger(name, filepath, level=logging.INFO, format_string=None):
    global log
    if not format_string:
        format_string = "%(asctime)s %(name)s [%(levelname)s]:%(message)s"
    logger = logging.getLogger(name)
    logger.setLevel(level)
    fh = logging.FileHandler(filepath)
    fh.setLevel(level)
    formatter = logging.Formatter(format_string)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    log = logger

def set_stream_logger(name, level=logging.DEBUG, format_string=None):
    global log
    if not format_string:
        format_string = "%(asctime)s %(name)s [%(levelname)s]:%(message)s"
    logger = logging.getLogger(name)
    logger.setLevel(level)
    fh = logging.StreamHandler()
    fh.setLevel(level)
    formatter = logging.Formatter(format_string)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    log = logger

def connect_sqs(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.sqs.connection.SQSConnection`
:return: A connection to Amazon's SQS
"""
    from boto.sqs.connection import SQSConnection
    return SQSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_s3(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.s3.connection.S3Connection`
:return: A connection to Amazon's S3
"""
    from boto.s3.connection import S3Connection
    return S3Connection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_gs(gs_access_key_id=None, gs_secret_access_key=None, **kwargs):
    """
@type gs_access_key_id: string
@param gs_access_key_id: Your Google Storage Access Key ID

@type gs_secret_access_key: string
@param gs_secret_access_key: Your Google Storage Secret Access Key

@rtype: L{GSConnection<boto.gs.connection.GSConnection>}
@return: A connection to Google's Storage service
"""
    from boto.gs.connection import GSConnection
    return GSConnection(gs_access_key_id, gs_secret_access_key, **kwargs)

def connect_ec2(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.ec2.connection.EC2Connection`
:return: A connection to Amazon's EC2
"""
    from boto.ec2.connection import EC2Connection
    return EC2Connection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_elb(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.ec2.elb.ELBConnection`
:return: A connection to Amazon's Load Balancing Service
"""
    from boto.ec2.elb import ELBConnection
    return ELBConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_autoscale(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.ec2.autoscale.AutoScaleConnection`
:return: A connection to Amazon's Auto Scaling Service
"""
    from boto.ec2.autoscale import AutoScaleConnection
    return AutoScaleConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_cloudwatch(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.ec2.cloudwatch.CloudWatchConnection`
:return: A connection to Amazon's EC2 Monitoring service
"""
    from boto.ec2.cloudwatch import CloudWatchConnection
    return CloudWatchConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_sdb(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.sdb.connection.SDBConnection`
:return: A connection to Amazon's SDB
"""
    from boto.sdb.connection import SDBConnection
    return SDBConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_fps(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.fps.connection.FPSConnection`
:return: A connection to FPS
"""
    from boto.fps.connection import FPSConnection
    return FPSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_mturk(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.mturk.connection.MTurkConnection`
:return: A connection to MTurk
"""
    from boto.mturk.connection import MTurkConnection
    return MTurkConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_cloudfront(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.fps.connection.FPSConnection`
:return: A connection to FPS
"""
    from boto.cloudfront import CloudFrontConnection
    return CloudFrontConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_vpc(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.vpc.VPCConnection`
:return: A connection to VPC
"""
    from boto.vpc import VPCConnection
    return VPCConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_rds(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.rds.RDSConnection`
:return: A connection to RDS
"""
    from boto.rds import RDSConnection
    return RDSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_emr(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID
:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key
:rtype: :class:`boto.emr.EmrConnection`
:return: A connection to Elastic mapreduce
"""
    from boto.emr import EmrConnection
    return EmrConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_sns(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.sns.SNSConnection`
:return: A connection to Amazon's SNS
"""
    from boto.sns import SNSConnection
    return SNSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)


def connect_iam(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.iam.IAMConnection`
:return: A connection to Amazon's IAM
"""
    from boto.iam import IAMConnection
    return IAMConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_route53(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.dns.Route53Connection`
:return: A connection to Amazon's Route53 DNS Service
"""
    from boto.route53 import Route53Connection
    return Route53Connection(aws_access_key_id, aws_secret_access_key, **kwargs)

def connect_euca(host, aws_access_key_id=None, aws_secret_access_key=None,
                 port=8773, path='/services/Eucalyptus', is_secure=False,
                 **kwargs):
    """
Connect to a Eucalyptus service.

:type host: string
:param host: the host name or ip address of the Eucalyptus server
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.ec2.connection.EC2Connection`
:return: A connection to Eucalyptus server
"""
    from boto.ec2 import EC2Connection
    from boto.ec2.regioninfo import RegionInfo

    reg = RegionInfo(name='eucalyptus', endpoint=host)
    return EC2Connection(aws_access_key_id, aws_secret_access_key,
                         region=reg, port=port, path=path,
                         is_secure=is_secure, **kwargs)

def connect_walrus(host, aws_access_key_id=None, aws_secret_access_key=None,
                   port=8773, path='/services/Walrus', is_secure=False,
                   **kwargs):
    """
Connect to a Walrus service.

:type host: string
:param host: the host name or ip address of the Walrus server
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.s3.connection.S3Connection`
:return: A connection to Walrus
"""
    from boto.s3.connection import S3Connection
    from boto.s3.connection import OrdinaryCallingFormat

    return S3Connection(aws_access_key_id, aws_secret_access_key,
                        host=host, port=port, path=path,
                        calling_format=OrdinaryCallingFormat(),
                        is_secure=is_secure, **kwargs)

def connect_ses(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
:type aws_access_key_id: string
:param aws_access_key_id: Your AWS Access Key ID

:type aws_secret_access_key: string
:param aws_secret_access_key: Your AWS Secret Access Key

:rtype: :class:`boto.ses.SESConnection`
:return: A connection to Amazon's SES
"""
    from boto.ses import SESConnection
    return SESConnection(aws_access_key_id, aws_secret_access_key, **kwargs)

def check_extensions(module_name, module_path):
    """
This function checks for extensions to boto modules. It should be called in the
__init__.py file of all boto modules. See:
http://code.google.com/p/boto/wiki/ExtendModules

for details.
"""
    option_name = '%s_extend' % module_name
    version = config.get('Boto', option_name, None)
    if version:
        dirname = module_path[0]
        path = os.path.join(dirname, version)
        if os.path.isdir(path):
            log.info('extending module %s with: %s' % (module_name, path))
            module_path.insert(0, path)

_aws_cache = {}

def _get_aws_conn(service):
    global _aws_cache
    conn = _aws_cache.get(service)
    if not conn:
        meth = getattr(sys.modules[__name__], 'connect_' + service)
        conn = meth()
        _aws_cache[service] = conn
    return conn

def lookup(service, name):
    global _aws_cache
    conn = _get_aws_conn(service)
    obj = _aws_cache.get('.'.join((service, name)), None)
    if not obj:
        obj = conn.lookup(name)
        _aws_cache['.'.join((service, name))] = obj
    return obj

def storage_uri(uri_str, default_scheme='file', debug=0, validate=True,
                bucket_storage_uri_class=BucketStorageUri):
    """
Instantiate a StorageUri from a URI string.

:type uri_str: string
:param uri_str: URI naming bucket + optional object.
:type default_scheme: string
:param default_scheme: default scheme for scheme-less URIs.
:type debug: int
:param debug: debug level to pass in to boto connection (range 0..2).
:type validate: bool
:param validate: whether to check for bucket name validity.
:type bucket_storage_uri_class: BucketStorageUri interface.
:param bucket_storage_uri_class: Allows mocking for unit tests.

We allow validate to be disabled to allow caller
to implement bucket-level wildcarding (outside the boto library;
see gsutil).

:rtype: :class:`boto.StorageUri` subclass
:return: StorageUri subclass for given URI.

``uri_str`` must be one of the following formats:

* gs://bucket/name
* s3://bucket/name
* gs://bucket
* s3://bucket
* filename

The last example uses the default scheme ('file', unless overridden)
"""

    # Manually parse URI components instead of using urlparse.urlparse because
    # what we're calling URIs don't really fit the standard syntax for URIs
    # (the latter includes an optional host/net location part).
    end_scheme_idx = uri_str.find('://')
    if end_scheme_idx == -1:
        # Check for common error: user specifies gs:bucket instead
        # of gs://bucket. Some URI parsers allow this, but it can cause
        # confusion for callers, so we don't.
        if uri_str.find(':') != -1:
            raise InvalidUriError('"%s" contains ":" instead of "://"' % uri_str)
        scheme = default_scheme.lower()
        path = uri_str
    else:
        scheme = uri_str[0:end_scheme_idx].lower()
        path = uri_str[end_scheme_idx + 3:]

    if scheme not in ['file', 's3', 'gs']:
        raise InvalidUriError('Unrecognized scheme "%s"' % scheme)
    if scheme == 'file':
        # For file URIs we have no bucket name, and use the complete path
        # (minus 'file://') as the object name.
        return FileStorageUri(path, debug)
    else:
        path_parts = path.split('/', 1)
        bucket_name = path_parts[0]
        if (validate and bucket_name and
            # Disallow buckets violating charset or not [3..255] chars total.
            (not re.match('^[a-z0-9][a-z0-9\._-]{1,253}[a-z0-9]$', bucket_name)
            # Disallow buckets with individual DNS labels longer than 63.
             or re.search('[-_a-z0-9]{64}', bucket_name))):
            raise InvalidUriError('Invalid bucket name in URI "%s"' % uri_str)
        # If enabled, ensure the bucket name is valid, to avoid possibly
        # confusing other parts of the code. (For example if we didn't
        # catch bucket names containing ':', when a user tried to connect to
        # the server with that name they might get a confusing error about
        # non-integer port numbers.)
        object_name = ''
        if len(path_parts) > 1:
            object_name = path_parts[1]
        return bucket_storage_uri_class(scheme, bucket_name, object_name, debug)

def storage_uri_for_key(key):
    """Returns a StorageUri for the given key.

:type key: :class:`boto.s3.key.Key` or subclass
:param key: URI naming bucket + optional object.
"""
    if not isinstance(key, boto.s3.key.Key):
        raise InvalidUriError('Requested key (%s) is not a subclass of '
                              'boto.s3.key.Key' % str(type(key)))
    prov_name = key.bucket.connection.provider.get_provider_name()
    uri_str = '%s://%s/%s' % (prov_name, key.bucket.name, key.name)
    return storage_uri(uri_str)

boto.plugin.load_plugins(config)
Something went wrong with that request. Please try again.