def parseGetBucketStorageInfo(xml):
    root = ET.fromstring(xml)
    size = root.find('.//Size')
    size = size.text if size is not None else None
    objectNumber = root.find('.//ObjectNumber')
    objectNumber = objectNumber.text if objectNumber is not None else None
    return GetBucketStorageInfoResponse(size=common_util.toLong(size), objectNumber=common_util.toInt(objectNumber))  # 返回GetBucketStorageInfoResponse的对象
def parseListParts(xml):
    root = ET.fromstring(xml)

    bucketName = root.find('.//Bucket')
    bucketName = common_util.toString(bucketName.text) if bucketName is not None else None
    objectKey = root.find('.//Key')
    objectKey = common_util.toString(objectKey.text) if objectKey is not None else None
    uploadId = root.find('.//UploadId')
    uploadId = common_util.toString(uploadId.text) if uploadId is not None else None

    storageClass = root.find('.//StorageClass')
    storageClass = common_util.toString(storageClass.text) if storageClass is not None else None
    partNumbermarker = root.find('.//PartNumberMarker')
    partNumbermarker = common_util.toInt(partNumbermarker.text) if partNumbermarker is not None else None
    nextPartNumberMarker = root.find('.//NextPartNumberMarker')
    nextPartNumberMarker = common_util.toInt(nextPartNumberMarker.text) if nextPartNumberMarker is not None else None
    maxParts = root.find('.//MaxParts')
    maxParts = common_util.toInt(maxParts) if maxParts is not None else None
    isTruncated = root.find('.//IsTruncated')
    isTruncated = common_util.toBool(isTruncated.text) if isTruncated is not None else None

    initiatorid = root.find('.//Initiator/ID')
    initiatorid = common_util.toString(initiatorid.text) if initiatorid is not None else None
    displayname = root.find('.//Initiator/DisplayName')
    displayname = common_util.toString(displayname.text) if displayname is not None else None

    initiator = Initiator(id=initiatorid, name=displayname)

    ownerid = root.find('.//Owner/ID')
    ownerid = common_util.toString(ownerid.text) if ownerid is not None else None
    ownername = root.find('.//Owner/DisplayName')
    ownername = common_util.toString(ownername.text) if ownername is not None else None

    owner = Owner(owner_id=ownerid, owner_name=ownername)

    part_list = root.findall('./Part')
    parts = []
    if part_list:
        for part in part_list:
            partnumber = part.find('./PartNumber')
            partnumber = common_util.toInt(partnumber.text) if partnumber is not None else None
            modifieddate = part.find('./LastModified')
            modifieddate = UTCToLocal(modifieddate.text) if modifieddate is not None else None
            etag = part.find('./ETag')
            etag = common_util.toString(etag.text) if etag is not None else None
            size = part.find('./Size')
            size = common_util.toLong(size.text) if size is not None else None
            __part = Part(partNumber=partnumber, lastModified=modifieddate, etag=etag, size=size)
            parts.append(__part)

    return ListPartsResponse(bucketName=bucketName, objectKey=objectKey, uploadId=uploadId, initiator=initiator, owner=owner, storageClass=storageClass,
                             partNumberMarker=partNumbermarker, nextPartNumberMarker=nextPartNumberMarker, maxParts=maxParts, isTruncated=isTruncated, parts=parts)  # 返回ListPartsResponse的对象
Exemple #3
0
 def _slice_file(self):
     uploadParts = []
     num_counts = int(self.size/self.partSize)
     if num_counts >= 10000:
         import math
         self.partSize = int(math.ceil(float(self.size) / (10000-1)))
         num_counts = int(self.size / self.partSize)
     offset = 0
     if num_counts == 0:
         uploadParts.append(Part(toLong(1), toLong(0), toLong(0), False))
     else:
         for i in range(1, num_counts+1, 1):
             part = Part(toLong(i), toLong(offset), toLong(self.partSize), False)
             offset += self.partSize
             uploadParts.append(part)
         if self.size % self.partSize > 0:
             part = Part(toLong(num_counts+1), toLong(offset), toLong(self.size % self.partSize), False)
             uploadParts.append(part)
     return uploadParts
Exemple #4
0
 def _split_object(self):
     downloadParts = []
     num_counts = int(self.size / self.partSize)
     if num_counts >= 10000:
         import math
         self.partSize = int(math.ceil(float(self.size) / (10000 - 1)))
         num_counts = int(self.size / self.partSize)
     offset = 0
     for i in range(1, num_counts + 1, 1):
         part = Part(toLong(i), toLong(offset), toLong(self.partSize), False)
         offset += self.partSize
         downloadParts.append(part)
     if self.size % self.partSize > 0:
         part = Part(toLong(num_counts + 1), toLong(offset), toLong(self.size % self.partSize), False)
         downloadParts.append(part)
     return downloadParts
Exemple #5
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 parseListObjects(xml):
    root = ET.fromstring(xml)  # 获取xml文件的根节点root

    key_entries = []
    commonprefix_entries = []

    name = root.find('Name')
    name = name.text if name is not None else None
    prefix = find_item(root, 'Prefix')
    marker = find_item(root, 'Marker')
    delimiter = find_item(root, 'Delimiter')
    max_keys = find_item(root, 'MaxKeys')
    is_truncated = find_item(root, 'IsTruncated')
    next_marker = find_item(root, 'NextMarker')

    # 获取对象key的相关信息,在Contents节点中
    contents = root.findall('Contents')
    if contents is not None:
        for node in contents:
            key = find_item(node, 'Key')
            lastmodified = find_item(node, 'LastModified')
            etag = find_item(node, 'ETag')
            size = find_item(node, 'Size')
            storage = find_item(node, 'StorageClass')

            # 获取Owner相关信息
            owner_id = find_item(node, './/ID')
            owner_name = find_item(node, './/DisplayName')
            owner = Owner(owner_id=owner_id, owner_name=owner_name)  # 创建Owner对象
            key_entry = Content(key=key, lastmodified=UTCToLocal(lastmodified), etag=etag, size=common_util.toLong(size), owner=owner, storageClass=storage)
            key_entries.append(key_entry)  # 将对象添加到对象列表中

    # 获取CommonPrefixes的相关信息
    prefixes = root.findall('CommonPrefixes')
    if prefixes is not None:
        for p in prefixes:
            pre = find_item(p, 'Prefix')
            commonprefix = CommonPrefix(prefix=pre)
            commonprefix_entries.append(commonprefix)
    # 返回ListObjectsResponse的对象
    return ListObjectsResponse(name=name, prefix=prefix, marker=marker, delimiter=delimiter, max_keys=common_util.toInt(max_keys),
                               is_truncated=common_util.toBool(is_truncated), next_marker=next_marker, contents=key_entries, commonprefixs=commonprefix_entries)
def parseListVersions(xml):

    root = ET.fromstring(xml)
    Name = root.find('./Name')
    Name =  Name.text if Name is not None else None
    Prefix = root.find('./Prefix')
    Prefix = Prefix.text if Prefix is not None else None
    KeyMarker = root.find('./KeyMarker')
    KeyMarker = KeyMarker.text if KeyMarker is not None else None
    VersionIdMarker = root.find('./VersionIdMarker')
    VersionIdMarker = VersionIdMarker.text if VersionIdMarker is not None else None
    NextKeyMarker = root.find('./NextKeyMarker')
    NextKeyMarker = NextKeyMarker.text if NextKeyMarker is not None else None
    NextVersionIdMarker = root.find('./NextVersionIdMarker')
    NextVersionIdMarker = NextVersionIdMarker.text if NextVersionIdMarker is not None else None
    MaxKeys = root.find('./MaxKeys')
    MaxKeys = MaxKeys.text if MaxKeys is not None else None
    IsTruncated = root.find('./IsTruncated')
    IsTruncated = IsTruncated.text if IsTruncated is not None else None

    head = ObjectVersionHead(name=Name, prefix=Prefix, keyMarker=KeyMarker, versionIdMarker=VersionIdMarker,
                             nextKeyMarker=NextKeyMarker, nextVersionIdMarker=NextVersionIdMarker, maxKeys=common_util.toInt(MaxKeys),
                             isTruncated=common_util.toBool(IsTruncated))

    versions = root.findall('./Version')
    version_list = []
    for version in versions:
        Key = version.find('./Key')
        Key = Key.text if Key is not None else None
        VersionId = version.find('./VersionId')
        VersionId = VersionId.text if VersionId is not None else None
        IsLatest = version.find('./IsLatest')
        IsLatest = IsLatest.text if IsLatest is not None else None
        LastModified = version.find('./LastModified')
        LastModified = LastModified.text if LastModified is not None else None
        ETag = version.find('./ETag')
        ETag = ETag.text if ETag is not None else None
        Size = version.find('./Size')
        Size = Size.text if Size is not None else None
        owner = version.find('./Owner')
        Owners = None
        if owner is not None:
            ID = version.find('.//ID')
            ID = ID.text if ID is not None else None
            DisplayName = version.find('.//DisplayName')
            DisplayName = DisplayName.text if DisplayName is not None else None
            Owners = Owner(owner_id=ID, owner_name=DisplayName)

        StorageClass = version.find('./StorageClass')
        StorageClass = StorageClass.text if StorageClass is not None else None

        Version = ObjectVersion(key=Key, versionId=VersionId, isLatest=common_util.toBool(IsLatest), lastModified=UTCToLocal(LastModified), eTag=ETag, size=common_util.toLong(Size), owner=Owners,
                                storageClass=StorageClass)
        version_list.append(Version)

    markers = root.findall('./DeleteMarker')
    marker_list = []
    for marker in markers:
        Key = marker.find('./Key')
        Key = Key.text if Key is not None else None
        VersionId = marker.find('./VersionId')
        VersionId = VersionId.text if VersionId is not None else None
        IsLatest = marker.find('./IsLatest')
        IsLatest = IsLatest.text if IsLatest is not None else None
        LastModified = marker.find('./LastModified')
        LastModified = LastModified.text if LastModified is not None else None
        owner = marker.find('./Owner')
        Owners = None
        if owner is not None:
            ID = marker.find('.//ID')
            ID = ID.text if ID is not None else None
            DisplayName = marker.find('.//DisplayName')
            DisplayName = DisplayName.text if DisplayName is not None else None
            Owners = Owner(owner_id=ID, owner_name=DisplayName)

        Marker = ObjectDeleteMarker(key=Key, versionId=VersionId, isLatest=common_util.toBool(IsLatest), lastModified=UTCToLocal(LastModified), owner=Owners)
        marker_list.append(Marker)

    prefixs = root.findall('./CommonPrefixes')
    prefix_list = []
    for prefix in prefixs:
        Prefix = prefix.find('./Prefix')
        Prefix = Prefix.text if Prefix is not None else None
        Pre = CommonPrefix(prefix=Prefix)
        prefix_list.append(Pre)
    ret = ObjectVersions(head=head, markers=marker_list, commonPrefixs=prefix_list)
    ret.versions=version_list
    return ret