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