示例#1
0
def copy_object(session, container_from, object_from, container_to, object_to):
    '''
        COPY's an Object() from one Container() to another, perserving metadata
        and optionally changing the Content-Type.
    '''
    if type(container_from) == str or type(container_from) == unicode:
        container_from = Container(container_from)
    if not isinstance(container_from, Container):
        raise CreateRequestException('first argument must be a Container() instance or a string')
    if type(object_from) == str or type(object_from) == unicode:
        object_from = Object(object_from)
    if not isinstance(object_from, Object):
        raise CreateRequestException('second argument must be an Object()  instance or a string')
    if type(container_to) == str or type(container_to) == unicode:
        container_to = Container(container_to)
    if not isinstance(container_to, Container):
        raise CreateRequestException('third argument must be a Container() instance or a string')
    if type(object_to) == str or type(object_to) == unicode:
        object_to = Object(object_to)
    if not isinstance(object_to, Object):
        raise CreateRequestException('fourth argument must be an Object()  instance or a string')
    d = Deferred()
    def _parse(r):
        if r.OK:
            d.callback((r, True))
        elif r.status_code == 401:
            d.errback(NotAuthenticatedException('failed to set object metadata, not authorised'))
        elif r.status_code == 404:
            d.errback(ResponseException('failed to set object metadata, object does not exist'))
        else:
            d.errback(ResponseException('failed to set object metadata'))
    request = CopyObjectRequest(session)
    request.set_parser(_parse)
    request.set_container(container_from)
    request.set_object(object_from)
    request.set_header(('Destination', '%s/%s' % (container_to.get_name(), object_to.get_name())))
    if object_from._content_type != object_to._content_type:
        request.set_header(('Content-Type', object_to._content_type))
    request.run()
    return d