Example #1
0
def list_objects(session, container=None, prefix=None, path=None, delimiter=None):
    '''
        Returns a Container() populated with objects on success.
    '''
    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')
    d = Deferred()
    def _parse(r):
        if r.OK:
            container = Container()
            container.add_objects(r.json)
            d.callback((r, 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)
    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 #2
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 #3
0
def set_temp_url_key(session, key=''):
    '''
        Returns boolean True on success.
    '''
    key = parse_str(key)
    d = Deferred()
    def _parse(r):
        if r.OK:
            d.callback((r, True))
        elif r.status_code == 401:
            d.errback(NotAuthenticatedException('failed to set temp url key, not authorised'))
        else:
            d.errback(ResponseException('failed to set the account key'))
    request = AccountSetTempURLKeyRequest(session)
    request.set_header(('X-Account-Meta-Temp-Url-Key', key))
    request.set_parser(_parse)
    request.run()
    return d
Example #4
0
 def _get_request_url(self):
     request_type = self._get_request_type()
     if request_type == Request.REQUEST_AUTH:
         return self._auth.get_endpoint().get_auth_url()
     elif request_type == Request.REQUEST_CDN:
         parts = self._session.get_cdn_url_parts()
     else:
         parts = self._session.get_storage_url_parts()
     path = parts.path
     if hasattr(self._container, 'get_name'):
         path += '/' + self._container.get_name()
         if hasattr(self._object, 'get_name'):
             path += '/' + self._object.get_name()
     qs = parts.query + self._get_query_string()
     request_url = urlunsplit((parts.scheme, parts.netloc, path, qs, parts.fragment))
     #print '-'*80
     #print request_url
     #print '-'*80
     return parse_str(request_url)
Example #5
0
def create_container(session, name='', metadata={}):
    '''
        Creates a container and returns boolean True on success.
    '''
    name = parse_str(name)
    container = Container(name=name)
    d = Deferred()
    def _parse(r):
        if r.OK:
            d.callback((r, True))
        elif r.status_code == 401:
            d.errback(NotAuthenticatedException('failed to create container, not authorised'))
        else:
            d.errback(ResponseException('failed to create container'))
    request = CreateContainerRequest(session)
    request.set_parser(_parse)
    request.set_container(container)
    for k,v in metadata.items():
        request.set_metadata((k, v), Metadata.CONTAINER)
    request.run()
    return d