def _get_all(self, element_map, initial_query_string='', headers=None, **params): query_args = self._get_all_query_args( params, initial_query_string=initial_query_string) response = self.connection.make_request('GET', self.name, headers=headers, query_args=query_args) body = response.read() if response.status == 200: rs = ResultSet(element_map) h = handler.XmlHandler(rs, self) if not isinstance(body, bytes): body = body.encode('utf-8') try: xml.sax.parseString(body, h) except Exception as exc: print "There have exception {0}, use alternative method".format( str(exc)) doc = PyQuery(body) for content in doc('Contents').items(): new_key = Key(bucket=self) new_key.name = content('Key').text() new_key.etag = content('Etag').text() new_key.size = content('Size').text() new_key.last_modified = content('LastModified').text() new_key.storage_class = content('StorageClass').text() rs.append(new_key) return rs else: raise S3ResponseError(response.status, response.reason, body)
def _delete_key_internal(self, key_name, headers=None, version_id=None, mfa_token=None, query_args_l=None): query_args_l = query_args_l or "" provider = self.connection.provider if version_id: query_args_l.append('versionId=%s' % version_id) query_args = '&'.join(query_args_l) or None if mfa_token: if not headers: headers = {} headers[provider.mfa_header] = ' '.join(mfa_token) response = self.connection.make_request('DELETE', self.name, key_name, headers=headers, query_args=query_args) body = response.read() if response.status != 204: raise provider.storage_response_error(response.status, response.reason, body) else: # return a key object with information on what was deleted. k = Key(self) k.name = key_name k.handle_version_headers(response) k.handle_addl_headers(response.getheaders()) return k
def new_key(self, key_name=None): """ Creates a new key :type key_name: string :param key_name: The name of the key to create :rtype: :class:`boto.s3.key.Key` or subclass :returns: An instance of the newly created key object """ if not key_name: raise ValueError('Empty key names are not allowed') return Key(self, key_name)
def complete_multipart_upload(self, key_name, upload_id, xml_body, headers=None): """ Complete a multipart upload operation. """ query_args = 'uploadId=%s' % upload_id if headers is None: headers = {} headers['Content-Type'] = 'text/xml' response = self.connection.make_request('POST', self.name, key_name, query_args=query_args, headers=headers, data=xml_body) contains_error = False body = response.read().decode('utf-8') # Some errors will be reported in the body of the response # even though the HTTP response code is 200. This check # does a quick and dirty peek in the body for an error element. if body.find('<Error>') > 0: contains_error = True if response.status == 200 and not contains_error: resp = CompleteMultiPartUpload(self) h = handler.XmlHandler(resp, self) if not isinstance(body, bytes): body = body.encode('utf-8') xml.sax.parseString(body, h) # Use a dummy key to parse various response headers # for versioning, encryption info and then explicitly # set the completed MPU object values from key. k = Key(self) k.handle_version_headers(response) k.handle_encryption_headers(response) resp.version_id = k.version_id resp.encrypted = k.encrypted return resp else: raise self.connection.provider.storage_response_error( response.status, response.reason, body)
def complete_multipart_upload(self, key_name, upload_id, xml_body, headers=None): """ Complete a multipart upload operation. """ query_args = 'uploadId=%s' % upload_id if headers is None: headers = {} headers['Content-Type'] = 'text/xml' response = self.connection.make_request('POST', self.name, key_name, query_args=query_args, headers=headers, data=xml_body) contains_error = False body = response.read().decode('utf-8') # Some errors will be reported in the body of the response # even though the HTTP response code is 200. This check # does a quick and dirty peek in the body for an error element. if body.find('<Error>') > 0: contains_error = True if response.status == 200 and not contains_error: resp = CompleteMultiPartUpload(self) h = handler.XmlHandler(resp, self) if not isinstance(body, bytes): body = body.encode('utf-8') xml.sax.parseString(body, h) # Use a dummy key to parse various response headers # for versioning, encryption info and then explicitly # set the completed MPU object values from key. k = Key(self) k.handle_version_headers(response) k.handle_encryption_headers(response) resp.version_id = k.version_id resp.encrypted = k.encrypted resp.status = response.status return resp else: raise self.connection.provider.storage_response_error( response.status, response.reason, body)
def _get_all(self, element_map, initial_query_string='', headers=None, **params): query_args = self._get_all_query_args( params, initial_query_string=initial_query_string) response = self.connection.make_request('GET', self.name, headers=headers, query_args=query_args) body = response.read() if response.status == 200: rs = ResultSet(element_map) h = handler.XmlHandler(rs, self) if not isinstance(body, bytes): body = body.encode('utf-8') try: xml.sax.parseString(body, h) except: print "My Func" doc = PyQuery(body) for content in doc('Contents').items(): new_key = Key(bucket=self) new_key.name = content('Key').text() new_key.etag = content('Etag').text() new_key.size = content('Size').text() new_key.last_modified = content('LastModified').text() new_key.storage_class = content('StorageClass').text() rs.append(new_key) # k_name_list = re.findall('<Key>([\d\D]+?)</Key>', body) # for k_name in k_name_list: # kkk = Key() # kkk.name = unicode(k_name) # rs.append(kkk) return rs else: raise S3ResponseError(response.status, response.reason, body)
def _get_key_internal(self, key_name, headers, query_args_l): query_args = query_args_l or None response = self.connection.make_request('HEAD', self.name, key_name, headers=headers, query_args=query_args) response.read() # Allow any success status (2xx) - for example this lets us # support Range gets, which return status 206: if response.status / 100 == 2: k = Key(self) #provider = self.connection.provider #k.metadata = boto.utils.get_aws_metadata(response.msg, provider) for field in Key.base_fields: k.__dict__[field.lower().replace('-', '_')] = \ response.getheader(field) # the following machinations are a workaround to the fact that # apache/fastcgi omits the content-length header on HEAD # requests when the content-length is zero. # See http://goo.gl/0Tdax for more details. clen = response.getheader('content-length') if clen: k.size = int(response.getheader('content-length')) else: k.size = 0 k.name = key_name k.handle_version_headers(response) k.handle_encryption_headers(response) k.handle_restore_headers(response) k.handle_addl_headers(response.getheaders()) return k, response else: if response.status == 404: return None, response else: raise S3ResponseError(response.status, response.reason, None)
def _get_key_internal(self, key_name, headers, query_args_l): query_args = query_args_l or None response = self.connection.make_request('HEAD', self.name, key_name, headers=headers, query_args=query_args) response.read() # Allow any success status (2xx) - for example this lets us # support Range gets, which return status 206: if response.status / 100 == 2: k = Key(self) #provider = self.connection.provider #k.metadata = boto.utils.get_aws_metadata(response.msg, provider) for field in Key.base_fields: k.__dict__[field.lower().replace('-', '_')] = \ response.getheader(field) # the following machinations are a workaround to the fact that # apache/fastcgi omits the content-length header on HEAD # requests when the content-length is zero. # See http://goo.gl/0Tdax for more details. clen = response.getheader('content-length') if clen: k.size = int(response.getheader('content-length')) else: k.size = 0 k.name = key_name k.handle_version_headers(response) k.handle_encryption_headers(response) k.handle_restore_headers(response) k.handle_addl_headers(response.getheaders()) return k, response else: if response.status == 404: return None, response else: raise S3ResponseError(response.status, response.reason, response.body)