| 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 key, for use with "file://" URIs. |
| 24 |
| 25 import os, shutil, StringIO |
| 26 import sys |
| 27 |
| 28 class Key(object): |
| 29 |
| 30 KEY_STREAM_READABLE = 0x01 |
| 31 KEY_STREAM_WRITABLE = 0x02 |
| 32 KEY_STREAM = (KEY_STREAM_READABLE | KEY_STREAM_WRITABLE) |
| 33 KEY_REGULAR_FILE = 0x00 |
| 34 |
| 35 def __init__(self, bucket, name, fp=None, key_type=KEY_REGULAR_FILE): |
| 36 self.bucket = bucket |
| 37 self.full_path = name |
| 38 if name == '-': |
| 39 self.name = None |
| 40 else: |
| 41 self.name = name |
| 42 self.key_type = key_type |
| 43 if key_type == self.KEY_STREAM_READABLE: |
| 44 self.fp = sys.stdin |
| 45 self.full_path = '<STDIN>' |
| 46 elif key_type == self.KEY_STREAM_WRITABLE: |
| 47 self.fp = sys.stdout |
| 48 self.full_path = '<STDOUT>' |
| 49 else: |
| 50 self.fp = fp |
| 51 |
| 52 def __str__(self): |
| 53 return 'file://' + self.full_path |
| 54 |
| 55 def get_file(self, fp, headers=None, cb=None, num_cb=10, torrent=False): |
| 56 """ |
| 57 Retrieves a file from a Key |
| 58 |
| 59 :type fp: file |
| 60 :param fp: File pointer to put the data into |
| 61 |
| 62 :type headers: string |
| 63 :param: ignored in this subclass. |
| 64 |
| 65 :type cb: function |
| 66 :param cb: ignored in this subclass. |
| 67 |
| 68 :type cb: int |
| 69 :param num_cb: ignored in this subclass. |
| 70 """ |
| 71 if self.key_type & self.KEY_STREAM_READABLE: |
| 72 raise BotoClientError('Stream is not Readable') |
| 73 elif self.key_type & self.KEY_STREAM_WRITABLE: |
| 74 key_file = self.fp |
| 75 else: |
| 76 key_file = open(self.full_path, 'rb') |
| 77 try: |
| 78 shutil.copyfileobj(key_file, fp) |
| 79 finally: |
| 80 key_file.close() |
| 81 |
| 82 def set_contents_from_file(self, fp, headers=None, replace=True, cb=None, |
| 83 num_cb=10, policy=None, md5=None): |
| 84 """ |
| 85 Store an object in a file using the name of the Key object as the |
| 86 key in file URI and the contents of the file pointed to by 'fp' as the |
| 87 contents. |
| 88 |
| 89 :type fp: file |
| 90 :param fp: the file whose contents to upload |
| 91 |
| 92 :type headers: dict |
| 93 :param headers: ignored in this subclass. |
| 94 |
| 95 :type replace: bool |
| 96 :param replace: If this parameter is False, the method |
| 97 will first check to see if an object exists in the |
| 98 bucket with the same key. If it does, it won't |
| 99 overwrite it. The default value is True which will |
| 100 overwrite the object. |
| 101 |
| 102 :type cb: function |
| 103 :param cb: ignored in this subclass. |
| 104 |
| 105 :type cb: int |
| 106 :param num_cb: ignored in this subclass. |
| 107 |
| 108 :type policy: :class:`boto.s3.acl.CannedACLStrings` |
| 109 :param policy: ignored in this subclass. |
| 110 |
| 111 :type md5: A tuple containing the hexdigest version of the MD5 checksum |
| 112 of the file as the first element and the Base64-encoded |
| 113 version of the plain checksum as the second element. |
| 114 This is the same format returned by the compute_md5 method. |
| 115 :param md5: ignored in this subclass. |
| 116 """ |
| 117 if self.key_type & self.KEY_STREAM_WRITABLE: |
| 118 raise BotoClientError('Stream is not writable') |
| 119 elif self.key_type & self.KEY_STREAM_READABLE: |
| 120 key_file = self.fp |
| 121 else: |
| 122 if not replace and os.path.exists(self.full_path): |
| 123 return |
| 124 key_file = open(self.full_path, 'wb') |
| 125 try: |
| 126 shutil.copyfileobj(fp, key_file) |
| 127 finally: |
| 128 key_file.close() |
| 129 |
| 130 def get_contents_as_string(self, headers=None, cb=None, num_cb=10, |
| 131 torrent=False): |
| 132 """ |
| 133 Retrieve file data from the Key, and return contents as a string. |
| 134 |
| 135 :type headers: dict |
| 136 :param headers: ignored in this subclass. |
| 137 |
| 138 :type cb: function |
| 139 :param cb: ignored in this subclass. |
| 140 |
| 141 :type cb: int |
| 142 :param num_cb: ignored in this subclass. |
| 143 |
| 144 :type cb: int |
| 145 :param num_cb: ignored in this subclass. |
| 146 |
| 147 :type torrent: bool |
| 148 :param torrent: ignored in this subclass. |
| 149 |
| 150 :rtype: string |
| 151 :returns: The contents of the file as a string |
| 152 """ |
| 153 |
| 154 fp = StringIO.StringIO() |
| 155 self.get_contents_to_file(fp) |
| 156 return fp.getvalue() |
| 157 |
| 158 def is_stream(self): |
| 159 return (self.key_type & self.KEY_STREAM) |
| 160 |
| 161 def close(self): |
| 162 """ |
| 163 Closes fp associated with underlying file. |
| 164 Caller should call this method when done with this class, to avoid |
| 165 using up OS resources (e.g., when iterating over a large number |
| 166 of files). |
| 167 """ |
| 168 self.fp.close() |
| OLD | NEW |