| OLD | NEW |
| 1 # Copyright 2010 Google Inc. | 1 # Copyright 2010 Google Inc. |
| 2 # Copyright (c) 2011, Nexenta Systems Inc. |
| 2 # | 3 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a | 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the | 5 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including | 6 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- | 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit | 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- | 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: | 10 # lowing conditions: |
| 10 # | 11 # |
| 11 # The above copyright notice and this permission notice shall be included | 12 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. | 13 # in all copies or substantial portions of the Software. |
| 13 # | 14 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. | 21 # IN THE SOFTWARE. |
| 21 | 22 |
| 22 # File representation of key, for use with "file://" URIs. | 23 # File representation of key, for use with "file://" URIs. |
| 23 | 24 |
| 24 import os, shutil, StringIO | 25 import os, shutil, StringIO |
| 26 import sys |
| 25 | 27 |
| 26 class Key(object): | 28 class Key(object): |
| 27 | 29 |
| 28 def __init__(self, bucket, name, fp=None): | 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): |
| 29 self.bucket = bucket | 36 self.bucket = bucket |
| 30 self.full_path = name | 37 self.full_path = name |
| 31 self.name = name | 38 if name == '-': |
| 32 self.fp = fp | 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 |
| 33 | 51 |
| 34 def __str__(self): | 52 def __str__(self): |
| 35 return 'file://' + self.full_path | 53 return 'file://' + self.full_path |
| 36 | 54 |
| 37 def get_file(self, fp, headers=None, cb=None, num_cb=10, torrent=False): | 55 def get_file(self, fp, headers=None, cb=None, num_cb=10, torrent=False): |
| 38 """ | 56 """ |
| 39 Retrieves a file from a Key | 57 Retrieves a file from a Key |
| 40 | 58 |
| 41 :type fp: file | 59 :type fp: file |
| 42 :param fp: File pointer to put the data into | 60 :param fp: File pointer to put the data into |
| 43 | 61 |
| 44 :type headers: string | 62 :type headers: string |
| 45 :param: ignored in this subclass. | 63 :param: ignored in this subclass. |
| 46 | 64 |
| 47 :type cb: function | 65 :type cb: function |
| 48 :param cb: ignored in this subclass. | 66 :param cb: ignored in this subclass. |
| 49 | 67 |
| 50 :type cb: int | 68 :type cb: int |
| 51 :param num_cb: ignored in this subclass. | 69 :param num_cb: ignored in this subclass. |
| 52 """ | 70 """ |
| 53 key_file = open(self.full_path, 'rb') | 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') |
| 54 shutil.copyfileobj(key_file, fp) | 77 shutil.copyfileobj(key_file, fp) |
| 55 | 78 |
| 56 def set_contents_from_file(self, fp, headers=None, replace=True, cb=None, | 79 def set_contents_from_file(self, fp, headers=None, replace=True, cb=None, |
| 57 num_cb=10, policy=None, md5=None): | 80 num_cb=10, policy=None, md5=None): |
| 58 """ | 81 """ |
| 59 Store an object in a file using the name of the Key object as the | 82 Store an object in a file using the name of the Key object as the |
| 60 key in file URI and the contents of the file pointed to by 'fp' as the | 83 key in file URI and the contents of the file pointed to by 'fp' as the |
| 61 contents. | 84 contents. |
| 62 | 85 |
| 63 :type fp: file | 86 :type fp: file |
| (...skipping 17 matching lines...) Expand all Loading... |
| 81 | 104 |
| 82 :type policy: :class:`boto.s3.acl.CannedACLStrings` | 105 :type policy: :class:`boto.s3.acl.CannedACLStrings` |
| 83 :param policy: ignored in this subclass. | 106 :param policy: ignored in this subclass. |
| 84 | 107 |
| 85 :type md5: A tuple containing the hexdigest version of the MD5 checksum | 108 :type md5: A tuple containing the hexdigest version of the MD5 checksum |
| 86 of the file as the first element and the Base64-encoded | 109 of the file as the first element and the Base64-encoded |
| 87 version of the plain checksum as the second element. | 110 version of the plain checksum as the second element. |
| 88 This is the same format returned by the compute_md5 method. | 111 This is the same format returned by the compute_md5 method. |
| 89 :param md5: ignored in this subclass. | 112 :param md5: ignored in this subclass. |
| 90 """ | 113 """ |
| 91 if not replace and os.path.exists(self.full_path): | 114 if self.key_type & self.KEY_STREAM_WRITABLE: |
| 92 return | 115 raise BotoClientError('Stream is not writable') |
| 93 key_file = open(self.full_path, 'wb') | 116 elif self.key_type & self.KEY_STREAM_READABLE: |
| 117 key_file = self.fp |
| 118 else: |
| 119 if not replace and os.path.exists(self.full_path): |
| 120 return |
| 121 key_file = open(self.full_path, 'wb') |
| 94 shutil.copyfileobj(fp, key_file) | 122 shutil.copyfileobj(fp, key_file) |
| 95 key_file.close() | 123 key_file.close() |
| 96 | 124 |
| 97 def get_contents_as_string(self, headers=None, cb=None, num_cb=10, | 125 def get_contents_as_string(self, headers=None, cb=None, num_cb=10, |
| 98 torrent=False): | 126 torrent=False): |
| 99 """ | 127 """ |
| 100 Retrieve file data from the Key, and return contents as a string. | 128 Retrieve file data from the Key, and return contents as a string. |
| 101 | 129 |
| 102 :type headers: dict | 130 :type headers: dict |
| 103 :param headers: ignored in this subclass. | 131 :param headers: ignored in this subclass. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 :type torrent: bool | 142 :type torrent: bool |
| 115 :param torrent: ignored in this subclass. | 143 :param torrent: ignored in this subclass. |
| 116 | 144 |
| 117 :rtype: string | 145 :rtype: string |
| 118 :returns: The contents of the file as a string | 146 :returns: The contents of the file as a string |
| 119 """ | 147 """ |
| 120 | 148 |
| 121 fp = StringIO.StringIO() | 149 fp = StringIO.StringIO() |
| 122 self.get_contents_to_file(fp) | 150 self.get_contents_to_file(fp) |
| 123 return fp.getvalue() | 151 return fp.getvalue() |
| 152 |
| 153 def is_stream(self): |
| 154 return (self.key_type & self.KEY_STREAM) |
| OLD | NEW |