Example #1
0
def list_all_containers(session, limit=10000):
    '''
        A slower and more elaborate version of list_containers. Performs
        sucessive recursive requests on accounts with large numbers of
        containers. Returns a single (and possibly very large) ContainerSet()
        populated with Containers() on success.
    '''
    limit = parse_int(limit)
    limit = session.CONTAINER_LIMIT if limit > session.CONTAINER_LIMIT else limit
    limit = session.CONTAINER_LIMIT if limit < 1 else limit
    d = Deferred()
    containerset = ContainerSet()
    def _parse(r):
        if r.OK:
            containerset.add_containers(r.json)
            if len(r.json) == limit:
                request = ListContainersRequest(session)
                request.set_parser(_parse)
                request.set_query_string(('limit', limit))
                request.set_query_string(('marker', containerset.get_last_container().get_name()))
                request.run()
            else:
                d.callback((r, containerset))
        elif r.status_code == 401:
            d.errback(NotAuthenticatedException('failed to get a block of containers, not authorised'))
        else:
            d.errback(ResponseException('failed to get a block of containers'))
    request = ListContainersRequest(session)
    request.set_parser(_parse)
    request.set_query_string(('limit', limit))
    request.run()
    return d
Example #2
0
def set_cdn_container_metadata(session, container=None, metadata={}):
    """
        Sets metadata on a CDN-enabled container and returns a Container()
        object on success populated with metadata.
    """
    if type(container) == str or type(container) == unicode:
        container = Container(name=container)
    if not isinstance(container, Container):
        raise CreateRequestException("first argument must be a Container() instance or a string")
    cdn = metadata.get("cdn", None)
    ttl = metadata.get("ttl", None)
    logging = metadata.get("logging", None)
    index_page = metadata.get("index_page", None)
    error_page = metadata.get("error_page", None)
    if cdn != None:
        cdn = True if cdn else False
    if ttl != None:
        ttl = parse_int(ttl)
        ttl = session.CDN_TTL_MIN if ttl < session.CDN_TTL_MIN else ttl
        ttl = session.CDN_TTL_MAX if ttl > session.CDN_TTL_MAX else ttl
    if logging != None:
        logging = True if logging else False
    d = Deferred()

    def _parse(r):
        if r.OK:
            container = Container(
                name=r.request._container.get_name(),
                object_count=0,
                bytes=0,
                cdn=r.headers.get("X-Cdn-Enabled", False),
                logging=r.headers.get("X-Log-Retention", False),
                ttl=parse_int(r.headers.get("X-Ttl", 0)),
                cdn_uri=r.headers.get("X-Cdn-Uri", ""),
                ssl_uri=r.headers.get("X-Cdn-Ssl-Uri", ""),
                stream_uri=r.headers.get("X-Cdn-Streaming-Uri", ""),
            )
            d.callback((r, container))
        elif r.status_code == 401:
            d.errback(NotAuthenticatedException("failed to set CDN-enabled container metadata, not authorised"))
        elif r.status_code == 404:
            d.errback(
                NotAuthenticatedException("failed to set CDN-enabled container metadata, container does not exist")
            )
        else:
            d.errback(ResponseException("failed to set CDN-enabled container metadata"))

    request = UpdateCDNContainerMetadataRequest(session)
    request.set_parser(_parse)
    request.set_container(container)
    if cdn != None:
        request.set_header(("X-CDN-Enabled", cdn))
    if ttl != None:
        request.set_header(("X-TTL", ttl))
    if logging != None:
        request.set_header(("X-Log-Retention", logging))
    request.run()
    return d
Example #3
0
def list_all_objects(session, container=None, limit=0, prefix=None, path=None, delimiter=None):
    '''
        A slower and more elaborate version of list_objects. Performs
        sucessive recursive requests on accounts wiRetrieveObjectRequestth large numbers of 
        objects in a single container. Returns a single (and possibly very
        large) Container() object.
    '''
    if type(container) == str or type(container) == unicode:
        container = Container(name=container)
    if not isinstance(container, Container):
        raise CreateRequestException('first argument must be a Container() instance or a string')
    limit = parse_int(limit)
    limit = session.CONTAINER_LIMIT if limit > session.CONTAINER_LIMIT else limit
    limit = session.CONTAINER_LIMIT if limit < 1 else limit
    d = Deferred()
    return_container = Container()
    def _parse(r):
        if r.OK:
            return_container.add_objects(r.json)
            if len(r.json) == limit:
                request = ListObjectsRequest(session)
                request.set_parser(_parse)
                request.set_container(container)
                request.set_query_string(('limit', limit))
                request.set_query_string(('marker', return_container.get_last_object().get_name()))
                if prefix != None:
                    request.set_query_string(('prefix', prefix))
                if path != None:
                    request.set_query_string(('path', path))
                if delimiter != None:
                    request.set_query_string(('delimiter', delimiter))
                request.run()
            d.callback((r, return_container))
        elif r.status_code == 401:
            d.errback(NotAuthenticatedException('failed to get a list of objects, not authorised'))
        elif r.status_code == 404:
            d.errback(NotAuthenticatedException('failed to get a list of objects, container does not exist'))
        else:
            d.errback(ResponseException('failed to get a list of objects'))
    request = ListObjectsRequest(session)
    request.set_parser(_parse)
    request.set_container(container)
    request.set_query_string(('limit', limit))
    if prefix != None:
        request.set_query_string(('prefix', parse_str(prefix)))
    if path != None:
        request.set_query_string(('path', parse_str(path)))
    if delimiter != None:
        request.set_query_string(('delimiter', parse_str(delimiter)[:1]))
    request.run()
    return d
Example #4
0
 def _verify_response(self, status_code, headers, binary_data, json_data):
     status_code = parse_int(status_code)
     expected_status_code = self._get_expected_response_code()
     if type(expected_status_code) == int and status_code != expected_status_code:
         return status_code, 0
     elif type(expected_status_code) == tuple and status_code not in expected_status_code:
         return status_code, 0
     for header in self._get_expected_headers():
         if header not in headers:
             return status_code, 0
     if self._get_expected_body() == self.FORMAT_BINARY and not binary_data and status_code != 204:
         return status_code, 0
     if self._get_expected_body() == self.FORMAT_JSON and type(json_data) != list and type(json_data) != dict and status_code != 204:
         return status_code, 0
     return status_code, status_code
Example #5
0
 def _parse(r):
     if r.OK:
         container = Container(
             name=r.request._container.get_name(),
             object_count=0,
             bytes=0,
             cdn=r.headers.get("X-Cdn-Enabled", False),
             logging=r.headers.get("X-Log-Retention", False),
             ttl=parse_int(r.headers.get("X-Ttl", 0)),
             cdn_uri=r.headers.get("X-Cdn-Uri", ""),
             ssl_uri=r.headers.get("X-Cdn-Ssl-Uri", ""),
             stream_uri=r.headers.get("X-Cdn-Streaming-Uri", ""),
         )
         d.callback((r, container))
     elif r.status_code == 401:
         d.errback(NotAuthenticatedException("failed to get CDN container, not authorised"))
     elif r.status_code == 404:
         d.errback(NotAuthenticatedException("failed to get CDN container, container does not exist"))
     else:
         d.errback(ResponseException("failed to get CDN container"))