Exemple #1
0
from kamaki.clients.astakos import AstakosClient
from kamaki.clients.pithos import PithosClient

AUTHENTICATION_URL = "https://astakos.example.com/identity/v2.0"
TOKEN = "User-Token"
astakos = AstakosClient(AUTHENTICATION_URL, TOKEN)

service_type = PithosClient.service_type
endpoint = astakos.get_endpoint_url(service_type)
pithos = PithosClient(endpoint, TOKEN)

user = astakos.authenticate()
uuid = user["access"]["user"]["id"]
pithos.account = uuid

#  Get the project containers we care for
containers = filter(
    lambda c: c["name"] in ("pithos", "images"),
    pithos.list_containers())

#  Construct dict of the form {CONTAINER_NAME: PROJECT_ID, ...}
projects = dict([(
    c["name"],
    c["x_container_policy"]["project"]) for c in containers])

#  Check projects and reassign if needed
if projects["pithos"] != projects["images"]:
    pithos.container = "images"
    pithos.reassign_container(projects["pithos"])
Exemple #2
0
#  initliaze astakos client
AUTHENTICATION_URL = "https://astakos.example.com/identity/v2.0"
TOKEN = "User-Token"
astakos = AstakosClient(AUTHENTICATION_URL, TOKEN)

#  Initliaze pithos
service_type = PithosClient.service_type
endpoint = astakos.get_endpoint_url(service_type)
pithos = PithosClient(endpoint, TOKEN)

#  Set user UUID and Container (optional)
user = astakos.authenticate()
uuid = user["access"]["user"]["id"]

pithos.account = uuid

containers = pithos.list_containers()
for container in containers:
    pithos.container = container["name"]
    project = container["x_container_policy"]["project"]

    print "Listing contents of {container} (project: {project})".format(
        container=pithos.container, project=project)
    for object_ in pithos.list_objects():
        name = object_["name"]
        size = object_["bytes"]
        type_ = object_["content_type"]
        print "\t{name} \t{type_} \t{size} bytes".format(
            name=name, size=size, type_=type_)
Exemple #3
0
uuid = user["access"]["user"]["id"]

service_type = PithosClient.service_type
endpoint = astakos.get_endpoint_url(service_type)
pithos = PithosClient(endpoint, TOKEN)
pithos.account = uuid

service_type = ImageClient.service_type
endpoint = astakos.get_endpoint_url(service_type)
image = ImageClient(endpoint, TOKEN)

#  Find the image by id
image_id = "my-image-id"
my_image = image.get_meta(image_id)

#  Check if it is my image
if my_image["owner"] == uuid:
    image.unregister(image_id)

    #  Delete the image files
    pithos.container = "images"
    separator = "{uuid}/{container}/".format(uuid=uuid,
                                             container=pithos.container)
    _, location = my_image["location"].split(separator)
    meta_object = "{0}.meta".format(location)

    pithos.del_object(location)
    pithos.del_object(meta_object)
else:
    print "This image wasn't registered by me"
Exemple #4
0
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from kamaki.clients.astakos import AstakosClient
from kamaki.clients.pithos import PithosClient

#  Initliaze astakos client
AUTHENTICATION_URL = "https://astakos.example.com/identity/v2.0"
TOKEN = "User-Token"
astakos = AstakosClient(AUTHENTICATION_URL, TOKEN)

#  Initliaze pithos
service_type = PithosClient.service_type
endpoint = astakos.get_endpoint_url(service_type)
pithos = PithosClient(endpoint, TOKEN)

#  Set user UUID and Container (optional)
user = astakos.authenticate()
uuid = user["access"]["user"]["id"]

pithos.account = uuid
pithos.container = "pithos"
def main():
    #authenticate to Astakos
    print"---------------------------------------------------"
    print"******* Authenticating to Astakos******************"
    print"---------------------------------------------------"
    try:
        my_astakos_client = AstakosClient(AUTHENTICATION_URL,
                                          TOKEN)
        my_accountData = my_astakos_client.authenticate()
        ACCOUNT_UUID = my_accountData['access']['user']['id']
        print "Status: Authenticated"
    except ClientError:
        print"Failed to authenticate user token"

    print"\n"
    print"---------------------------------------------------"
    print"**********Getting Endpoints for pithos*************"
    print"---------------------------------------------------"
    #get endpoint url
    try:
        endpoints = my_astakos_client.get_service_endpoints('object-store')
        PITHOS_URL = endpoints['publicURL']
        print "The public URL:", PITHOS_URL
    except ClientError:
        print "Failed to get endpoints for pithos"

    print"\n"
    print"---------------------------------------------------"
    print"**********Authenticating to Pithos*****************"
    print"---------------------------------------------------"
    #Initialize pithos client
    try:
        pithos = PithosClient(PITHOS_URL, TOKEN)
        pithos.account = ACCOUNT_UUID
        pithos.container = ''
    except ClientError:
        print "Failed to initialize Pithos+ client"

    print"\n"
    print"---------------------------------------------------"
    print"**********LIST ALL CONTAINERS IN YOUR ACCOUNT******"
    print"---------------------------------------------------"
    #list all containers
    try:
        container_list = pithos.list_containers()
        containers = parse_containers(container_list)
        ContNums = len(containers)
        print "The number of Containers in your account:", ContNums
        print "The containers are"
        print ','.join(containers)
    except ClientError:
        print"Error in container list"

    print"\n"
    print"---------------------------------------------------"
    print"******LIST OBJECTS OF A FOLDER IN A CONTAINER******"
    print"---------------------------------------------------"
    #list all containers
    try:
        print_container_objects(pithos, YOUR_CONTAINER,
                              prefixName=YOUR_FOLDER_PATH)
    except ClientError:
        print"Error in listing folder objects"

    print"\n"
    print"---------------------------------------------------"
    print"**********Print objects for all containers*********"
    print"---------------------------------------------------"

    try:
        for i in range(len(containers)):
            print_container_objects(pithos, containers[i])
    except ClientError as e:
        print('Error: %s' % e)
        if e.status:
            print('- error code: %s' % e.status)
        if e.details:
            for detail in e.details:
                print('- %s' % detail)

    #  Create and set a different container than pithos
    print "Create a new container - my container"
    CONTAINER = 'my container'
    pithos.create_container(CONTAINER)
    pithos.container = CONTAINER

    print"\n"
    print"---------------------------------------------------"
    print"**********UPLOAD AND DOWNLOAD**********************"
    print"---------------------------------------------------"
    """
    B. UPLOAD AND DOWNLOAD
    """
    print "Upload a small file to pithos"
    #  Upload a small file
    print './test/'+SMALLFILE
    with open('./test/'+SMALLFILE) as f:
        pithos.upload_object(SMALLFILE, f)

    print "Download a small file from pithos and store to string"
    print SMALLFILE
    FILETOSTRING = pithos.download_to_string(SMALLFILE,
                                             download_cb=
                                             create_pb('Downloading...'))
    print "Small file string:", FILETOSTRING
    #To optimize for large files, allow pithos client
    # to use multiple threads! pithos client will
    # auto-adjust the number of threads, up to a limit
    pithos.MAX_THREADS = 5
    print "Upload a large file to pithos"
    #  Now, large file upload will be optimized:
    #  dd if=/dev/zero of=test/large.txt count=8 bs=1073741824
    with open('./test/'+BIGFILE) as f:
        pithos.upload_object(BIGFILE, f,
                             hash_cb=create_pb('Calculating hashes...'),
                             upload_cb=create_pb('Uploading...'))

    print "Create my own metadata for object"
    tags = {}
    tags['mytag'] = 12
    pithos.set_object_meta(BIGFILE, tags)
    myOwnMetadataObject = pithos.get_object_meta(BIGFILE)
    print "Object Metatadata", myOwnMetadataObject
    print "Download a large file from pithos"
    #  Download a file (btw, MAX_THREADS are still 5)
    with open('./test/'+TMPFILE, 'wb+') as f:
        pithos.download_object(BIGFILE, f,
                               download_cb=create_pb('Downloading...'))

    #  HIGHLIGHTS: If parts of the file are already uploaded or downloaded,
    #  corresponding methods will transfer only the missing parts!
    print"\n"
    print"---------------------------------------------------"
    print"**********CREATE A NEW CONTAINER AND MOVE OBJECT***"
    print"---------------------------------------------------"

    """ Create a new container and move object"""
    print "Create a new container - containerToCopy"
    CONTAINERNEW = 'containertocopy'
    pithos.create_container(CONTAINERNEW)
    pithos.move_object(CONTAINER, SMALLFILE, CONTAINERNEW, SMALLFILE)
    print"\n"
    print"---------------------------------------------------"
    print"**********DELETE AND RECOVER***********************"
    print"---------------------------------------------------"
    """
    C. DELETE AND RECOVER
    """
    #  Delete a file
    pithos.container = CONTAINER
    pithos.delete_object(BIGFILE)
    #  Recover file
    file_versions = pithos.get_object_versionlist(BIGFILE)
    print "The file versions"
    for data in file_versions:
        print "The value of id=", data[0], "date=", data[1]

    first_version = file_versions[0]
    v_id, v_date = first_version
    pithos.copy_object(CONTAINER, BIGFILE,
                       CONTAINER, BIGFILE,
                       source_version=v_id)

    print"\n"
    print"---------------------------------------------------"
    print"**********GET FILE DETAILS************************"
    print"---------------------------------------------------"
    objectDetails = pithos.get_object_info(BIGFILE)
    for obj in objectDetails:
        print "The value of", obj, "=", objectDetails.get(obj)

    print"\n"
    print"---------------------------------------------------"
    print"**********SHARING AND PUBLISHING*******************"
    print"---------------------------------------------------"
    """
    D. SHARING AND PUBLISHING
    """
    #  Read permission to all pithos users
    pithos.set_object_sharing(FILETOPUBLISH, read_permission='*')

    #  Publish and get public URL
    pithos.publish_object(FILETOPUBLISH)
    print "Get sharing and public information"
    #  Get sharing and public information
    info = pithos.get_object_info(FILETOPUBLISH)
    for data in info:
        print "The value of", data, "=", info.get(data)

    sharing = info.get('x-object-sharing', {})
    print sharing

    public = info.get('x-object-public', None)
    print "The public URL=", public
    print "Remove sharing and publishing"
    #  Remove sharing and publishing
    pithos.del_object_sharing(FILETOPUBLISH)
    pithos.unpublish_object(FILETOPUBLISH)

    print "Get sharing and public information"
    #  Get sharing and public information
    info = pithos.get_object_info(FILETOPUBLISH)
    sharing = info.get('x-object-sharing', {})
    public = info.get('x-object-public', None)
    print "The public URL=", public