管理数据集和文件

阅读时间8分钟
上次更新4 个月前

开始之前

要访问任何数据集,请确保应用程序具有正确的访问权限。有关更多信息,请参阅管理身份用例.

如何...?

创建一个新的数据集

要将数据集添加到现有的资源,请使用所需信息调用create_dataset

此方法将数据集 ID 作为字符串返回。

    dataset_id = unity_cloud.assets.create_dataset(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_name = "your-new-dataset-name"
    )

更新数据集

要更新数据集名称、描述、标签或文件顺序,请按照以下步骤操作

  1. 创建一个DatasetUpdate对象。
  2. 调用update_dataset方法。
    dataset_update = DatasetUpdate(
        name = "your-dataset-name",
        description = "your-dataset-description",
        tags = ["your-tag-1", "your-tag-2", "your-tag-3"],
        file_order = ["your_file_1.ext", "your_file_2.ext", "your_file_3.ext"]
    )
    unity_cloud.assets.update_dataset(
        dataset_update = dataset_update,
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef"
    )

获取数据集

要获取特定数据集,请调用get_dataset方法。

    dataset = unity_cloud.assets.get_dataset(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef"
    )

获取资源中所有数据集的列表

要获取给定资源中包含的所有数据集,请调用get_dataset_list方法。

    datasets = unity_cloud.assets.get_dataset_list(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef"
    )

将本地文件上传到数据集

要将文件添加到资源中包含的任何数据集,请按照以下步骤操作

  1. 创建一个AssetFileUploadInformation对象。
  2. 调用upload_file方法。
  3. (可选) 通过回调将upload_file方法传递给跟踪进度。
    from pathlib import PurePath, PurePosixPath

    def show_upload_progress(progress):
        print(progress)

    upload_asset = FileUploadInformation(
        organization_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef",
        upload_file_path = PurePath("computer/path/to/file.ext"),
        cloud_file_path = PurePosixPath("path/to/file.ext")
    )

    unity_cloud.assets.upload_file(
        asset_upload_information = upload_asset,
        progress_callback = show_progress
    )

更新文件的元数据

要更新文件的描述和标签,请按照以下步骤操作

  1. 创建一个AssetFileUpdate对象。
  2. 调用update_file方法。

update_file方法中的file_path参数必须与上传文件时使用的云文件路径相对应。

    from pathlib import PurePosixPath

    asset_file_update = FileUpdate(
        description = "your-file-description",
        tags = ["your-tag-1", "your-tag-2", "your-tag-3"]
    )

    unity_cloud.assets.update_file(
        asset_file_update = asset_file_update,
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef",
        cloud_file_path = PurePosixPath("path/to/file.ext")
    )

下载数据集中的文件

要下载文件,请按照以下步骤操作

  1. 使用FilePath创建一个AssetFileDownloadInformation对象。FilePath指示资源管理器中的文件路径,而DownloadFolderPath指示下载到计算机上的目标位置。
  2. 调用download_file方法。
  3. (可选) 通过回调将download_file方法传递给跟踪进度。
    def show_download_progress(progress):
        print(progress)

    asset_file_download_infos = FileDownloadInformation(
        organization_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef",
        cloud_file_path = PurePosixPath("path/to/file.ext"),
        download_folder_path = PurePath("path/to/folder")
    )

    unity_cloud.assets.download_file(
        asset_download_information = asset_file_download_infos,
        progress_callback = show_download_progress
    )

获取文件

要获取特定文件,请调用get_file

    files = unity_cloud.assets.get_file_list(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef",
        cloud_file_path = PurePosixPath("path/to/file.ext")
    )

获取数据集内所有文件的列表

要获取给定数据集内包含的所有文件,请调用get_file_list

    files = unity_cloud.assets.get_file_list(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef"
    )

从数据集删除文件

要从数据集删除文件,请调用unity_cloud.assets.remove_file

    from pathlib import PurePosixPath

    unity_cloud.assets.remove_file(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef",
        file_path = PurePosixPath("path/to/file.ext")
    )

获取图片文件建议的生成标签的列表

要获取图片文件的建议标签列表,请调用get_suggested_tags。它们是根据图像内容生成的。

    suggested_tags = unity_cloud.assets.generate_suggested_file_tags(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = "1234abcd-ab12-cd34-ef56-123456abcdef",
        dataset_id = "1234abcd-12ab-34cd-56ef-123456abcdef",
        file_path = PurePosixPath("path/to/file.ext")
    )