OLD | NEW |
(Empty) | |
| 1 # Copyright 2010 Google Inc. |
| 2 # Copyright (c) 2011, Nexenta Systems Inc. |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 # File representation of bucket, for use with "file://" URIs. |
| 24 |
| 25 import os |
| 26 from key import Key |
| 27 from boto.file.simpleresultset import SimpleResultSet |
| 28 from boto.s3.bucketlistresultset import BucketListResultSet |
| 29 |
| 30 class Bucket(object): |
| 31 def __init__(self, name, contained_key): |
| 32 """Instantiate an anonymous file-based Bucket around a single key. |
| 33 """ |
| 34 self.name = name |
| 35 self.contained_key = contained_key |
| 36 |
| 37 def __iter__(self): |
| 38 return iter(BucketListResultSet(self)) |
| 39 |
| 40 def __str__(self): |
| 41 return 'anonymous bucket for file://' + self.contained_key |
| 42 |
| 43 def delete_key(self, key_name, headers=None, |
| 44 version_id=None, mfa_token=None): |
| 45 """ |
| 46 Deletes a key from the bucket. |
| 47 |
| 48 :type key_name: string |
| 49 :param key_name: The key name to delete |
| 50 |
| 51 :type version_id: string |
| 52 :param version_id: Unused in this subclass. |
| 53 |
| 54 :type mfa_token: tuple or list of strings |
| 55 :param mfa_token: Unused in this subclass. |
| 56 """ |
| 57 os.remove(key_name) |
| 58 |
| 59 def get_all_keys(self, headers=None, **params): |
| 60 """ |
| 61 This method returns the single key around which this anonymous Bucket |
| 62 was instantiated. |
| 63 |
| 64 :rtype: SimpleResultSet |
| 65 :return: The result from file system listing the keys requested |
| 66 |
| 67 """ |
| 68 key = Key(self.name, self.contained_key) |
| 69 return SimpleResultSet([key]) |
| 70 |
| 71 def get_key(self, key_name, headers=None, version_id=None, |
| 72 key_type=Key.KEY_REGULAR_FILE): |
| 73 """ |
| 74 Check to see if a particular key exists within the bucket. |
| 75 Returns: An instance of a Key object or None |
| 76 |
| 77 :type key_name: string |
| 78 :param key_name: The name of the key to retrieve |
| 79 |
| 80 :type version_id: string |
| 81 :param version_id: Unused in this subclass. |
| 82 |
| 83 :type stream_type: integer |
| 84 :param stream_type: Type of the Key - Regular File or input/output Strea
m |
| 85 |
| 86 :rtype: :class:`boto.file.key.Key` |
| 87 :returns: A Key object from this bucket. |
| 88 """ |
| 89 if key_name == '-': |
| 90 return Key(self.name, '-', key_type=Key.KEY_STREAM_READABLE) |
| 91 else: |
| 92 fp = open(key_name, 'rb') |
| 93 return Key(self.name, key_name, fp) |
| 94 |
| 95 def new_key(self, key_name=None, key_type=Key.KEY_REGULAR_FILE): |
| 96 """ |
| 97 Creates a new key |
| 98 |
| 99 :type key_name: string |
| 100 :param key_name: The name of the key to create |
| 101 |
| 102 :rtype: :class:`boto.file.key.Key` |
| 103 :returns: An instance of the newly created key object |
| 104 """ |
| 105 if key_name == '-': |
| 106 return Key(self.name, '-', key_type=Key.KEY_STREAM_WRITABLE) |
| 107 else: |
| 108 dir_name = os.path.dirname(key_name) |
| 109 if dir_name and not os.path.exists(dir_name): |
| 110 os.makedirs(dir_name) |
| 111 fp = open(key_name, 'wb') |
| 112 return Key(self.name, key_name, fp) |
OLD | NEW |