Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(774)

Side by Side Diff: third_party/gsutil/boto/__init__.py

Issue 12042069: Scripts to download files from google storage based on sha1 sums (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Removed gsutil/tests and gsutil/docs Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 # All rights reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish, dis-
11 # tribute, sublicense, and/or sell copies of the Software, and to permit
12 # persons to whom the Software is furnished to do so, subject to the fol-
13 # lowing conditions:
14 #
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
20 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # IN THE SOFTWARE.
25 #
26 from boto.pyami.config import Config, BotoConfigLocations
27 from boto.storage_uri import BucketStorageUri, FileStorageUri
28 import boto.plugin
29 import os
30 import platform
31 import re
32 import sys
33 import logging
34 import logging.config
35 import urlparse
36 from boto.exception import InvalidUriError
37
38 __version__ = '2.6.0-dev'
39 Version = __version__ # for backware compatibility
40
41 UserAgent = 'Boto/%s (%s)' % (__version__, sys.platform)
42 config = Config()
43
44
45 def init_logging():
46 for file in BotoConfigLocations:
47 try:
48 logging.config.fileConfig(os.path.expanduser(file))
49 except:
50 pass
51
52
53 class NullHandler(logging.Handler):
54 def emit(self, record):
55 pass
56
57 log = logging.getLogger('boto')
58 perflog = logging.getLogger('boto.perf')
59 log.addHandler(NullHandler())
60 perflog.addHandler(NullHandler())
61 init_logging()
62
63 # convenience function to set logging to a particular file
64
65
66 def set_file_logger(name, filepath, level=logging.INFO, format_string=None):
67 global log
68 if not format_string:
69 format_string = "%(asctime)s %(name)s [%(levelname)s]:%(message)s"
70 logger = logging.getLogger(name)
71 logger.setLevel(level)
72 fh = logging.FileHandler(filepath)
73 fh.setLevel(level)
74 formatter = logging.Formatter(format_string)
75 fh.setFormatter(formatter)
76 logger.addHandler(fh)
77 log = logger
78
79
80 def set_stream_logger(name, level=logging.DEBUG, format_string=None):
81 global log
82 if not format_string:
83 format_string = "%(asctime)s %(name)s [%(levelname)s]:%(message)s"
84 logger = logging.getLogger(name)
85 logger.setLevel(level)
86 fh = logging.StreamHandler()
87 fh.setLevel(level)
88 formatter = logging.Formatter(format_string)
89 fh.setFormatter(formatter)
90 logger.addHandler(fh)
91 log = logger
92
93
94 def connect_sqs(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
95 """
96 :type aws_access_key_id: string
97 :param aws_access_key_id: Your AWS Access Key ID
98
99 :type aws_secret_access_key: string
100 :param aws_secret_access_key: Your AWS Secret Access Key
101
102 :rtype: :class:`boto.sqs.connection.SQSConnection`
103 :return: A connection to Amazon's SQS
104 """
105 from boto.sqs.connection import SQSConnection
106 return SQSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
107
108
109 def connect_s3(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
110 """
111 :type aws_access_key_id: string
112 :param aws_access_key_id: Your AWS Access Key ID
113
114 :type aws_secret_access_key: string
115 :param aws_secret_access_key: Your AWS Secret Access Key
116
117 :rtype: :class:`boto.s3.connection.S3Connection`
118 :return: A connection to Amazon's S3
119 """
120 from boto.s3.connection import S3Connection
121 return S3Connection(aws_access_key_id, aws_secret_access_key, **kwargs)
122
123
124 def connect_gs(gs_access_key_id=None, gs_secret_access_key=None, **kwargs):
125 """
126 @type gs_access_key_id: string
127 @param gs_access_key_id: Your Google Cloud Storage Access Key ID
128
129 @type gs_secret_access_key: string
130 @param gs_secret_access_key: Your Google Cloud Storage Secret Access Key
131
132 @rtype: L{GSConnection<boto.gs.connection.GSConnection>}
133 @return: A connection to Google's Storage service
134 """
135 from boto.gs.connection import GSConnection
136 return GSConnection(gs_access_key_id, gs_secret_access_key, **kwargs)
137
138
139 def connect_ec2(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
140 """
141 :type aws_access_key_id: string
142 :param aws_access_key_id: Your AWS Access Key ID
143
144 :type aws_secret_access_key: string
145 :param aws_secret_access_key: Your AWS Secret Access Key
146
147 :rtype: :class:`boto.ec2.connection.EC2Connection`
148 :return: A connection to Amazon's EC2
149 """
150 from boto.ec2.connection import EC2Connection
151 return EC2Connection(aws_access_key_id, aws_secret_access_key, **kwargs)
152
153
154 def connect_elb(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
155 """
156 :type aws_access_key_id: string
157 :param aws_access_key_id: Your AWS Access Key ID
158
159 :type aws_secret_access_key: string
160 :param aws_secret_access_key: Your AWS Secret Access Key
161
162 :rtype: :class:`boto.ec2.elb.ELBConnection`
163 :return: A connection to Amazon's Load Balancing Service
164 """
165 from boto.ec2.elb import ELBConnection
166 return ELBConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
167
168
169 def connect_autoscale(aws_access_key_id=None, aws_secret_access_key=None,
170 **kwargs):
171 """
172 :type aws_access_key_id: string
173 :param aws_access_key_id: Your AWS Access Key ID
174
175 :type aws_secret_access_key: string
176 :param aws_secret_access_key: Your AWS Secret Access Key
177
178 :rtype: :class:`boto.ec2.autoscale.AutoScaleConnection`
179 :return: A connection to Amazon's Auto Scaling Service
180 """
181 from boto.ec2.autoscale import AutoScaleConnection
182 return AutoScaleConnection(aws_access_key_id, aws_secret_access_key,
183 **kwargs)
184
185
186 def connect_cloudwatch(aws_access_key_id=None, aws_secret_access_key=None,
187 **kwargs):
188 """
189 :type aws_access_key_id: string
190 :param aws_access_key_id: Your AWS Access Key ID
191
192 :type aws_secret_access_key: string
193 :param aws_secret_access_key: Your AWS Secret Access Key
194
195 :rtype: :class:`boto.ec2.cloudwatch.CloudWatchConnection`
196 :return: A connection to Amazon's EC2 Monitoring service
197 """
198 from boto.ec2.cloudwatch import CloudWatchConnection
199 return CloudWatchConnection(aws_access_key_id, aws_secret_access_key,
200 **kwargs)
201
202
203 def connect_sdb(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
204 """
205 :type aws_access_key_id: string
206 :param aws_access_key_id: Your AWS Access Key ID
207
208 :type aws_secret_access_key: string
209 :param aws_secret_access_key: Your AWS Secret Access Key
210
211 :rtype: :class:`boto.sdb.connection.SDBConnection`
212 :return: A connection to Amazon's SDB
213 """
214 from boto.sdb.connection import SDBConnection
215 return SDBConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
216
217
218 def connect_fps(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
219 """
220 :type aws_access_key_id: string
221 :param aws_access_key_id: Your AWS Access Key ID
222
223 :type aws_secret_access_key: string
224 :param aws_secret_access_key: Your AWS Secret Access Key
225
226 :rtype: :class:`boto.fps.connection.FPSConnection`
227 :return: A connection to FPS
228 """
229 from boto.fps.connection import FPSConnection
230 return FPSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
231
232
233 def connect_mturk(aws_access_key_id=None, aws_secret_access_key=None,
234 **kwargs):
235 """
236 :type aws_access_key_id: string
237 :param aws_access_key_id: Your AWS Access Key ID
238
239 :type aws_secret_access_key: string
240 :param aws_secret_access_key: Your AWS Secret Access Key
241
242 :rtype: :class:`boto.mturk.connection.MTurkConnection`
243 :return: A connection to MTurk
244 """
245 from boto.mturk.connection import MTurkConnection
246 return MTurkConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
247
248
249 def connect_cloudfront(aws_access_key_id=None, aws_secret_access_key=None,
250 **kwargs):
251 """
252 :type aws_access_key_id: string
253 :param aws_access_key_id: Your AWS Access Key ID
254
255 :type aws_secret_access_key: string
256 :param aws_secret_access_key: Your AWS Secret Access Key
257
258 :rtype: :class:`boto.fps.connection.FPSConnection`
259 :return: A connection to FPS
260 """
261 from boto.cloudfront import CloudFrontConnection
262 return CloudFrontConnection(aws_access_key_id, aws_secret_access_key,
263 **kwargs)
264
265
266 def connect_vpc(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
267 """
268 :type aws_access_key_id: string
269 :param aws_access_key_id: Your AWS Access Key ID
270
271 :type aws_secret_access_key: string
272 :param aws_secret_access_key: Your AWS Secret Access Key
273
274 :rtype: :class:`boto.vpc.VPCConnection`
275 :return: A connection to VPC
276 """
277 from boto.vpc import VPCConnection
278 return VPCConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
279
280
281 def connect_rds(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
282 """
283 :type aws_access_key_id: string
284 :param aws_access_key_id: Your AWS Access Key ID
285
286 :type aws_secret_access_key: string
287 :param aws_secret_access_key: Your AWS Secret Access Key
288
289 :rtype: :class:`boto.rds.RDSConnection`
290 :return: A connection to RDS
291 """
292 from boto.rds import RDSConnection
293 return RDSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
294
295
296 def connect_emr(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
297 """
298 :type aws_access_key_id: string
299 :param aws_access_key_id: Your AWS Access Key ID
300
301 :type aws_secret_access_key: string
302 :param aws_secret_access_key: Your AWS Secret Access Key
303
304 :rtype: :class:`boto.emr.EmrConnection`
305 :return: A connection to Elastic mapreduce
306 """
307 from boto.emr import EmrConnection
308 return EmrConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
309
310
311 def connect_sns(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
312 """
313 :type aws_access_key_id: string
314 :param aws_access_key_id: Your AWS Access Key ID
315
316 :type aws_secret_access_key: string
317 :param aws_secret_access_key: Your AWS Secret Access Key
318
319 :rtype: :class:`boto.sns.SNSConnection`
320 :return: A connection to Amazon's SNS
321 """
322 from boto.sns import SNSConnection
323 return SNSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
324
325
326 def connect_iam(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
327 """
328 :type aws_access_key_id: string
329 :param aws_access_key_id: Your AWS Access Key ID
330
331 :type aws_secret_access_key: string
332 :param aws_secret_access_key: Your AWS Secret Access Key
333
334 :rtype: :class:`boto.iam.IAMConnection`
335 :return: A connection to Amazon's IAM
336 """
337 from boto.iam import IAMConnection
338 return IAMConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
339
340
341 def connect_route53(aws_access_key_id=None, aws_secret_access_key=None,
342 **kwargs):
343 """
344 :type aws_access_key_id: string
345 :param aws_access_key_id: Your AWS Access Key ID
346
347 :type aws_secret_access_key: string
348 :param aws_secret_access_key: Your AWS Secret Access Key
349
350 :rtype: :class:`boto.dns.Route53Connection`
351 :return: A connection to Amazon's Route53 DNS Service
352 """
353 from boto.route53 import Route53Connection
354 return Route53Connection(aws_access_key_id, aws_secret_access_key,
355 **kwargs)
356
357
358 def connect_cloudformation(aws_access_key_id=None, aws_secret_access_key=None,
359 **kwargs):
360 """
361 :type aws_access_key_id: string
362 :param aws_access_key_id: Your AWS Access Key ID
363
364 :type aws_secret_access_key: string
365 :param aws_secret_access_key: Your AWS Secret Access Key
366
367 :rtype: :class:`boto.cloudformation.CloudFormationConnection`
368 :return: A connection to Amazon's CloudFormation Service
369 """
370 from boto.cloudformation import CloudFormationConnection
371 return CloudFormationConnection(aws_access_key_id, aws_secret_access_key,
372 **kwargs)
373
374
375 def connect_euca(host=None, aws_access_key_id=None, aws_secret_access_key=None,
376 port=8773, path='/services/Eucalyptus', is_secure=False,
377 **kwargs):
378 """
379 Connect to a Eucalyptus service.
380
381 :type host: string
382 :param host: the host name or ip address of the Eucalyptus server
383
384 :type aws_access_key_id: string
385 :param aws_access_key_id: Your AWS Access Key ID
386
387 :type aws_secret_access_key: string
388 :param aws_secret_access_key: Your AWS Secret Access Key
389
390 :rtype: :class:`boto.ec2.connection.EC2Connection`
391 :return: A connection to Eucalyptus server
392 """
393 from boto.ec2 import EC2Connection
394 from boto.ec2.regioninfo import RegionInfo
395
396 # Check for values in boto config, if not supplied as args
397 if not aws_access_key_id:
398 aws_access_key_id = config.get('Credentials',
399 'euca_access_key_id',
400 None)
401 if not aws_secret_access_key:
402 aws_secret_access_key = config.get('Credentials',
403 'euca_secret_access_key',
404 None)
405 if not host:
406 host = config.get('Boto', 'eucalyptus_host', None)
407
408 reg = RegionInfo(name='eucalyptus', endpoint=host)
409 return EC2Connection(aws_access_key_id, aws_secret_access_key,
410 region=reg, port=port, path=path,
411 is_secure=is_secure, **kwargs)
412
413
414 def connect_glacier(aws_access_key_id=None, aws_secret_access_key=None,
415 **kwargs):
416 """
417 :type aws_access_key_id: string
418 :param aws_access_key_id: Your AWS Access Key ID
419
420 :type aws_secret_access_key: string
421 :param aws_secret_access_key: Your AWS Secret Access Key
422
423 :rtype: :class:`boto.glacier.layer2.Layer2`
424 :return: A connection to Amazon's Glacier Service
425 """
426 from boto.glacier.layer2 import Layer2
427 return Layer2(aws_access_key_id, aws_secret_access_key,
428 **kwargs)
429
430
431 def connect_ec2_endpoint(url, aws_access_key_id=None,
432 aws_secret_access_key=None,
433 **kwargs):
434 """
435 Connect to an EC2 Api endpoint. Additional arguments are passed
436 through to connect_ec2.
437
438 :type url: string
439 :param url: A url for the ec2 api endpoint to connect to
440
441 :type aws_access_key_id: string
442 :param aws_access_key_id: Your AWS Access Key ID
443
444 :type aws_secret_access_key: string
445 :param aws_secret_access_key: Your AWS Secret Access Key
446
447 :rtype: :class:`boto.ec2.connection.EC2Connection`
448 :return: A connection to Eucalyptus server
449 """
450 from boto.ec2.regioninfo import RegionInfo
451
452 purl = urlparse.urlparse(url)
453 kwargs['port'] = purl.port
454 kwargs['host'] = purl.hostname
455 kwargs['path'] = purl.path
456 if not 'is_secure' in kwargs:
457 kwargs['is_secure'] = (purl.scheme == "https")
458
459 kwargs['region'] = RegionInfo(name=purl.hostname,
460 endpoint=purl.hostname)
461 kwargs['aws_access_key_id'] = aws_access_key_id
462 kwargs['aws_secret_access_key'] = aws_secret_access_key
463
464 return(connect_ec2(**kwargs))
465
466
467 def connect_walrus(host=None, aws_access_key_id=None,
468 aws_secret_access_key=None,
469 port=8773, path='/services/Walrus', is_secure=False,
470 **kwargs):
471 """
472 Connect to a Walrus service.
473
474 :type host: string
475 :param host: the host name or ip address of the Walrus server
476
477 :type aws_access_key_id: string
478 :param aws_access_key_id: Your AWS Access Key ID
479
480 :type aws_secret_access_key: string
481 :param aws_secret_access_key: Your AWS Secret Access Key
482
483 :rtype: :class:`boto.s3.connection.S3Connection`
484 :return: A connection to Walrus
485 """
486 from boto.s3.connection import S3Connection
487 from boto.s3.connection import OrdinaryCallingFormat
488
489 # Check for values in boto config, if not supplied as args
490 if not aws_access_key_id:
491 aws_access_key_id = config.get('Credentials',
492 'euca_access_key_id',
493 None)
494 if not aws_secret_access_key:
495 aws_secret_access_key = config.get('Credentials',
496 'euca_secret_access_key',
497 None)
498 if not host:
499 host = config.get('Boto', 'walrus_host', None)
500
501 return S3Connection(aws_access_key_id, aws_secret_access_key,
502 host=host, port=port, path=path,
503 calling_format=OrdinaryCallingFormat(),
504 is_secure=is_secure, **kwargs)
505
506
507 def connect_ses(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
508 """
509 :type aws_access_key_id: string
510 :param aws_access_key_id: Your AWS Access Key ID
511
512 :type aws_secret_access_key: string
513 :param aws_secret_access_key: Your AWS Secret Access Key
514
515 :rtype: :class:`boto.ses.SESConnection`
516 :return: A connection to Amazon's SES
517 """
518 from boto.ses import SESConnection
519 return SESConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
520
521
522 def connect_sts(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
523 """
524 :type aws_access_key_id: string
525 :param aws_access_key_id: Your AWS Access Key ID
526
527 :type aws_secret_access_key: string
528 :param aws_secret_access_key: Your AWS Secret Access Key
529
530 :rtype: :class:`boto.sts.STSConnection`
531 :return: A connection to Amazon's STS
532 """
533 from boto.sts import STSConnection
534 return STSConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
535
536
537 def connect_ia(ia_access_key_id=None, ia_secret_access_key=None,
538 is_secure=False, **kwargs):
539 """
540 Connect to the Internet Archive via their S3-like API.
541
542 :type ia_access_key_id: string
543 :param ia_access_key_id: Your IA Access Key ID. This will also look
544 in your boto config file for an entry in the Credentials
545 section called "ia_access_key_id"
546
547 :type ia_secret_access_key: string
548 :param ia_secret_access_key: Your IA Secret Access Key. This will also
549 look in your boto config file for an entry in the Credentials
550 section called "ia_secret_access_key"
551
552 :rtype: :class:`boto.s3.connection.S3Connection`
553 :return: A connection to the Internet Archive
554 """
555 from boto.s3.connection import S3Connection
556 from boto.s3.connection import OrdinaryCallingFormat
557
558 access_key = config.get('Credentials', 'ia_access_key_id',
559 ia_access_key_id)
560 secret_key = config.get('Credentials', 'ia_secret_access_key',
561 ia_secret_access_key)
562
563 return S3Connection(access_key, secret_key,
564 host='s3.us.archive.org',
565 calling_format=OrdinaryCallingFormat(),
566 is_secure=is_secure, **kwargs)
567
568
569 def connect_dynamodb(aws_access_key_id=None,
570 aws_secret_access_key=None,
571 **kwargs):
572 """
573 :type aws_access_key_id: string
574 :param aws_access_key_id: Your AWS Access Key ID
575
576 :type aws_secret_access_key: string
577 :param aws_secret_access_key: Your AWS Secret Access Key
578
579 :rtype: :class:`boto.dynamodb.layer2.Layer2`
580 :return: A connection to the Layer2 interface for DynamoDB.
581 """
582 from boto.dynamodb.layer2 import Layer2
583 return Layer2(aws_access_key_id, aws_secret_access_key, **kwargs)
584
585
586 def connect_swf(aws_access_key_id=None,
587 aws_secret_access_key=None,
588 **kwargs):
589 """
590 :type aws_access_key_id: string
591 :param aws_access_key_id: Your AWS Access Key ID
592
593 :type aws_secret_access_key: string
594 :param aws_secret_access_key: Your AWS Secret Access Key
595
596 :rtype: :class:`boto.swf.layer1.Layer1`
597 :return: A connection to the Layer1 interface for SWF.
598 """
599 from boto.swf.layer1 import Layer1
600 return Layer1(aws_access_key_id, aws_secret_access_key, **kwargs)
601
602
603 def connect_cloudsearch(aws_access_key_id=None,
604 aws_secret_access_key=None,
605 **kwargs):
606 """
607 :type aws_access_key_id: string
608 :param aws_access_key_id: Your AWS Access Key ID
609
610 :type aws_secret_access_key: string
611 :param aws_secret_access_key: Your AWS Secret Access Key
612
613 :rtype: :class:`boto.ec2.autoscale.CloudSearchConnection`
614 :return: A connection to Amazon's CloudSearch service
615 """
616 from boto.cloudsearch.layer2 import Layer2
617 return Layer2(aws_access_key_id, aws_secret_access_key,
618 **kwargs)
619
620
621 def connect_beanstalk(aws_access_key_id=None,
622 aws_secret_access_key=None,
623 **kwargs):
624 """
625 :type aws_access_key_id: string
626 :param aws_access_key_id: Your AWS Access Key ID
627
628 :type aws_secret_access_key: string
629 :param aws_secret_access_key: Your AWS Secret Access Key
630
631 :rtype: :class:`boto.beanstalk.layer1.Layer1`
632 :return: A connection to Amazon's Elastic Beanstalk service
633 """
634 from boto.beanstalk.layer1 import Layer1
635 return Layer1(aws_access_key_id, aws_secret_access_key, **kwargs)
636
637
638 def storage_uri(uri_str, default_scheme='file', debug=0, validate=True,
639 bucket_storage_uri_class=BucketStorageUri,
640 suppress_consec_slashes=True):
641 """
642 Instantiate a StorageUri from a URI string.
643
644 :type uri_str: string
645 :param uri_str: URI naming bucket + optional object.
646 :type default_scheme: string
647 :param default_scheme: default scheme for scheme-less URIs.
648 :type debug: int
649 :param debug: debug level to pass in to boto connection (range 0..2).
650 :type validate: bool
651 :param validate: whether to check for bucket name validity.
652 :type bucket_storage_uri_class: BucketStorageUri interface.
653 :param bucket_storage_uri_class: Allows mocking for unit tests.
654 :param suppress_consec_slashes: If provided, controls whether
655 consecutive slashes will be suppressed in key paths.
656
657 We allow validate to be disabled to allow caller
658 to implement bucket-level wildcarding (outside the boto library;
659 see gsutil).
660
661 :rtype: :class:`boto.StorageUri` subclass
662 :return: StorageUri subclass for given URI.
663
664 ``uri_str`` must be one of the following formats:
665
666 * gs://bucket/name
667 * s3://bucket/name
668 * gs://bucket
669 * s3://bucket
670 * filename (which could be a Unix path like /a/b/c or a Windows path like
671 C:\a\b\c)
672
673 The last example uses the default scheme ('file', unless overridden)
674 """
675
676 # Manually parse URI components instead of using urlparse.urlparse because
677 # what we're calling URIs don't really fit the standard syntax for URIs
678 # (the latter includes an optional host/net location part).
679 end_scheme_idx = uri_str.find('://')
680 if end_scheme_idx == -1:
681 # Check for common error: user specifies gs:bucket instead
682 # of gs://bucket. Some URI parsers allow this, but it can cause
683 # confusion for callers, so we don't.
684 colon_pos = uri_str.find(':')
685 if colon_pos != -1:
686 # Allow Windows path names including drive letter (C: etc.)
687 drive_char = uri_str[0].lower()
688 if not (platform.system().lower().startswith('windows')
689 and colon_pos == 1
690 and drive_char >= 'a' and drive_char <= 'z'):
691 raise InvalidUriError('"%s" contains ":" instead of "://"' % uri_s tr)
692 scheme = default_scheme.lower()
693 path = uri_str
694 else:
695 scheme = uri_str[0:end_scheme_idx].lower()
696 path = uri_str[end_scheme_idx + 3:]
697
698 if scheme not in ['file', 's3', 'gs']:
699 raise InvalidUriError('Unrecognized scheme "%s"' % scheme)
700 if scheme == 'file':
701 # For file URIs we have no bucket name, and use the complete path
702 # (minus 'file://') as the object name.
703 is_stream = False
704 if path == '-':
705 is_stream = True
706 return FileStorageUri(path, debug, is_stream)
707 else:
708 path_parts = path.split('/', 1)
709 bucket_name = path_parts[0]
710 if (validate and bucket_name and
711 # Disallow buckets violating charset or not [3..255] chars total.
712 (not re.match('^[a-z0-9][a-z0-9\._-]{1,253}[a-z0-9]$', bucket_name)
713 # Disallow buckets with individual DNS labels longer than 63.
714 or re.search('[-_a-z0-9]{64}', bucket_name))):
715 raise InvalidUriError('Invalid bucket name in URI "%s"' % uri_str)
716 # If enabled, ensure the bucket name is valid, to avoid possibly
717 # confusing other parts of the code. (For example if we didn't
718 # catch bucket names containing ':', when a user tried to connect to
719 # the server with that name they might get a confusing error about
720 # non-integer port numbers.)
721 object_name = ''
722 if len(path_parts) > 1:
723 object_name = path_parts[1]
724 return bucket_storage_uri_class(
725 scheme, bucket_name, object_name, debug,
726 suppress_consec_slashes=suppress_consec_slashes)
727
728
729 def storage_uri_for_key(key):
730 """Returns a StorageUri for the given key.
731
732 :type key: :class:`boto.s3.key.Key` or subclass
733 :param key: URI naming bucket + optional object.
734 """
735 if not isinstance(key, boto.s3.key.Key):
736 raise InvalidUriError('Requested key (%s) is not a subclass of '
737 'boto.s3.key.Key' % str(type(key)))
738 prov_name = key.bucket.connection.provider.get_provider_name()
739 uri_str = '%s://%s/%s' % (prov_name, key.bucket.name, key.name)
740 return storage_uri(uri_str)
741
742 boto.plugin.load_plugins(config)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698