Пример #1
0
def test_object_exists(blobstore: BlobStore,
                       bucket: str,
                       match: str,
                       test_type: ObjectTest = ObjectTest.EXACT) -> bool:
    """
    Test if an object exists in the BlobStore
    :param blobstore: the blobstore to check
    :param bucket: the bucket to check in the blobstore
    :param match: the string to match against; this is _not_ a regex pattern, strings must match exactly
    :param test_type: the type of test to conduct, prefix matches test if the object name starts with the match string,
        exact matches must match the full string
    :return: test bool
    """
    if test_type == ObjectTest.PREFIX:
        try:
            blobstore.list(bucket, prefix=match).__iter__().__next__()
        except StopIteration:
            return False
        else:
            return True
    elif test_type == ObjectTest.EXACT:
        try:
            blobstore.get_user_metadata(bucket, match)
            return True
        except BlobNotFoundError:
            return False
    else:
        raise ValueError(f"Not a valid storage object test type: " +
                         test_type.name)
Пример #2
0
def write_file_metadata(handle: BlobStore, dst_bucket: str, file_uuid: str,
                        file_version: str, document: str):
    # what's the target object name for the file metadata?
    metadata_key = f"files/{file_uuid}.{file_version}"

    # if it already exists, then it's a failure.
    try:
        handle.get_user_metadata(dst_bucket, metadata_key)
    except BlobNotFoundError:
        pass
    else:
        raise BlobAlreadyExistsError()

    handle.upload_file_handle(dst_bucket, metadata_key,
                              io.BytesIO(document.encode("utf-8")))