Ejemplo n.º 1
0
def delete_object(global_id, container, object_name):
    """
    Deletes an Object

    :param global_id: user id
    :param container: container name
    :return: HTTP 201 if created
    """
    password = request.headers.get('X-Password')
    user = StorageUser.objects(global_id=global_id, global_pwd=password).first()
    if user is not None:
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id, insecure=True)
            conn.delete_object(container, object_name)
            conn.close()

            # Delete entry from Mongo
            obj = StorageObject.objects(tenant_name=global_id, container_name=container,
                                        object_name=object_name).first()
            if obj is not None:
                obj.delete()

            return HTTPResponse(status=204)
        except ClientException:
            traceback.print_exc(sys.stderr)
            abort(500, "Could not delete file")
        except AttributeError:
            traceback.print_exc(sys.stderr)
            abort(400, "Request malformed")
    else:
        abort(401, 'Unknown user')
Ejemplo n.º 2
0
def delete_object(global_id, container, object_name):
    """
    Deletes an Object

    :param global_id: user id
    :param container: container name
    :return: HTTP 201 if created
    """
    password = request.headers.get('X-Password')
    user = StorageUser.objects(global_id=global_id, global_pwd=password).first()
    if user is not None:
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id, insecure=True)
            conn.delete_object(container, object_name)
            conn.close()

            # Delete entry from Mongo
            obj = StorageObject.objects(tenant_name=global_id, container_name=container,
                                        object_name=object_name).first()
            if obj is not None:
                obj.delete()

            return HTTPResponse(status=204)
        except ClientException:
            traceback.print_exc(sys.stderr)
            abort(500, "Could not delete file")
        except AttributeError:
            traceback.print_exc(sys.stderr)
            abort(400, "Request malformed")
    else:
        abort(401, 'Unknown user')
Ejemplo n.º 3
0
def get_object(global_id, container_name, object_name):
    """
    Redirects to an object already stored in Swift
    If object missing, raise 404
    :param global_id: global user id
    :param container_name: container name
    :param object_name: file name
    :return: redirection (HTTP 303) to the actual object
    """

    #if file exists
    obj = StorageObject.objects(tenant_name=global_id, container_name=container_name,
                                object_name=object_name).first()
    if obj is not None:
        return redirect(swift_root + "%s/%s/%s" % (obj.tenant_id, container_name, object_name), 303)
    else:
        # let's try to retrieve it... if it exists
        # Before checking anything, verify that user global_id exists!
        usr = StorageUser.objects(global_id=global_id).first()
        if usr is not None and cdn_central_address is not None:
            # find origin for user from central or from the cache
            origin_object = StorageOrigin.objects(user=usr).first()
            origin_address = None
            if origin_object is not None:
                origin_address = origin_object.url
            else:
                r = get(cdn_central_address + '/origin/' + global_id)
                jresp = r.json()
                origin_address = jresp['origin_address']

            # If current instance of cdnlocal IS the origin, then file does not exist, returns a 404
            if origin_address == local_address or origin_address is None:
                return abort(404, 'File does not exist or is unavailable')

            # Cache object, but should redirect to origin this time so user does not wait for file retrieval
            t = Thread(target=cache_object, args=(origin_address, usr, container_name, object_name,))
            t.start()
            print "redirect to: %s/%s/%s/%s" % (origin_address, global_id, container_name, object_name)
            return redirect("%s/%s/%s/%s" % (origin_address, global_id, container_name, object_name), 303)
        else:
            return abort(404, 'User does not exist or Central CDN server undefined')
Ejemplo n.º 4
0
def get_object(global_id, container_name, object_name):
    """
    Redirects to an object already stored in Swift
    If object missing, raise 404
    :param global_id: global user id
    :param container_name: container name
    :param object_name: file name
    :return: redirection (HTTP 303) to the actual object
    """

    #if file exists
    obj = StorageObject.objects(tenant_name=global_id, container_name=container_name,
                                object_name=object_name).first()
    if obj is not None:
        return redirect(swift_root + "%s/%s/%s" % (obj.tenant_id, container_name, object_name), 303)
    else:
        # let's try to retrieve it... if it exists
        # Before checking anything, verify that user global_id exists!
        usr = StorageUser.objects(global_id=global_id).first()
        if usr is not None and cdn_central_address is not None:
            # find origin for user from central or from the cache
            origin_object = StorageOrigin.objects(user=usr).first()
            origin_address = None
            if origin_object is not None:
                origin_address = origin_object.url
            else:
                r = get(cdn_central_address + '/origin/' + global_id)
                jresp = r.json()
                origin_address = jresp['origin_address']

            # If current instance of cdnlocal IS the origin, then file does not exist, returns a 404
            if origin_address == local_address or origin_address is None:
                return abort(404, 'File does not exist or is unavailable')

            # Cache object, but should redirect to origin this time so user does not wait for file retrieval
            t = Thread(target=cache_object, args=(origin_address, usr, container_name, object_name))
            t.start()

            return redirect("%s/%s/%s/%s" % (origin_address, global_id, container_name, object_name), 303)
        else:
            return abort(404, 'User does not exist or Central CDN server undefined')