更新时间:2024-02-08 GMT+08:00

进阶用法

Storage

该对象是InputStorage和OutputStorage的基类,包含了两者的所有能力,可以供用户灵活使用。

属性

描述

是否必填

数据类型

name

名称。

str

title

不填默认使用name的值。

str

description

描述信息。

str

create_dir

表示是否自动创建目录,默认为“False”。

bool

with_execution_id

表示创建目录时是否拼接execution_id,默认为“False”。该字段只有在create_dir为True时才支持设置为True。

bool

使用示例如下:

  • 实现InputStorage相同的能力
    import modelarts.workflow as wf 
    # 构建一个Storage对象, with_execution_id=False, create_dir=False
    storage = wf.data.Storage(name="storage_name", title="title_info", description="description_info", with_execution_id=False, create_dir=False) 
    input_data = wf.data.OBSPath(obs_path = storage.join("directory_path")) # 注意,如果是目录则最后需要加"/",例如:storage.join("/input/data/") 
    
    工作流运行时,如果storage对象配置的根路径为"/root/",则最后得到的路径为"/root/directory_path"
  • 实现OutputStorage相同的能力
    import modelarts.workflow as wf 
    # 构建一个Storage对象, with_execution_id=True, create_dir=True
    storage = wf.data.Storage(name="storage_name", title="title_info", description="description_info", with_execution_id=True, create_dir=True) 
    output_path = wf.data.OBSOutputConfig(obs_path = storage.join("directory_path")) # 注意,只能创建目录,不能创建文件 
    
    工作流运行时,如果storage对象配置的根路径为"/root/",则系统自动创建相对目录,最后得到的路径为"/root/执行ID/directory_path"
  • 通过join方法的参数实现同一个Storage的不同用法
    import modelarts.workflow as wf 
    # 构建一个Storage对象, 并且假设Storage配置的根目录为"/root/"
    storage = wf.data.Storage(name="storage_name", title="title_info", description="description_info", with_execution_id=False, create_dir=False) 
    input_data1 = wf.data.OBSPath(obs_path = storage) # 得到的路径为:/root/
    input_data2 = wf.data.OBSPath(obs_path = storage.join("directory_path")) # 得到的路径为:/root/directory_path,需要用户自行保证该路径存在
    output_path1 = wf.data.OBSOutputConfig(obs_path = storage.join(directory="directory_path", with_execution_id=False, create_dir=True)) # 系统自动创建目录,得到的路径为:/root/directory_path
    output_path2 = wf.data.OBSOutputConfig(obs_path = storage.join(directory="directory_path", with_execution_id=True, create_dir=True)) # 系统自动创建目录,得到的路径为:/root/执行ID/directory_path

Storage可实现链式调用

使用示例如下:
import modelarts.workflow as wf 
# 构建一个基类Storage对象, 并且假设Storage配置的根目录为"/root/"
storage = wf.data.Storage(name="storage_name", title="title_info", description="description_info", with_execution_id=False, create_dir=Fals)
input_storage = storage.join("directory_path_1") # 得到的路径为:/root/directory_path_1
input_storage_next = input_storage.join("directory_path_2") # 得到的路径为: /root/directory_path_1/directory_path_2