def transLoggingToXml(logging):
    xml_list = []
    xml_list.append('<?xml version="1.0" encoding="UTF-8"?>')
    xml_list.append('<BucketLoggingStatus  xmlns="http://doc.s3.amazonaws.com/2006-03-01/">')
    if logging.targetBucket is not None or logging.targetPrefix is not None or (logging.targetGrants is not None and len(logging.targetGrants) > 0):
        xml_list.append('<LoggingEnabled>')
        if logging.targetBucket is not None:
            xml_backet = '<TargetBucket>' + common_util.toString(logging.targetBucket) + '</TargetBucket>'
            xml_list.append(xml_backet)
        if logging.targetPrefix is not None:
            xml_prefix = '<TargetPrefix>' + common_util.toString(logging.targetPrefix) + '</TargetPrefix>'
            xml_list.append(xml_prefix)
        if logging.targetGrants is not None and len(logging.targetGrants) > 0:
            xml_list.append('<TargetGrants>')
            for grant in logging.targetGrants:
                xml_list.append('<Grant>')
                if grant.grantee is not None:
                    xml_list.append(transGranteeToXml(grant.grantee))
                if grant.permission is not None:
                    permission = '<Permission>' + common_util.toString(grant.permission) + '</Permission>'
                    xml_list.append(permission)
                xml_list.append('</Grant>')
            xml_list.append('</TargetGrants>')
        xml_list.append('</LoggingEnabled>')

    xml_list.append('</BucketLoggingStatus>')

    return ''.join(xml_list)
def parseGetBucketCors(xml):
    root = ET.fromstring(xml)
    corsList = []
    rules = root.findall('./CORSRule')
    if rules is not None:
        for rule in rules:
            id = rule.find('./ID')
            id = common_util.toString(id.text) if id is not None else None
            maxAgeSecond = rule.find('./MaxAgeSeconds')
            maxAgeSecond = common_util.toInt(maxAgeSecond.text) if maxAgeSecond is not None else None

            method = rule.findall('./AllowedMethod')
            allowMethod = []
            if method is not None:
                for v in method:
                    allowMethod.append(common_util.toString(v.text))
            allowedOrigin = []
            method = rule.findall('./AllowedOrigin')
            if method is not None:
                for v in method:
                    allowedOrigin.append(common_util.toString(v.text))
            allowedHeader = []
            method = rule.findall('./AllowedHeader')
            if method is not None:
                for v in method:
                    allowedHeader.append(common_util.toString(v.text))
            exposeHeader = []
            method = rule.findall('./ExposeHeader')
            if method is not None:
                for v in method:
                    exposeHeader.append(common_util.toString(v.text))

            corsList.append(CorsRule(id=id, allowedMethod=allowMethod, allowedOrigin=allowedOrigin, allowedHeader=allowedHeader, maxAgeSecond=maxAgeSecond, exposeHeader=exposeHeader))
    return corsList
def transWebsiteToXml(website):
    xml_list = []
    xml_list.append('<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">')
    if website.redirectAllRequestTo is not None:
        xml_list.append(transRedirectAllRequestToXml(website.redirectAllRequestTo))
    else:
        if website.indexDocument is not None:
            temp = ''
            if website.indexDocument.suffix is not None:
                temp += '<IndexDocument>'
                temp += '<Suffix>' + common_util.toString(website.indexDocument.suffix) + '</Suffix>'
                temp += '</IndexDocument>'
            xml_list.append(temp)
        if website.errorDocument is not None:
            temp = ''
            if website.errorDocument.key is not None:
                temp = '<ErrorDocument><Key>' + common_util.toString(website.errorDocument.key) + '</Key></ErrorDocument>'
            xml_list.append(temp)
        if website.routingRules is not None and len(website.routingRules) > 0:
            xml_list.append('<RoutingRules>')
            for routingRule in website.routingRules:
                xml_list.append('<RoutingRule>')
                xml_list.append(transRoutingRuleToXml(routingRule))
                xml_list.append('</RoutingRule>')
            xml_list.append('</RoutingRules>')
    xml_list.append('</WebsiteConfiguration>')
    return ''.join(xml_list)
def transConditionToXml(condition):
    xml_list = []
    if condition.keyPrefixEquals is not None:
        xml_list.append('<KeyPrefixEquals>' + common_util.toString(condition.keyPrefixEquals) + '</KeyPrefixEquals>')
    if condition.httpErrorCodeReturnedEquals is not None:
        xml_list.append('<HttpErrorCodeReturnedEquals>' + common_util.toString(condition.httpErrorCodeReturnedEquals) + '</HttpErrorCodeReturnedEquals>')
    return '' if len(xml_list) == 0 else '<Condition>{0}</Condition>'.format(''.join(xml_list))
def parseDeleteObjects(xml):
    root = ET.fromstring(xml)
    deleted_list = []
    error_list = []
    deleteds = root.findall('./Deleted')
    if deleteds:
        for d in deleteds:
            key = d.find('./Key')
            key = common_util.toString(key.text) if key is not None else None
            deleteMarker = d.find('./DeleteMarker')
            deleteMarker = common_util.toBool(deleteMarker.text) if deleteMarker is not None else None
            deleteMarkerVersionId = d.find('./DeleteMarkerVersionId')
            deleteMarkerVersionId = common_util.toString(deleteMarkerVersionId.text) if deleteMarkerVersionId is not None else None
            deleted_list.append(DeleteObjectResult(key=key, deleteMarker=deleteMarker, deleteMarkerVersionId=deleteMarkerVersionId))
    errors = root.findall('./Error')
    if errors:
        for e in errors:
            _key = e.find('./Key')
            _key = common_util.toString(_key.text) if _key is not None else None
            _code = e.find('./Code')
            _code = common_util.toString(_code.text) if _code is not None else None
            _message = e.find('./Message')
            _message = common_util.toString(_message.text) if _message is not None else None
            error_list.append(ErrorResult(key=_key, code=_code, message=_message))
    return DeleteObjectsResponse(deleted=deleted_list, error=error_list)
def parseGetBucketNotification(xml):
    notification = Notification()
    root = ET.fromstring(xml)
    id = root.find('.//Id')
    notification.id = common_util.toString(id.text) if id is not None else None
    topic = root.find('.//Topic')
    notification.topic = common_util.toString(topic.text) if topic is not None else None
    event_list = []

    events = root.findall('.//Event')
    if events is not None:
        for event in events:
            event_list.append(common_util.toString(event.text))

    notification.events = event_list
    filterRule_list = []
    filterRules = root.findall('.//Filter/S3Key/FilterRule')
    if filterRules is not None:
        for filterRule in filterRules:
            name = filterRule.find('./Name')
            value = filterRule.find('./Value')
            fr = FilterRule(name=common_util.toString(name.text) if name is not None else None, value=common_util.toString(value.text)
                            if value is not None else None)
            filterRule_list.append(fr)
    notification.filterRules = filterRule_list
    return notification
def parseInitiateMultipartUpload(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
    return InitiateMultipartUploadResponse(bucketName=bucketName, objectKey=objectKey, uploadId=uploadId)  # 返回InitiateMultipartUploadResponse对象
def transRedirectAllRequestToXml(redirectAllRequestTo):
    xml_list = []
    if redirectAllRequestTo.hostName is not None:
        name = '<HostName>' + common_util.toString(redirectAllRequestTo.hostName) + '</HostName>'
        xml_list.append(name)
    if redirectAllRequestTo.Protocol is not None:
        prot = '<Protocol>' + common_util.toString(redirectAllRequestTo.protocol) + '</Protocol>'
        xml_list.append(prot)
    return '' if len(xml_list) == 0 else '<RedirectAllRequestsTo>{0}</RedirectAllRequestsTo>'.format(''.join(xml_list))
def parseGetBucketLoggingConfiguration(xml):
    root = ET.fromstring(xml)
    TargetBucket = root.find('.//TargetBucket')
    bucket = common_util.toString(TargetBucket.text) if TargetBucket is not None else None
    TargetPrefix = root.find('.//TargetPrefix')
    prefix = common_util.toString(TargetPrefix.text) if TargetPrefix is not None else None

    grants = root.findall('.//TargetGrants/Grant')
    return Logging(targetBucket=bucket, targetPrefix=prefix, targetGrants=parseGrants(grants))
示例#10
0
def _resumer_download(bucketName, objectKey, downloadFile, partSize, taskNum, enableCheckPoint, checkPointFile,
                      header, versionId, obsClient):
    down_operation = downloadOperation(toString(bucketName), toString(objectKey), toString(downloadFile), partSize, taskNum, enableCheckPoint, toString(checkPointFile),
                                       header, versionId, obsClient)
    if down_operation.size == 0:
        with open(down_operation.fileName, 'wb') as _:
            pass
        return down_operation._metedata_resp
    return down_operation._download()
示例#11
0
def _resumer_upload(bucketName, objectKey, uploadFile, partSize, taskNum,
                    enableCheckPoint, checkPointFile, checkSum, metadata,
                    obsClient):
    upload_operation = uploadOperation(toString(bucketName),
                                       toString(objectKey),
                                       toString(uploadFile), partSize, taskNum,
                                       enableCheckPoint,
                                       toString(checkPointFile), checkSum,
                                       metadata, obsClient)
    return upload_operation._upload()
def transCompleteMultipartUploadRequestToXml(completeMultipartUploadRequest):
    str_list = []
    if completeMultipartUploadRequest.parts is not None:
        str_list.append('<CompleteMultipartUpload xmlns="http://s3.amazonaws.com/doc/2006-03-01/">')
        for obj in completeMultipartUploadRequest.parts:
            str_list.append('<Part><PartNumber>' + common_util.toString(obj.partNum) + '</PartNumber>')
            str_list.append('<ETag>' + common_util.toString(obj.etag) + '</ETag></Part>')

        str_list.append('</CompleteMultipartUpload>')

    return ''.join(str_list)
def parseCompleteMultipartUpload(xml):
    root = ET.fromstring(xml)
    location = root.find('.//Location')
    location = common_util.toString(location.text) if location is not None else None
    bucket = root.find('.//Bucket')
    bucket = common_util.toString(bucket.text) if bucket is not None else None
    key = root.find('.//Key')
    key = common_util.toString(key.text) if key is not None else None
    eTag = root.find('.//ETag')
    eTag = common_util.toString(eTag.text) if eTag is not None else None
    return CompleteMultipartUploadResponse(location=location, bucket=bucket, key=key, eTag=eTag)  # 返回CompleteMultipartUploadResponse的对象
示例#14
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 parseGetBucketWebsiteConfiguration(xml):
    root = ET.fromstring(xml)
    redirectAll = None
    redirectAllRequestTo = root.find('./RedirectAllRequestsTo')
    if redirectAllRequestTo is not None:
        hostname = root.find('.//HostName')
        hostname = common_util.toString(hostname.text) if hostname is not None else None
        protocol = root.find('.//Protocol')
        protocol = common_util.toString(protocol.text) if protocol is not None else None
        redirectAll = RedirectAllRequestTo(hostName=hostname, protocol=protocol)

    index = None
    indexDocument = root.find('./IndexDocument')
    if indexDocument is not None:
        Suffix = root.find('.//Suffix')
        Suffix =  common_util.toString(Suffix.text) if Suffix is not None else None
        index = IndexDocument(suffix=Suffix)

    error = None
    errorDocument = root.find('./ErrorDocument')
    if errorDocument is not None:
        Key = root.find('.//Key')
        Key = common_util.toString(Key.text) if Key is not None else None
        error = ErrorDocument(key=Key)

    routs = None
    routingRules = root.findall('./RoutingRules/RoutingRule')
    if routingRules is not None and len(routingRules) > 0:
        routs = []
        for rout in routingRules:
            KeyPrefixEquals = rout.find('.//Condition/KeyPrefixEquals')
            KeyPrefixEquals = common_util.toString(KeyPrefixEquals.text) if KeyPrefixEquals is not None else None
            HttpErrorCodeReturnedEquals = rout.find(
                './/Condition/HttpErrorCodeReturnedEquals')
            HttpErrorCodeReturnedEquals = common_util.toInt(HttpErrorCodeReturnedEquals.text) if HttpErrorCodeReturnedEquals is not None else None

            condition = Condition(keyPrefixEquals=KeyPrefixEquals, httpErrorCodeReturnedEquals=HttpErrorCodeReturnedEquals)

            Protocol = rout.find('.//Redirect/Protocol')
            Protocol = common_util.toString(Protocol.text) if Protocol is not None else None
            HostName = rout.find('.//Redirect/HostName')
            HostName = common_util.toString(HostName.text) if HostName is not None else None
            ReplaceKeyPrefixWith = rout.find('.//Redirect/ReplaceKeyPrefixWith')
            ReplaceKeyPrefixWith = common_util.toString(ReplaceKeyPrefixWith.text) if ReplaceKeyPrefixWith is not None else None
            ReplaceKeyWith = rout.find('.//Redirect/ReplaceKeyWith')
            ReplaceKeyWith = common_util.toString(ReplaceKeyWith.text) if ReplaceKeyWith is not None else None
            HttpRedirectCode = rout.find('.//Redirect/HttpRedirectCode')
            HttpRedirectCode = common_util.toInt(HttpRedirectCode.text) if HttpRedirectCode is not None else None

            redirect = Redirect(protocol=Protocol, hostName=HostName, replaceKeyPrefixWith=ReplaceKeyPrefixWith, replaceKeyWith=ReplaceKeyWith,
                                httpRedirectCode=HttpRedirectCode)
            routingRule = RoutingRule(condition=condition, redirect=redirect)
            routs.append(routingRule)

    return BucketWebsite(redirectAllRequestTo=redirectAll, indexDocument=index, errorDocument=error, routingRules=routs)
def parseCopyObject(xml):
    root = ET.fromstring(xml)
    lastModified = root.find('.//LastModified')
    lastModified = UTCToLocal(lastModified.text) if lastModified is not None else None
    eTag = root.find('.//ETag')
    eTag = common_util.toString(eTag.text) if eTag is not None else None
    return CopyObjectResponse(lastModified=lastModified, eTag=eTag)  # 返回CopyObjectResponse的对象
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的对象
def transRestoreToXml(restore):
    str_list = ['<RestoreRequest xmlns="http://s3.amazonaws.com/doc/2006-03-01/" ><Days>', common_util.toString(restore.days), '</Days>']
    if restore.tier:
        str_list.append('<GlacierJobParameters><Tier>')
        str_list.append(common_util.toString(restore.tier))
        str_list.append('</Tier></GlacierJobParameters>')
    str_list.append('</RestoreRequest>')
    return ''.join(str_list)
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)
def transRedirectToXml(redirect):
    xml_list = []
    if redirect.protocol is not None:
        xml = '<Protocol>' + common_util.toString(redirect.protocol) + '</Protocol>'
        xml_list.append(xml)
    if redirect.hostName is not None:
        xml = '<HostName>' + common_util.toString(redirect.hostName) + '</HostName>'
        xml_list.append(xml)
    if redirect.replaceKeyPrefixWith is not None:
        xml = '<ReplaceKeyPrefixWith>' + common_util.toString(redirect.replaceKeyPrefixWith) + '</ReplaceKeyPrefixWith>'
        xml_list.append(xml)
    if redirect.replaceKeyWith is not None:
        xml = '<ReplaceKeyWith>' + common_util.toString(redirect.replaceKeyWith) + '</ReplaceKeyWith>'
        xml_list.append(xml)
    if redirect.httpRedirectCode is not None:
        xml = '<HttpRedirectCode>' + common_util.toString(redirect.httpRedirectCode) + '</HttpRedirectCode>'
        xml_list.append(xml)
    return '' if len(xml_list) == 0 else '<Redirect>{0}</Redirect>'.format(''.join(xml_list))
def transQuotaToXml(quota):
    str_list = []
    str_list.append('<?xml version="1.0" encoding="UTF-8"?>')
    str_list.append('<Quota xmlns="http://s3.amazonaws.com/doc/2006-03-01/">')
    str_quota = common_util.toString(quota)
    str_list.append('<StorageQuota>' + str_quota + '</StorageQuota>')
    str_list.append('</Quota>')

    return ''.join(str_list)
def transAclToXml(acl):
    if acl.owner is None:
        raise Exception('Invalid AccessControlList: missing an S3 Owner')

    str_list = []
    str_list.append('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')
    str_list.append('<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Owner><ID>')
    str_list.append(common_util.toString(acl.owner.owner_id) + '</ID><DisplayName>' + common_util.toString(acl.owner.owner_name) + '</DisplayName>')
    str_list.append('</Owner><AccessControlList>')

    for grant in acl.grants:
        grantee = grant.grantee
        permission = grant.permission
        str_list.append('<Grant>' + transGranteeToXml(grantee) + '<Permission>' + common_util.toString(permission) + '</Permission></Grant>')

    str_list.append('</AccessControlList></AccessControlPolicy>')

    return ''.join(str_list)
def UTCToLocal(strUTC):
    if strUTC is None:
        return None

    date_format = '%Y-%m-%dT%H:%M:%S.%fZ'
    CST_FORMAT = '%Y/%m/%d %H:%M:%S'

    gmt_time = time.strptime(common_util.toString(strUTC), date_format)

    cst_time = time.localtime(time.mktime(gmt_time) - time.timezone)
    dt = time.strftime(CST_FORMAT, cst_time)
    return dt
def transRuleToXml(rule):
    xml_list = []
    xml_list.append('<Rule>')
    id = '<ID>' + common_util.toString(rule.id) + '</ID>'
    xml_list.append(id)
    prefix = '<Prefix>' + common_util.toString(rule.prefix) + '</Prefix>'
    xml_list.append(prefix)
    status = '<Status>' + common_util.toString(rule.status) + '</Status>'
    xml_list.append(status)
    if rule.expiration is not None and (rule.expiration.date is not None or rule.expiration.days is not None):
        xml_list.append('<Expiration>')
        if rule.expiration.date is not None:
            date = rule.expiration.date.ToUTMidTime() if isinstance(rule.expiration.date, DateTime) else rule.expiration.date
            date = '<Date>' + common_util.toString(date) + '</Date>'
            xml_list.append(date)

        if rule.expiration.days is not None:
            days = '<Days>' + common_util.toString(rule.expiration.days) + '</Days>'
            xml_list.append(days)
        xml_list.append('</Expiration>')
    if rule.noncurrentVersionExpiration is not None and rule.noncurrentVersionExpiration.noncurrentDays is not None:
        xml_list.append('<NoncurrentVersionExpiration>')
        days = '<NoncurrentDays>' + common_util.toString(rule.noncurrentVersionExpiration.noncurrentDays) + '</NoncurrentDays>'
        xml_list.append(days)
        xml_list.append('</NoncurrentVersionExpiration>')

    xml_list.append('</Rule>')

    return ''.join(xml_list)
def transCorsRuleToXml(corsRuleList):
    xml = []
    xml.append('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')
    xml.append('<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">')
    for cors in corsRuleList:
        xml.append('<CORSRule>')
        if cors.id is not None:
            xml.append('<ID>' + common_util.toString(cors.id) + '</ID>')
        if cors.allowedMethod is not None:
            for v in cors.allowedMethod:
                xml.append('<AllowedMethod>' + common_util.toString(v) + '</AllowedMethod>')
        if cors.allowedOrigin is not None:
            for v in cors.allowedOrigin:
                xml.append('<AllowedOrigin>' + common_util.toString(v) + '</AllowedOrigin>')

        if cors.allowedHeader is not None:
            for v in cors.allowedHeader:
                xml.append('<AllowedHeader>' + common_util.toString(v) + '</AllowedHeader>')
        if cors.maxAgeSecond is not None:
            xml.append('<MaxAgeSeconds>' + common_util.toString(cors.maxAgeSecond) + '</MaxAgeSeconds>')
        if cors.exposeHeader is not None:
            for v in cors.exposeHeader:
                xml.append('<ExposeHeader>' + common_util.toString(v) + '</ExposeHeader>')
        xml.append("</CORSRule>")

    xml.append('</CORSConfiguration>')
    return ''.join(xml)
def transGranteeToXml(grantee):
    str_list = []

    if grantee.group:
        str_list.append('<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">')
        if grantee.group == Group.ALL_USERE:
            str_list.append('<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>')
        elif grantee.group == Group.AUTHENTICATED_USERS:
            str_list.append('<URI>http://acs.amazonaws.com/groups/global/AuthenticatedUsers</URI>')
        elif grantee.group == Group.LOG_DELIVERY:
            str_list.append('<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>')
        str_list.append('</Grantee>')

    if grantee.grantee_id is not None and grantee.grantee_name is not None:
        str_list.append('<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">')
        if grantee.grantee_id is not None:
            str_list.append('<ID>' + common_util.toString(grantee.grantee_id) + '</ID>')
        if grantee.grantee_name is not None:
            str_list.append('<DisplayName>' + common_util.toString(grantee.grantee_name) + '</DisplayName>')
        str_list.append('</Grantee>')

    return ''.join(str_list)
def transNotificationToXml(notification):
    xml_list = ['<?xml version="1.0" encoding="UTF-8" standalone="yes"?>']
    xml_list.append('<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">')
    if notification is not None and len(notification) > 0:
        xml_list.append('<TopicConfiguration>')
        if notification.id is not None:
            xml_list.append('<Id>{0}</Id>'.format(common_util.toString(notification.id)))
        if isinstance(notification.filterRules, list) and len(notification.filterRules)>0:
            xml_list.append('<Filter><S3Key>')
            for filterRule in notification.filterRules:
                xml_list.append('<FilterRule>')
                xml_list.append('<Name>{0}</Name>'.format(common_util.toString(filterRule.name)))
                xml_list.append('<Value>{0}</Value>'.format(common_util.toString(filterRule.value)))
                xml_list.append('</FilterRule>')
            xml_list.append('</S3Key></Filter>')

        if notification.topic is not None:
            xml_list.append('<Topic>{0}</Topic>'.format(common_util.toString(notification.topic)))
        if isinstance(notification.events,list) and len(notification.events)>0:
            for event in notification.events:
                xml_list.append('<Event>{0}</Event>'.format(common_util.toString(event)))
        xml_list.append('</TopicConfiguration>')
    xml_list.append('</NotificationConfiguration>')
    return ''.join(xml_list)
示例#28
0
 def parse_xml(cls, conn, methodName=None, connHolder=None):
     if not conn:
         return cls.getNoneResult('connection is null')
     result = None
     try:
         result = conn.getresponse()
         if not result:
             return cls.getNoneResult('response is null')
         return cls.__parse_xml(result, methodName)
     except RedirectException as re:
         raise re
     except Exception as e:
         LOG(ERROR, traceback.format_exc())
         return cls.getNoneResult(common_util.toString(e))
     finally:
         GetResult.doClose(result, conn, connHolder)
示例#29
0
 def __parse_headers(cls, headers):
     header = []
     for k, v in headers.items():
         k = common_util.toString(k).lower()
         flag = 0
         if k.startswith(common_util.METADATA_PREFIX):
             k = k[k.index(common_util.METADATA_PREFIX) +
                   len(common_util.METADATA_PREFIX):]
             flag = 1
         elif k.startswith(common_util.AMAZON_HEADER_PREFIX):
             k = k[k.index(common_util.AMAZON_HEADER_PREFIX) +
                   len(common_util.AMAZON_HEADER_PREFIX):]
             flag = 1
         elif k in common_util.ALLOWED_RESPONSE_HTTP_HEADER_METADATA_NAMES:
             flag = 1
         if flag:
             header.append((k, v))
     return header
示例#30
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)