def add_objects(self, objects): self._requests += 1 for object_data in objects: obj = Object(object_data.get('name', '')) obj.set_hash(object_data.get('hash', '')) obj.set_bytes(object_data.get('bytes', 0)) obj.set_content_type(object_data.get('content_type', '')) obj.set_last_modified(object_data.get('last_modified', '')) if obj.is_valid(): self._objects.appbytesend(obj)
def _parse(r): if r.OK: object_name = r.request._object.get_name() obj = Object(name=object_name) obj.set_metadata(r.metadata) d.callback((r, obj)) elif r.status_code == 401: d.errback(NotAuthenticatedException('failed to set object metadata, not authorised')) elif r.status_code == 404: d.errback(NotAuthenticatedException('failed to set object metadata, object does not exist')) else: d.errback(ResponseException('failed to set object metadata'))
def _got_session(session): print '> got session: %s' % session container_name = 'some_test_container' # creating an Object() instance here so we can specify a new Content-Type object_instance = Object('some_test_object.txt') object_instance.set_content_type('text/css') def _ok((response, v)): ''' 'response' is a transport.Response() instance. 'v' is boolean True. ''' print '> got response: %s' % response print '> changed object content type: %s/%s -> text/css' % (container_name, object_instance.get_name()) reactor.stop() print '> sending request' session.object_content_type(container_name, object_instance).addCallback(_ok).addErrback(_error)
def _parse(r): if r.OK: object_name = r.request._object.get_name() obj = Object(name=object_name) obj.set_remote_hash(r.headers.get('ETag', '')) obj.set_content_type(r.headers.get('Content-Type', '')) obj.set_remote_lenth(r.headers.get('Content-Length', 0)) obj.set_data(r.body) d.callback((r, obj)) elif r.status_code == 401: d.errback(NotAuthenticatedException('failed to delete object, not authorised')) elif r.status_code == 404: d.errback(ResponseException('failed to delete object, object does not exist')) else: d.errback(ResponseException('failed to delete object'))
def _got_session(session): print '> got session: %s' % session container_from = 'some_test_container' object_from = 'some_test_object.txt' # same container, just new object name container_to = container_from # creating an Object() instance here so we can specify a new Content-Type object_to = Object('some_test_object2.txt') object_to.set_content_type('text/css') def _ok((response, v)): ''' 'response' is a transport.Response() instance. 'v' is boolean True. ''' print '> got response: %s' % response print '> copied object: %s/%s -> %s/%s' % (container_from, object_from, container_to, object_to.get_name()) reactor.stop() print '> sending request' session.copy_object(container_from, object_from, container_to, object_to).addCallback(_ok).addErrback(_error)
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
def _got_session(session): print '> got session: %s' % session container_name = 'some_test_container' object_instance = Object('some_test_object.txt') object_instance.set_content_type('text/plain') object_instance.set_compressed() object_instance.set_data('example data in object') def _ok((response, obj)): ''' 'response' is a transport.Response() instance. 'obj' is a cfobject.Object() instance. ''' print '> got response: %s' % response print '> created object: %s/%s' % (container_name, object_instance.get_name()) print '> got object populated with response data: %s' % obj reactor.stop() print '> sending request' # create_object() also takes an optional 'delete_at' kwarg which if set to a # datetime.datetime instance in the future will reques the object be deleted # from the rackspace cloudfiles servers at that time, use timedelta's to # specify rolling timers (e.g. delete this 24hr's from now). session.create_object(container_name, object_instance).addCallback(_ok).addErrback(_error)