• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2006-2012 Mitch Garnaat http://garnaat.org/
2# Copyright (c) 2010-2011, Eucalyptus Systems, Inc.
3# Copyright (c) 2011, Nexenta Systems Inc.
4# Copyright (c) 2012 Amazon.com, Inc. or its affiliates.
5# Copyright (c) 2010, Google, Inc.
6# All rights reserved.
7#
8# Permission is hereby granted, free of charge, to any person obtaining a
9# copy of this software and associated documentation files (the
10# "Software"), to deal in the Software without restriction, including
11# without limitation the rights to use, copy, modify, merge, publish, dis-
12# tribute, sublicense, and/or sell copies of the Software, and to permit
13# persons to whom the Software is furnished to do so, subject to the fol-
14# lowing conditions:
15#
16# The above copyright notice and this permission notice shall be included
17# in all copies or substantial portions of the Software.
18#
19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
21# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
22# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25# IN THE SOFTWARE.
26#
27from boto.pyami.config import Config, BotoConfigLocations
28from boto.storage_uri import BucketStorageUri, FileStorageUri
29import boto.plugin
30import datetime
31import os
32import platform
33import re
34import sys
35import logging
36import logging.config
37
38from boto.compat import urlparse
39from boto.exception import InvalidUriError
40
41__version__ = '2.38.0'
42Version = __version__  # for backware compatibility
43
44# http://bugs.python.org/issue7980
45datetime.datetime.strptime('', '')
46
47UserAgent = 'Boto/%s Python/%s %s/%s' % (
48    __version__,
49    platform.python_version(),
50    platform.system(),
51    platform.release()
52)
53config = Config()
54
55# Regex to disallow buckets violating charset or not [3..255] chars total.
56BUCKET_NAME_RE = re.compile(r'^[a-zA-Z0-9][a-zA-Z0-9\._-]{1,253}[a-zA-Z0-9]$')
57# Regex to disallow buckets with individual DNS labels longer than 63.
58TOO_LONG_DNS_NAME_COMP = re.compile(r'[-_a-z0-9]{64}')
59GENERATION_RE = re.compile(r'(?P<versionless_uri_str>.+)'
60                           r'#(?P<generation>[0-9]+)$')
61VERSION_RE = re.compile('(?P<versionless_uri_str>.+)#(?P<version_id>.+)$')
62ENDPOINTS_PATH = os.path.join(os.path.dirname(__file__), 'endpoints.json')
63
64
65def init_logging():
66    for file in BotoConfigLocations:
67        try:
68            logging.config.fileConfig(os.path.expanduser(file))
69        except:
70            pass
71
72
73class NullHandler(logging.Handler):
74    def emit(self, record):
75        pass
76
77log = logging.getLogger('boto')
78perflog = logging.getLogger('boto.perf')
79log.addHandler(NullHandler())
80perflog.addHandler(NullHandler())
81init_logging()
82
83# convenience function to set logging to a particular file
84
85
86def set_file_logger(name, filepath, level=logging.INFO, format_string=None):
87    global log
88    if not format_string:
89        format_string = "%(asctime)s %(name)s [%(levelname)s]:%(message)s"
90    logger = logging.getLogger(name)
91    logger.setLevel(level)
92    fh = logging.FileHandler(filepath)
93    fh.setLevel(level)
94    formatter = logging.Formatter(format_string)
95    fh.setFormatter(formatter)
96    logger.addHandler(fh)
97    log = logger
98
99
100def set_stream_logger(name, level=logging.DEBUG, format_string=None):
101    global log
102    if not format_string:
103        format_string = "%(asctime)s %(name)s [%(levelname)s]:%(message)s"
104    logger = logging.getLogger(name)
105    logger.setLevel(level)
106    fh = logging.StreamHandler()
107    fh.setLevel(level)
108    formatter = logging.Formatter(format_string)
109    fh.setFormatter(formatter)
110    logger.addHandler(fh)
111    log = logger
112
113
114def connect_sqs(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
115    """
116    :type aws_access_key_id: string
117    :param aws_access_key_id: Your AWS Access Key ID
118
119    :type aws_secret_access_key: string
120    :param aws_secret_access_key: Your AWS Secret Access Key
121
122    :rtype: :class:`boto.sqs.connection.SQSConnection`
123    :return: A connection to Amazon's SQS
124    """
125    from boto.sqs.connection import SQSConnection
126    return SQSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
127
128
129def connect_s3(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
130    """
131    :type aws_access_key_id: string
132    :param aws_access_key_id: Your AWS Access Key ID
133
134    :type aws_secret_access_key: string
135    :param aws_secret_access_key: Your AWS Secret Access Key
136
137    :rtype: :class:`boto.s3.connection.S3Connection`
138    :return: A connection to Amazon's S3
139    """
140    from boto.s3.connection import S3Connection
141    return S3Connection(aws_access_key_id, aws_secret_access_key, **kwargs)
142
143
144def connect_gs(gs_access_key_id=None, gs_secret_access_key=None, **kwargs):
145    """
146    @type gs_access_key_id: string
147    @param gs_access_key_id: Your Google Cloud Storage Access Key ID
148
149    @type gs_secret_access_key: string
150    @param gs_secret_access_key: Your Google Cloud Storage Secret Access Key
151
152    @rtype: L{GSConnection<boto.gs.connection.GSConnection>}
153    @return: A connection to Google's Storage service
154    """
155    from boto.gs.connection import GSConnection
156    return GSConnection(gs_access_key_id, gs_secret_access_key, **kwargs)
157
158
159def connect_ec2(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
160    """
161    :type aws_access_key_id: string
162    :param aws_access_key_id: Your AWS Access Key ID
163
164    :type aws_secret_access_key: string
165    :param aws_secret_access_key: Your AWS Secret Access Key
166
167    :rtype: :class:`boto.ec2.connection.EC2Connection`
168    :return: A connection to Amazon's EC2
169    """
170    from boto.ec2.connection import EC2Connection
171    return EC2Connection(aws_access_key_id, aws_secret_access_key, **kwargs)
172
173
174def connect_elb(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
175    """
176    :type aws_access_key_id: string
177    :param aws_access_key_id: Your AWS Access Key ID
178
179    :type aws_secret_access_key: string
180    :param aws_secret_access_key: Your AWS Secret Access Key
181
182    :rtype: :class:`boto.ec2.elb.ELBConnection`
183    :return: A connection to Amazon's Load Balancing Service
184    """
185    from boto.ec2.elb import ELBConnection
186    return ELBConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
187
188
189def connect_autoscale(aws_access_key_id=None, aws_secret_access_key=None,
190                      **kwargs):
191    """
192    :type aws_access_key_id: string
193    :param aws_access_key_id: Your AWS Access Key ID
194
195    :type aws_secret_access_key: string
196    :param aws_secret_access_key: Your AWS Secret Access Key
197
198    :rtype: :class:`boto.ec2.autoscale.AutoScaleConnection`
199    :return: A connection to Amazon's Auto Scaling Service
200
201    :type use_block_device_types bool
202    :param use_block_device_types: Specifies whether to return described Launch Configs with block device mappings containing
203        block device types, or a list of old style block device mappings (deprecated).  This defaults to false for compatability
204        with the old incorrect style.
205    """
206    from boto.ec2.autoscale import AutoScaleConnection
207    return AutoScaleConnection(aws_access_key_id, aws_secret_access_key,
208                               **kwargs)
209
210
211def connect_cloudwatch(aws_access_key_id=None, aws_secret_access_key=None,
212                       **kwargs):
213    """
214    :type aws_access_key_id: string
215    :param aws_access_key_id: Your AWS Access Key ID
216
217    :type aws_secret_access_key: string
218    :param aws_secret_access_key: Your AWS Secret Access Key
219
220    :rtype: :class:`boto.ec2.cloudwatch.CloudWatchConnection`
221    :return: A connection to Amazon's EC2 Monitoring service
222    """
223    from boto.ec2.cloudwatch import CloudWatchConnection
224    return CloudWatchConnection(aws_access_key_id, aws_secret_access_key,
225                                **kwargs)
226
227
228def connect_sdb(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
229    """
230    :type aws_access_key_id: string
231    :param aws_access_key_id: Your AWS Access Key ID
232
233    :type aws_secret_access_key: string
234    :param aws_secret_access_key: Your AWS Secret Access Key
235
236    :rtype: :class:`boto.sdb.connection.SDBConnection`
237    :return: A connection to Amazon's SDB
238    """
239    from boto.sdb.connection import SDBConnection
240    return SDBConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
241
242
243def connect_fps(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
244    """
245    :type aws_access_key_id: string
246    :param aws_access_key_id: Your AWS Access Key ID
247
248    :type aws_secret_access_key: string
249    :param aws_secret_access_key: Your AWS Secret Access Key
250
251    :rtype: :class:`boto.fps.connection.FPSConnection`
252    :return: A connection to FPS
253    """
254    from boto.fps.connection import FPSConnection
255    return FPSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
256
257
258def connect_mturk(aws_access_key_id=None, aws_secret_access_key=None,
259                  **kwargs):
260    """
261    :type aws_access_key_id: string
262    :param aws_access_key_id: Your AWS Access Key ID
263
264    :type aws_secret_access_key: string
265    :param aws_secret_access_key: Your AWS Secret Access Key
266
267    :rtype: :class:`boto.mturk.connection.MTurkConnection`
268    :return: A connection to MTurk
269    """
270    from boto.mturk.connection import MTurkConnection
271    return MTurkConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
272
273
274def connect_cloudfront(aws_access_key_id=None, aws_secret_access_key=None,
275                       **kwargs):
276    """
277    :type aws_access_key_id: string
278    :param aws_access_key_id: Your AWS Access Key ID
279
280    :type aws_secret_access_key: string
281    :param aws_secret_access_key: Your AWS Secret Access Key
282
283    :rtype: :class:`boto.fps.connection.FPSConnection`
284    :return: A connection to FPS
285    """
286    from boto.cloudfront import CloudFrontConnection
287    return CloudFrontConnection(aws_access_key_id, aws_secret_access_key,
288                                **kwargs)
289
290
291def connect_vpc(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
292    """
293    :type aws_access_key_id: string
294    :param aws_access_key_id: Your AWS Access Key ID
295
296    :type aws_secret_access_key: string
297    :param aws_secret_access_key: Your AWS Secret Access Key
298
299    :rtype: :class:`boto.vpc.VPCConnection`
300    :return: A connection to VPC
301    """
302    from boto.vpc import VPCConnection
303    return VPCConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
304
305
306def connect_rds(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
307    """
308    :type aws_access_key_id: string
309    :param aws_access_key_id: Your AWS Access Key ID
310
311    :type aws_secret_access_key: string
312    :param aws_secret_access_key: Your AWS Secret Access Key
313
314    :rtype: :class:`boto.rds.RDSConnection`
315    :return: A connection to RDS
316    """
317    from boto.rds import RDSConnection
318    return RDSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
319
320
321def connect_rds2(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
322    """
323    :type aws_access_key_id: string
324    :param aws_access_key_id: Your AWS Access Key ID
325
326    :type aws_secret_access_key: string
327    :param aws_secret_access_key: Your AWS Secret Access Key
328
329    :rtype: :class:`boto.rds2.layer1.RDSConnection`
330    :return: A connection to RDS
331    """
332    from boto.rds2.layer1 import RDSConnection
333    return RDSConnection(
334        aws_access_key_id=aws_access_key_id,
335        aws_secret_access_key=aws_secret_access_key,
336        **kwargs
337    )
338
339
340def connect_emr(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
341    """
342    :type aws_access_key_id: string
343    :param aws_access_key_id: Your AWS Access Key ID
344
345    :type aws_secret_access_key: string
346    :param aws_secret_access_key: Your AWS Secret Access Key
347
348    :rtype: :class:`boto.emr.EmrConnection`
349    :return: A connection to Elastic mapreduce
350    """
351    from boto.emr import EmrConnection
352    return EmrConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
353
354
355def connect_sns(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
356    """
357    :type aws_access_key_id: string
358    :param aws_access_key_id: Your AWS Access Key ID
359
360    :type aws_secret_access_key: string
361    :param aws_secret_access_key: Your AWS Secret Access Key
362
363    :rtype: :class:`boto.sns.SNSConnection`
364    :return: A connection to Amazon's SNS
365    """
366    from boto.sns import SNSConnection
367    return SNSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
368
369
370def connect_iam(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
371    """
372    :type aws_access_key_id: string
373    :param aws_access_key_id: Your AWS Access Key ID
374
375    :type aws_secret_access_key: string
376    :param aws_secret_access_key: Your AWS Secret Access Key
377
378    :rtype: :class:`boto.iam.IAMConnection`
379    :return: A connection to Amazon's IAM
380    """
381    from boto.iam import IAMConnection
382    return IAMConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
383
384
385def connect_route53(aws_access_key_id=None, aws_secret_access_key=None,
386                    **kwargs):
387    """
388    :type aws_access_key_id: string
389    :param aws_access_key_id: Your AWS Access Key ID
390
391    :type aws_secret_access_key: string
392    :param aws_secret_access_key: Your AWS Secret Access Key
393
394    :rtype: :class:`boto.dns.Route53Connection`
395    :return: A connection to Amazon's Route53 DNS Service
396    """
397    from boto.route53 import Route53Connection
398    return Route53Connection(aws_access_key_id, aws_secret_access_key,
399                             **kwargs)
400
401
402def connect_cloudformation(aws_access_key_id=None, aws_secret_access_key=None,
403                           **kwargs):
404    """
405    :type aws_access_key_id: string
406    :param aws_access_key_id: Your AWS Access Key ID
407
408    :type aws_secret_access_key: string
409    :param aws_secret_access_key: Your AWS Secret Access Key
410
411    :rtype: :class:`boto.cloudformation.CloudFormationConnection`
412    :return: A connection to Amazon's CloudFormation Service
413    """
414    from boto.cloudformation import CloudFormationConnection
415    return CloudFormationConnection(aws_access_key_id, aws_secret_access_key,
416                                    **kwargs)
417
418
419def connect_euca(host=None, aws_access_key_id=None, aws_secret_access_key=None,
420                 port=8773, path='/services/Eucalyptus', is_secure=False,
421                 **kwargs):
422    """
423    Connect to a Eucalyptus service.
424
425    :type host: string
426    :param host: the host name or ip address of the Eucalyptus server
427
428    :type aws_access_key_id: string
429    :param aws_access_key_id: Your AWS Access Key ID
430
431    :type aws_secret_access_key: string
432    :param aws_secret_access_key: Your AWS Secret Access Key
433
434    :rtype: :class:`boto.ec2.connection.EC2Connection`
435    :return: A connection to Eucalyptus server
436    """
437    from boto.ec2 import EC2Connection
438    from boto.ec2.regioninfo import RegionInfo
439
440    # Check for values in boto config, if not supplied as args
441    if not aws_access_key_id:
442        aws_access_key_id = config.get('Credentials',
443                                       'euca_access_key_id',
444                                       None)
445    if not aws_secret_access_key:
446        aws_secret_access_key = config.get('Credentials',
447                                           'euca_secret_access_key',
448                                           None)
449    if not host:
450        host = config.get('Boto', 'eucalyptus_host', None)
451
452    reg = RegionInfo(name='eucalyptus', endpoint=host)
453    return EC2Connection(aws_access_key_id, aws_secret_access_key,
454                         region=reg, port=port, path=path,
455                         is_secure=is_secure, **kwargs)
456
457
458def connect_glacier(aws_access_key_id=None, aws_secret_access_key=None,
459                    **kwargs):
460    """
461    :type aws_access_key_id: string
462    :param aws_access_key_id: Your AWS Access Key ID
463
464    :type aws_secret_access_key: string
465    :param aws_secret_access_key: Your AWS Secret Access Key
466
467    :rtype: :class:`boto.glacier.layer2.Layer2`
468    :return: A connection to Amazon's Glacier Service
469    """
470    from boto.glacier.layer2 import Layer2
471    return Layer2(aws_access_key_id, aws_secret_access_key,
472                  **kwargs)
473
474
475def connect_ec2_endpoint(url, aws_access_key_id=None,
476                         aws_secret_access_key=None,
477                         **kwargs):
478    """
479    Connect to an EC2 Api endpoint.  Additional arguments are passed
480    through to connect_ec2.
481
482    :type url: string
483    :param url: A url for the ec2 api endpoint to connect to
484
485    :type aws_access_key_id: string
486    :param aws_access_key_id: Your AWS Access Key ID
487
488    :type aws_secret_access_key: string
489    :param aws_secret_access_key: Your AWS Secret Access Key
490
491    :rtype: :class:`boto.ec2.connection.EC2Connection`
492    :return: A connection to Eucalyptus server
493    """
494    from boto.ec2.regioninfo import RegionInfo
495
496    purl = urlparse(url)
497    kwargs['port'] = purl.port
498    kwargs['host'] = purl.hostname
499    kwargs['path'] = purl.path
500    if not 'is_secure' in kwargs:
501        kwargs['is_secure'] = (purl.scheme == "https")
502
503    kwargs['region'] = RegionInfo(name=purl.hostname,
504                                  endpoint=purl.hostname)
505    kwargs['aws_access_key_id'] = aws_access_key_id
506    kwargs['aws_secret_access_key'] = aws_secret_access_key
507
508    return(connect_ec2(**kwargs))
509
510
511def connect_walrus(host=None, aws_access_key_id=None,
512                   aws_secret_access_key=None,
513                   port=8773, path='/services/Walrus', is_secure=False,
514                   **kwargs):
515    """
516    Connect to a Walrus service.
517
518    :type host: string
519    :param host: the host name or ip address of the Walrus server
520
521    :type aws_access_key_id: string
522    :param aws_access_key_id: Your AWS Access Key ID
523
524    :type aws_secret_access_key: string
525    :param aws_secret_access_key: Your AWS Secret Access Key
526
527    :rtype: :class:`boto.s3.connection.S3Connection`
528    :return: A connection to Walrus
529    """
530    from boto.s3.connection import S3Connection
531    from boto.s3.connection import OrdinaryCallingFormat
532
533    # Check for values in boto config, if not supplied as args
534    if not aws_access_key_id:
535        aws_access_key_id = config.get('Credentials',
536                                       'euca_access_key_id',
537                                       None)
538    if not aws_secret_access_key:
539        aws_secret_access_key = config.get('Credentials',
540                                           'euca_secret_access_key',
541                                           None)
542    if not host:
543        host = config.get('Boto', 'walrus_host', None)
544
545    return S3Connection(aws_access_key_id, aws_secret_access_key,
546                        host=host, port=port, path=path,
547                        calling_format=OrdinaryCallingFormat(),
548                        is_secure=is_secure, **kwargs)
549
550
551def connect_ses(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
552    """
553    :type aws_access_key_id: string
554    :param aws_access_key_id: Your AWS Access Key ID
555
556    :type aws_secret_access_key: string
557    :param aws_secret_access_key: Your AWS Secret Access Key
558
559    :rtype: :class:`boto.ses.SESConnection`
560    :return: A connection to Amazon's SES
561    """
562    from boto.ses import SESConnection
563    return SESConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
564
565
566def connect_sts(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
567    """
568    :type aws_access_key_id: string
569    :param aws_access_key_id: Your AWS Access Key ID
570
571    :type aws_secret_access_key: string
572    :param aws_secret_access_key: Your AWS Secret Access Key
573
574    :rtype: :class:`boto.sts.STSConnection`
575    :return: A connection to Amazon's STS
576    """
577    from boto.sts import STSConnection
578    return STSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
579
580
581def connect_ia(ia_access_key_id=None, ia_secret_access_key=None,
582               is_secure=False, **kwargs):
583    """
584    Connect to the Internet Archive via their S3-like API.
585
586    :type ia_access_key_id: string
587    :param ia_access_key_id: Your IA Access Key ID.  This will also look
588        in your boto config file for an entry in the Credentials
589        section called "ia_access_key_id"
590
591    :type ia_secret_access_key: string
592    :param ia_secret_access_key: Your IA Secret Access Key.  This will also
593        look in your boto config file for an entry in the Credentials
594        section called "ia_secret_access_key"
595
596    :rtype: :class:`boto.s3.connection.S3Connection`
597    :return: A connection to the Internet Archive
598    """
599    from boto.s3.connection import S3Connection
600    from boto.s3.connection import OrdinaryCallingFormat
601
602    access_key = config.get('Credentials', 'ia_access_key_id',
603                            ia_access_key_id)
604    secret_key = config.get('Credentials', 'ia_secret_access_key',
605                            ia_secret_access_key)
606
607    return S3Connection(access_key, secret_key,
608                        host='s3.us.archive.org',
609                        calling_format=OrdinaryCallingFormat(),
610                        is_secure=is_secure, **kwargs)
611
612
613def connect_dynamodb(aws_access_key_id=None,
614                     aws_secret_access_key=None,
615                     **kwargs):
616    """
617    :type aws_access_key_id: string
618    :param aws_access_key_id: Your AWS Access Key ID
619
620    :type aws_secret_access_key: string
621    :param aws_secret_access_key: Your AWS Secret Access Key
622
623    :rtype: :class:`boto.dynamodb.layer2.Layer2`
624    :return: A connection to the Layer2 interface for DynamoDB.
625    """
626    from boto.dynamodb.layer2 import Layer2
627    return Layer2(aws_access_key_id, aws_secret_access_key, **kwargs)
628
629
630def connect_swf(aws_access_key_id=None,
631                aws_secret_access_key=None,
632                **kwargs):
633    """
634    :type aws_access_key_id: string
635    :param aws_access_key_id: Your AWS Access Key ID
636
637    :type aws_secret_access_key: string
638    :param aws_secret_access_key: Your AWS Secret Access Key
639
640    :rtype: :class:`boto.swf.layer1.Layer1`
641    :return: A connection to the Layer1 interface for SWF.
642    """
643    from boto.swf.layer1 import Layer1
644    return Layer1(aws_access_key_id, aws_secret_access_key, **kwargs)
645
646
647def connect_cloudsearch(aws_access_key_id=None,
648                        aws_secret_access_key=None,
649                        **kwargs):
650    """
651    :type aws_access_key_id: string
652    :param aws_access_key_id: Your AWS Access Key ID
653
654    :type aws_secret_access_key: string
655    :param aws_secret_access_key: Your AWS Secret Access Key
656
657    :rtype: :class:`boto.cloudsearch.layer2.Layer2`
658    :return: A connection to Amazon's CloudSearch service
659    """
660    from boto.cloudsearch.layer2 import Layer2
661    return Layer2(aws_access_key_id, aws_secret_access_key,
662                  **kwargs)
663
664
665def connect_cloudsearch2(aws_access_key_id=None,
666                         aws_secret_access_key=None,
667                         sign_request=False,
668                         **kwargs):
669    """
670    :type aws_access_key_id: string
671    :param aws_access_key_id: Your AWS Access Key ID
672
673    :type aws_secret_access_key: string
674    :param aws_secret_access_key: Your AWS Secret Access Key
675
676    :type sign_request: bool
677    :param sign_request: whether or not to sign search and
678        upload requests
679
680    :rtype: :class:`boto.cloudsearch2.layer2.Layer2`
681    :return: A connection to Amazon's CloudSearch2 service
682    """
683    from boto.cloudsearch2.layer2 import Layer2
684    return Layer2(aws_access_key_id, aws_secret_access_key,
685                  sign_request=sign_request,
686                  **kwargs)
687
688
689def connect_cloudsearchdomain(aws_access_key_id=None,
690                              aws_secret_access_key=None,
691                              **kwargs):
692    """
693    :type aws_access_key_id: string
694    :param aws_access_key_id: Your AWS Access Key ID
695
696    :type aws_secret_access_key: string
697    :param aws_secret_access_key: Your AWS Secret Access Key
698
699    :rtype: :class:`boto.cloudsearchdomain.layer1.CloudSearchDomainConnection`
700    :return: A connection to Amazon's CloudSearch Domain service
701    """
702    from boto.cloudsearchdomain.layer1 import CloudSearchDomainConnection
703    return CloudSearchDomainConnection(aws_access_key_id,
704                                       aws_secret_access_key, **kwargs)
705
706
707def connect_beanstalk(aws_access_key_id=None,
708                      aws_secret_access_key=None,
709                      **kwargs):
710    """
711    :type aws_access_key_id: string
712    :param aws_access_key_id: Your AWS Access Key ID
713
714    :type aws_secret_access_key: string
715    :param aws_secret_access_key: Your AWS Secret Access Key
716
717    :rtype: :class:`boto.beanstalk.layer1.Layer1`
718    :return: A connection to Amazon's Elastic Beanstalk service
719    """
720    from boto.beanstalk.layer1 import Layer1
721    return Layer1(aws_access_key_id, aws_secret_access_key, **kwargs)
722
723
724def connect_elastictranscoder(aws_access_key_id=None,
725                              aws_secret_access_key=None,
726                              **kwargs):
727    """
728    :type aws_access_key_id: string
729    :param aws_access_key_id: Your AWS Access Key ID
730
731    :type aws_secret_access_key: string
732    :param aws_secret_access_key: Your AWS Secret Access Key
733
734    :rtype: :class:`boto.ets.layer1.ElasticTranscoderConnection`
735    :return: A connection to Amazon's Elastic Transcoder service
736    """
737    from boto.elastictranscoder.layer1 import ElasticTranscoderConnection
738    return ElasticTranscoderConnection(
739        aws_access_key_id=aws_access_key_id,
740        aws_secret_access_key=aws_secret_access_key,
741        **kwargs)
742
743
744def connect_opsworks(aws_access_key_id=None,
745                     aws_secret_access_key=None,
746                     **kwargs):
747    from boto.opsworks.layer1 import OpsWorksConnection
748    return OpsWorksConnection(
749        aws_access_key_id=aws_access_key_id,
750        aws_secret_access_key=aws_secret_access_key,
751        **kwargs)
752
753
754def connect_redshift(aws_access_key_id=None,
755                     aws_secret_access_key=None,
756                     **kwargs):
757    """
758    :type aws_access_key_id: string
759    :param aws_access_key_id: Your AWS Access Key ID
760
761    :type aws_secret_access_key: string
762    :param aws_secret_access_key: Your AWS Secret Access Key
763
764    :rtype: :class:`boto.redshift.layer1.RedshiftConnection`
765    :return: A connection to Amazon's Redshift service
766    """
767    from boto.redshift.layer1 import RedshiftConnection
768    return RedshiftConnection(
769        aws_access_key_id=aws_access_key_id,
770        aws_secret_access_key=aws_secret_access_key,
771        **kwargs
772    )
773
774
775def connect_support(aws_access_key_id=None,
776                    aws_secret_access_key=None,
777                    **kwargs):
778    """
779    :type aws_access_key_id: string
780    :param aws_access_key_id: Your AWS Access Key ID
781
782    :type aws_secret_access_key: string
783    :param aws_secret_access_key: Your AWS Secret Access Key
784
785    :rtype: :class:`boto.support.layer1.SupportConnection`
786    :return: A connection to Amazon's Support service
787    """
788    from boto.support.layer1 import SupportConnection
789    return SupportConnection(
790        aws_access_key_id=aws_access_key_id,
791        aws_secret_access_key=aws_secret_access_key,
792        **kwargs
793    )
794
795
796def connect_cloudtrail(aws_access_key_id=None,
797                    aws_secret_access_key=None,
798                    **kwargs):
799    """
800    Connect to AWS CloudTrail
801
802    :type aws_access_key_id: string
803    :param aws_access_key_id: Your AWS Access Key ID
804
805    :type aws_secret_access_key: string
806    :param aws_secret_access_key: Your AWS Secret Access Key
807
808    :rtype: :class:`boto.cloudtrail.layer1.CloudtrailConnection`
809    :return: A connection to the AWS Cloudtrail service
810    """
811    from boto.cloudtrail.layer1 import CloudTrailConnection
812    return CloudTrailConnection(
813        aws_access_key_id=aws_access_key_id,
814        aws_secret_access_key=aws_secret_access_key,
815        **kwargs
816    )
817
818
819def connect_directconnect(aws_access_key_id=None,
820                          aws_secret_access_key=None,
821                          **kwargs):
822    """
823    Connect to AWS DirectConnect
824
825    :type aws_access_key_id: string
826    :param aws_access_key_id: Your AWS Access Key ID
827
828    :type aws_secret_access_key: string
829    :param aws_secret_access_key: Your AWS Secret Access Key
830
831    :rtype: :class:`boto.directconnect.layer1.DirectConnectConnection`
832    :return: A connection to the AWS DirectConnect service
833    """
834    from boto.directconnect.layer1 import DirectConnectConnection
835    return DirectConnectConnection(
836        aws_access_key_id=aws_access_key_id,
837        aws_secret_access_key=aws_secret_access_key,
838        **kwargs
839    )
840
841def connect_kinesis(aws_access_key_id=None,
842                    aws_secret_access_key=None,
843                    **kwargs):
844    """
845    Connect to Amazon Kinesis
846
847    :type aws_access_key_id: string
848    :param aws_access_key_id: Your AWS Access Key ID
849
850    :type aws_secret_access_key: string
851    :param aws_secret_access_key: Your AWS Secret Access Key
852
853    rtype: :class:`boto.kinesis.layer1.KinesisConnection`
854    :return: A connection to the Amazon Kinesis service
855    """
856    from boto.kinesis.layer1 import KinesisConnection
857    return KinesisConnection(
858        aws_access_key_id=aws_access_key_id,
859        aws_secret_access_key=aws_secret_access_key,
860        **kwargs
861    )
862
863def connect_logs(aws_access_key_id=None,
864                    aws_secret_access_key=None,
865                    **kwargs):
866    """
867    Connect to Amazon CloudWatch Logs
868
869    :type aws_access_key_id: string
870    :param aws_access_key_id: Your AWS Access Key ID
871
872    :type aws_secret_access_key: string
873    :param aws_secret_access_key: Your AWS Secret Access Key
874
875    rtype: :class:`boto.kinesis.layer1.CloudWatchLogsConnection`
876    :return: A connection to the Amazon CloudWatch Logs service
877    """
878    from boto.logs.layer1 import CloudWatchLogsConnection
879    return CloudWatchLogsConnection(
880        aws_access_key_id=aws_access_key_id,
881        aws_secret_access_key=aws_secret_access_key,
882        **kwargs
883    )
884
885
886def connect_route53domains(aws_access_key_id=None,
887                           aws_secret_access_key=None,
888                           **kwargs):
889    """
890    Connect to Amazon Route 53 Domains
891
892    :type aws_access_key_id: string
893    :param aws_access_key_id: Your AWS Access Key ID
894
895    :type aws_secret_access_key: string
896    :param aws_secret_access_key: Your AWS Secret Access Key
897
898    rtype: :class:`boto.route53.domains.layer1.Route53DomainsConnection`
899    :return: A connection to the Amazon Route 53 Domains service
900    """
901    from boto.route53.domains.layer1 import Route53DomainsConnection
902    return Route53DomainsConnection(
903        aws_access_key_id=aws_access_key_id,
904        aws_secret_access_key=aws_secret_access_key,
905        **kwargs
906    )
907
908
909def connect_cognito_identity(aws_access_key_id=None,
910                             aws_secret_access_key=None,
911                             **kwargs):
912    """
913    Connect to Amazon Cognito Identity
914
915    :type aws_access_key_id: string
916    :param aws_access_key_id: Your AWS Access Key ID
917
918    :type aws_secret_access_key: string
919    :param aws_secret_access_key: Your AWS Secret Access Key
920
921    rtype: :class:`boto.cognito.identity.layer1.CognitoIdentityConnection`
922    :return: A connection to the Amazon Cognito Identity service
923    """
924    from boto.cognito.identity.layer1 import CognitoIdentityConnection
925    return CognitoIdentityConnection(
926        aws_access_key_id=aws_access_key_id,
927        aws_secret_access_key=aws_secret_access_key,
928        **kwargs
929    )
930
931
932def connect_cognito_sync(aws_access_key_id=None,
933                         aws_secret_access_key=None,
934                         **kwargs):
935    """
936    Connect to Amazon Cognito Sync
937
938    :type aws_access_key_id: string
939    :param aws_access_key_id: Your AWS Access Key ID
940
941    :type aws_secret_access_key: string
942    :param aws_secret_access_key: Your AWS Secret Access Key
943
944    rtype: :class:`boto.cognito.sync.layer1.CognitoSyncConnection`
945    :return: A connection to the Amazon Cognito Sync service
946    """
947    from boto.cognito.sync.layer1 import CognitoSyncConnection
948    return CognitoSyncConnection(
949        aws_access_key_id=aws_access_key_id,
950        aws_secret_access_key=aws_secret_access_key,
951        **kwargs
952    )
953
954
955def connect_kms(aws_access_key_id=None,
956                aws_secret_access_key=None,
957                **kwargs):
958    """
959    Connect to AWS Key Management Service
960
961    :type aws_access_key_id: string
962    :param aws_access_key_id: Your AWS Access Key ID
963
964    :type aws_secret_access_key: string
965    :param aws_secret_access_key: Your AWS Secret Access Key
966
967    rtype: :class:`boto.kms.layer1.KMSConnection`
968    :return: A connection to the AWS Key Management Service
969    """
970    from boto.kms.layer1 import KMSConnection
971    return KMSConnection(
972        aws_access_key_id=aws_access_key_id,
973        aws_secret_access_key=aws_secret_access_key,
974        **kwargs
975    )
976
977
978def connect_awslambda(aws_access_key_id=None,
979                      aws_secret_access_key=None,
980                      **kwargs):
981    """
982    Connect to AWS Lambda
983
984    :type aws_access_key_id: string
985    :param aws_access_key_id: Your AWS Access Key ID
986
987    :type aws_secret_access_key: string
988    :param aws_secret_access_key: Your AWS Secret Access Key
989
990    rtype: :class:`boto.awslambda.layer1.AWSLambdaConnection`
991    :return: A connection to the AWS Lambda service
992    """
993    from boto.awslambda.layer1 import AWSLambdaConnection
994    return AWSLambdaConnection(
995        aws_access_key_id=aws_access_key_id,
996        aws_secret_access_key=aws_secret_access_key,
997        **kwargs
998    )
999
1000
1001def connect_codedeploy(aws_access_key_id=None,
1002                       aws_secret_access_key=None,
1003                       **kwargs):
1004    """
1005    Connect to AWS CodeDeploy
1006
1007    :type aws_access_key_id: string
1008    :param aws_access_key_id: Your AWS Access Key ID
1009
1010    :type aws_secret_access_key: string
1011    :param aws_secret_access_key: Your AWS Secret Access Key
1012
1013    rtype: :class:`boto.cognito.sync.layer1.CodeDeployConnection`
1014    :return: A connection to the AWS CodeDeploy service
1015    """
1016    from boto.codedeploy.layer1 import CodeDeployConnection
1017    return CodeDeployConnection(
1018        aws_access_key_id=aws_access_key_id,
1019        aws_secret_access_key=aws_secret_access_key,
1020        **kwargs
1021    )
1022
1023
1024def connect_configservice(aws_access_key_id=None,
1025                          aws_secret_access_key=None,
1026                          **kwargs):
1027    """
1028    Connect to AWS Config
1029
1030    :type aws_access_key_id: string
1031    :param aws_access_key_id: Your AWS Access Key ID
1032
1033    :type aws_secret_access_key: string
1034    :param aws_secret_access_key: Your AWS Secret Access Key
1035
1036    rtype: :class:`boto.kms.layer1.ConfigServiceConnection`
1037    :return: A connection to the AWS Config service
1038    """
1039    from boto.configservice.layer1 import ConfigServiceConnection
1040    return ConfigServiceConnection(
1041        aws_access_key_id=aws_access_key_id,
1042        aws_secret_access_key=aws_secret_access_key,
1043        **kwargs
1044    )
1045
1046
1047def connect_cloudhsm(aws_access_key_id=None,
1048                     aws_secret_access_key=None,
1049                     **kwargs):
1050    """
1051    Connect to AWS CloudHSM
1052
1053    :type aws_access_key_id: string
1054    :param aws_access_key_id: Your AWS Access Key ID
1055
1056    :type aws_secret_access_key: string
1057    :param aws_secret_access_key: Your AWS Secret Access Key
1058
1059    rtype: :class:`boto.cloudhsm.layer1.CloudHSMConnection`
1060    :return: A connection to the AWS CloudHSM service
1061    """
1062    from boto.cloudhsm.layer1 import CloudHSMConnection
1063    return CloudHSMConnection(
1064        aws_access_key_id=aws_access_key_id,
1065        aws_secret_access_key=aws_secret_access_key,
1066        **kwargs
1067    )
1068
1069
1070def connect_ec2containerservice(aws_access_key_id=None,
1071                                aws_secret_access_key=None,
1072                                **kwargs):
1073    """
1074    Connect to Amazon EC2 Container Service
1075    rtype: :class:`boto.ec2containerservice.layer1.EC2ContainerServiceConnection`
1076    :return: A connection to the Amazon EC2 Container Service
1077    """
1078    from boto.ec2containerservice.layer1 import EC2ContainerServiceConnection
1079    return EC2ContainerServiceConnection(
1080        aws_access_key_id=aws_access_key_id,
1081        aws_secret_access_key=aws_secret_access_key,
1082        **kwargs
1083    )
1084
1085
1086def connect_machinelearning(aws_access_key_id=None,
1087                            aws_secret_access_key=None,
1088                            **kwargs):
1089    """
1090    Connect to Amazon Machine Learning service
1091    rtype: :class:`boto.machinelearning.layer1.MachineLearningConnection`
1092    :return: A connection to the Amazon Machine Learning service
1093    """
1094    from boto.machinelearning.layer1 import MachineLearningConnection
1095    return MachineLearningConnection(
1096        aws_access_key_id=aws_access_key_id,
1097        aws_secret_access_key=aws_secret_access_key,
1098        **kwargs
1099    )
1100
1101
1102def storage_uri(uri_str, default_scheme='file', debug=0, validate=True,
1103                bucket_storage_uri_class=BucketStorageUri,
1104                suppress_consec_slashes=True, is_latest=False):
1105    """
1106    Instantiate a StorageUri from a URI string.
1107
1108    :type uri_str: string
1109    :param uri_str: URI naming bucket + optional object.
1110    :type default_scheme: string
1111    :param default_scheme: default scheme for scheme-less URIs.
1112    :type debug: int
1113    :param debug: debug level to pass in to boto connection (range 0..2).
1114    :type validate: bool
1115    :param validate: whether to check for bucket name validity.
1116    :type bucket_storage_uri_class: BucketStorageUri interface.
1117    :param bucket_storage_uri_class: Allows mocking for unit tests.
1118    :param suppress_consec_slashes: If provided, controls whether
1119        consecutive slashes will be suppressed in key paths.
1120    :type is_latest: bool
1121    :param is_latest: whether this versioned object represents the
1122        current version.
1123
1124    We allow validate to be disabled to allow caller
1125    to implement bucket-level wildcarding (outside the boto library;
1126    see gsutil).
1127
1128    :rtype: :class:`boto.StorageUri` subclass
1129    :return: StorageUri subclass for given URI.
1130
1131    ``uri_str`` must be one of the following formats:
1132
1133    * gs://bucket/name
1134    * gs://bucket/name#ver
1135    * s3://bucket/name
1136    * gs://bucket
1137    * s3://bucket
1138    * filename (which could be a Unix path like /a/b/c or a Windows path like
1139      C:\a\b\c)
1140
1141    The last example uses the default scheme ('file', unless overridden).
1142    """
1143    version_id = None
1144    generation = None
1145
1146    # Manually parse URI components instead of using urlparse because
1147    # what we're calling URIs don't really fit the standard syntax for URIs
1148    # (the latter includes an optional host/net location part).
1149    end_scheme_idx = uri_str.find('://')
1150    if end_scheme_idx == -1:
1151        scheme = default_scheme.lower()
1152        path = uri_str
1153    else:
1154        scheme = uri_str[0:end_scheme_idx].lower()
1155        path = uri_str[end_scheme_idx + 3:]
1156
1157    if scheme not in ['file', 's3', 'gs']:
1158        raise InvalidUriError('Unrecognized scheme "%s"' % scheme)
1159    if scheme == 'file':
1160        # For file URIs we have no bucket name, and use the complete path
1161        # (minus 'file://') as the object name.
1162        is_stream = False
1163        if path == '-':
1164            is_stream = True
1165        return FileStorageUri(path, debug, is_stream)
1166    else:
1167        path_parts = path.split('/', 1)
1168        bucket_name = path_parts[0]
1169        object_name = ''
1170        # If validate enabled, ensure the bucket name is valid, to avoid
1171        # possibly confusing other parts of the code. (For example if we didn't
1172        # catch bucket names containing ':', when a user tried to connect to
1173        # the server with that name they might get a confusing error about
1174        # non-integer port numbers.)
1175        if (validate and bucket_name and
1176            (not BUCKET_NAME_RE.match(bucket_name)
1177             or TOO_LONG_DNS_NAME_COMP.search(bucket_name))):
1178            raise InvalidUriError('Invalid bucket name in URI "%s"' % uri_str)
1179        if scheme == 'gs':
1180            match = GENERATION_RE.search(path)
1181            if match:
1182                md = match.groupdict()
1183                versionless_uri_str = md['versionless_uri_str']
1184                path_parts = versionless_uri_str.split('/', 1)
1185                generation = int(md['generation'])
1186        elif scheme == 's3':
1187            match = VERSION_RE.search(path)
1188            if match:
1189                md = match.groupdict()
1190                versionless_uri_str = md['versionless_uri_str']
1191                path_parts = versionless_uri_str.split('/', 1)
1192                version_id = md['version_id']
1193        else:
1194            raise InvalidUriError('Unrecognized scheme "%s"' % scheme)
1195        if len(path_parts) > 1:
1196            object_name = path_parts[1]
1197        return bucket_storage_uri_class(
1198            scheme, bucket_name, object_name, debug,
1199            suppress_consec_slashes=suppress_consec_slashes,
1200            version_id=version_id, generation=generation, is_latest=is_latest)
1201
1202
1203def storage_uri_for_key(key):
1204    """Returns a StorageUri for the given key.
1205
1206    :type key: :class:`boto.s3.key.Key` or subclass
1207    :param key: URI naming bucket + optional object.
1208    """
1209    if not isinstance(key, boto.s3.key.Key):
1210        raise InvalidUriError('Requested key (%s) is not a subclass of '
1211                              'boto.s3.key.Key' % str(type(key)))
1212    prov_name = key.bucket.connection.provider.get_provider_name()
1213    uri_str = '%s://%s/%s' % (prov_name, key.bucket.name, key.name)
1214    return storage_uri(uri_str)
1215
1216boto.plugin.load_plugins(config)
1217