Esempio n. 1
0
    def parse_content(cls,
                      conn,
                      objectKey,
                      downloadPath=None,
                      chuckSize=65536,
                      loadStreamInMemory=False):
        if not conn:
            return cls.getNoneResult('connection is null')
        closeConn = True
        try:
            result = conn.getresponse()
            if not result:
                return cls.getNoneResult('response is null')
            if not result.status < 300:
                return cls.__parse_xml(result)

            if loadStreamInMemory:
                LOG(DEBUG,
                    'loadStreamInMemory is True, read stream into memory')
                buffer = result.read()
                body = ObjectStream(buffer=buffer, size=len(buffer))
            elif downloadPath is None or common_util.toString(
                    downloadPath).strip() == '':
                LOG(DEBUG, 'DownloadPath is null, return conn directly')
                closeConn = False
                body = ObjectStream(response=ResponseWrapper(conn, result))
            else:
                objectKey = common_util.safe_encode(objectKey)
                downloadPath = common_util.safe_encode(downloadPath)
                file_path = cls.get_data(result, objectKey, downloadPath,
                                         chuckSize)
                body = 'DownloadPath : %s' % str(file_path)
                LOG(DEBUG, body)
            status = common_util.toInt(result.status)
            reason = result.reason
            header = cls.__parse_headers(dict(result.getheaders()))
            return GetResult(status=status,
                             reason=reason,
                             header=header,
                             body=body)
        except RedirectException as re:
            raise re
        except Exception as e:
            LOG(ERROR, traceback.format_exc())
            return cls.getNoneResult(common_util.toString(e))
        finally:
            if closeConn:
                try:
                    conn.close()
                except Exception as ex:
                    LOG(ERROR, ex)
def transTagInfoToXml(tagInfo):
    if not tagInfo.tagSet or len(tagInfo.tagSet) == 0:
        raise Exception('Invalid TagInfo, at least one tag should be specified!')
    temp = ['<?xml version="1.0" encoding="UTF-8" standalone="yes"?>',
            '<Tagging xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><TagSet>']
    for tag in tagInfo.tagSet:
        temp.append('<Tag><Key>{0}</Key><Value>{1}</Value></Tag>'.format(common_util.safe_encode(tag.key), common_util.safe_encode(tag.value)))
    temp.append('</TagSet></Tagging>')
    return ''.join(temp)
def transDeleteObjectsRequestToXml(deleteObjectsRequest):
    str_list = []
    str_list.append('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')
    str_list.append('<Delete>')
    if deleteObjectsRequest is not None:
        if deleteObjectsRequest.quiet:
            str_list.append('<Quiet>' + common_util.toString(deleteObjectsRequest.quiet).lower() + '</Quiet>')
        if deleteObjectsRequest.objects:
            for obj in deleteObjectsRequest.objects:
                if obj.key is not None:
                    str_list.append('<Object><Key>' + common_util.safe_encode(obj.key) + '</Key>')
                    if obj.versionId is not None:
                        str_list.append('<VersionId>' + common_util.toString(obj.versionId) + '</VersionId>')
                    str_list.append('</Object>')
    str_list.append('</Delete>')

    return ''.join(str_list)
Esempio n. 4
0
    def parse_content(cls,
                      conn,
                      objectKey,
                      downloadPath=None,
                      chuckSize=65536,
                      loadStreamInMemory=False,
                      connHolder=None):
        if not conn:
            return cls.getNoneResult('connection is null')
        closeConn = True
        result = None
        try:
            result = conn.getresponse()
            if not result:
                return cls.getNoneResult('response is null')

            if connHolder and hasattr(connHolder, 'createTimeStamp'):
                connHolder.createTimeStamp = time.time()

            if not common_util.toInt(result.status) < 300:
                return cls.__parse_xml(result)

            if loadStreamInMemory:
                LOG(DEBUG,
                    'loadStreamInMemory is True, read stream into memory')
                buf = None
                while True:
                    chunk = result.read(chuckSize)
                    if not chunk:
                        break
                    if buf is None:
                        buf = chunk
                    else:
                        buf += chunk
                body = ObjectStream(buffer=buf,
                                    size=common_util.toLong(len(buf)))
            elif downloadPath is None or common_util.toString(
                    downloadPath).strip() == '':
                LOG(DEBUG, 'DownloadPath is null, return conn directly')
                closeConn = False
                body = ObjectStream(
                    response=ResponseWrapper(conn, result, connHolder))
            else:
                objectKey = common_util.safe_encode(objectKey)
                downloadPath = common_util.safe_encode(downloadPath)
                file_path = cls.get_data(result, downloadPath, chuckSize)
                body = ObjectStream(url=common_util.toString(file_path))
                LOG(DEBUG,
                    'DownloadPath is ' + common_util.toString(file_path))

            status = common_util.toInt(result.status)
            reason = result.reason
            headers = dict(result.getheaders())
            header = cls.__parse_headers(headers)
            requestId = headers.get('x-amz-request-id')

            convert_util.parseGetObject(dict(header), body)
            return GetResult(status=status,
                             reason=reason,
                             header=header,
                             body=body,
                             requestId=requestId)
        except RedirectException as ex:
            raise ex
        except Exception as e:
            LOG(ERROR, traceback.format_exc())
            raise e
        finally:
            if closeConn:
                GetResult.doClose(result, conn, connHolder)
 def characters(self, content):
     if self.__currentName == 'Key':
         self.__tag['Key'] = common_util.safe_encode(content)
     elif self.__currentName == 'Value':
         self.__tag['Value'] = common_util.safe_encode(content)