华为云ModelArts与OBS桶的数据拷贝
一、总体介绍
1.1 上传方式
==上传ModelArts的文件到obs==。
访问方式 | 上传对象方法 |
---|---|
控制台 | 通过控制台上传对象 |
OBS Browser+ | 通过OBS Browser+上传对象 |
obsutil | 通过obsutil上传对象 |
SDK | Java SDK上传Python SDK上传C SDK上传更多语言的SDK示例请参见SDK参考的上传对象章节。 |
API | PUT上传POST上传追加写多段上传 |
1.2 下载方式
==下载obs的文件到ModelArts==。
访问方式 | 下载对象方法 |
---|---|
控制台 | 通过控制台下载对象 |
OBS Browser+ | 通过OBS Browser+下载对象 |
obsutil | 通过obsutil下载对象 |
SDK | Java SDK下载Python SDK下载C SDK下载更多语言的SDK示例请参见SDK参考的下载对象章节。 |
API | 通过API下载对象 |
1.3 下载工具
==推荐使用obsutil工具==。
工具 | 下载方法 | 初始化配置方法 |
---|---|---|
OBS Browser+ | 下载OBS Browser+ | 登录OBS Browser+ |
obsutil | 下载obsutil | 初始化obsutil |
obsfs | 方式一:通过下载获取obsfs方式二:通过编译生成obsfs | 初始化obsfs |
SDK | 下载SDK,具体参考各语言开发指南的下载章节 | 安装和初始化SDK,具体参考各语言开发指南的快速入门章节 |
二、obs SDK(python)
2.1 重要说明
OBS本身是没有文件夹的概念的,桶中存储的元素只有对象。创建文件夹实际上是创建了一个大小为0且对象名以“/”结尾的对象,这类对象与其他对象无任何差异,可以进行下载、删除等操作,只是OBS控制台会将这类以“/”结尾的对象以文件夹的方式展示。
SDK支持上传0KB~5GB的对象。流式上传、文件上传和追加上传每次上传内容大小不能超过5GB;当上传较大文件时,请使用分段上传,分段上传每段内容大小不能超过5GB。
2.2 准备工作
2.2.1 安装obs SDK
pip3 install esdk-obs-python
2.2.2 创建AK、SK
参考资料:快速使用SDK
2.2.3 查看obs相关配置
==配置说明==
server:对应Endpoint服务器节点名称
bucketName,对应bucketName桶名称
objectKey:obs中待下载的对象路径
localFile:下载到本地的路径
2.3 下载文件
==下载obs的文件到ModelArts==。
2.3.1 问题引入
在ModelArts提供了多种类型的开发/运行环境镜像,有些镜像不带notebook,也没有集成moxing组件,不支持obs中的文件下载到开发环境中。所以,需要通过obs SDK的API,对obs对象进行下载和上传的操作。
2.3.2 单文件下载(推荐)
==下载速度快,推荐该下载方式。==
#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
This sample demonstrates how to download an object
from OBS in different ways using the OBS SDK for Python.
'''
from obs import *
AK = '*** Provide your Access Key ***'
SK = '*** Provide your Secret Key ***'
server = 'obs.cn-central-231.xckpjs.com'
bucketName = 'liulingjun-demo'
objectKey = 'datasets/ImageNet2012/imagenet.zip'
localFile = './imagenet.zip'
# 单文件下载
def download_file():
# Constructs a obs client instance with your account for accessing OBS
obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server)
# Download the object to a file
print('Downloading an object to :' + localFile + '\n')
obsClient.getObject(bucketName, objectKey, downloadPath=localFile)
if __name__ == '__main__':
download_file()
# download_file_v1()
2.3.3 断点续传下载
对范围下载的封装和加强,解决下载大对象到本地时由于网络不稳定或程序崩溃导致下载失败的问题。将待下载的对象分成若干个分段分别下载,并实时地将每段下载结果统一记录在checkpoint文件中,仅当所有分段都下载成功时返回下载成功的结果,否则抛出异常提醒用户再次调用接口进行重新下载。
# 断点续传下载
def download_file_v1():
# Download the object to a file
print('Downloading an object to :' + localFile + '\n')
# 下载时的最大并发数
taskNum = 5
# 分段大小,单位字节,取值范围是100KB~5GB,默认为5MB。
partSize = 10 * 1024 * 1024
resp = obsClient.downloadFile(bucketName, objectKey=objectKey, downloadFile=localFile, partSize=partSize, taskNum=5, enableCheckpoint=True)
if resp.status < 300:
print('requestId:', resp.requestId)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
2.4 上传文件
==上传ModelArts的文件到obs==。
2.4.1 单文件上传
def update_file():
# Create bucket
print('Create a new bucket for demo\n')
obsClient.createBucket(bucketName)
# Upload an object to your bucket
print('Uploading a new object to OBS from a file\n')
obsClient.putFile(bucketName, objectKey, localFile)
2.4.2 断点续传上传(推荐)
==上传稳定,推荐该上传方式。==
def update_file():
# Create bucket
print('Create a new bucket for demo\n')
obsClient.createBucket(bucketName)
# Upload an object to your bucket
print('Uploading a new object to OBS from a file\n')
# 下载时的最大并发数
taskNum = 5
# 分段大小,单位字节,取值范围是100KB~5GB,默认为5MB。
partSize = 10 * 1024 * 1024
try:
resp = obsClient.uploadFile(bucketName, objectKey, uploadFile=localFile, partSize=partSize, taskNum=taskNum, enableCheckpoint=True)
if resp.status < 300:
print('requestId:', resp.requestId)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
except:
import traceback
print(traceback.format_exc())
三、obsutil工具(推荐)
==上传下载的速度贼快,极力推荐==。
obsutil是一款用于访问管理华为云对象存储服务(Object Storage Service,OBS)的 命令行工具,您可以使用该工具对OBS进行常用的配置管理操作,如创建桶、上传文件/文件夹、下载文件/文件夹、删除文件/文件夹等。对于熟悉命令行程序的用户,obsutil是执行批量处理、自动化任务的好的选择。obsutil适用于Windows、Linux和macOS三大主流的操作系统。
3.1 下载安装
注意下载不同操作系统对应版本。
3.2 准备环境
3.3 初始化配置
3.4 常用指令
# 初始化配置
./obsutil config -i=ak -k=sk -e=endpoint
# 查看配置
cat /home/ma-user/.obsutilconfig
# 更新配置文件
./obsutil config -interactive
# 检查连通性
./obsutil ls -s
token,Enter回车默认为空。
[ma-user obsutil_linux_arm64_5.4.6]$./obsutil config -interactive
Please input your ak:
xxxxxxxxxxxxxx
Please input your sk:
xxxxxxxxxxxxxxxxxx
Please input your endpoint:
obs.cn-central-231.xckpjs.com
Please input your token:
Config file url:
/home/ma-user/.obsutilconfig
Update config file successfully!
3.4.1 上传文件
最大可以上传5GB(未采用分段上传)或48.8TB(采用分段上传)的单个文件。
# 上传单个文件
./obsutil cp ./setup.sh obs://liulingjun-demo/datasets/ImageNet2012/setup.sh
# 上传文件夹
./obsutil cp /home/ma-user/work/MyDocuments obs://liulingjun-demo/cache/ -f -r
[ma-user@notebook-87136e07-2e4c-4ec6-962b-8e74e7fb09d0 obsutil_linux_arm64_5.4.6]$ ./obsutil cp ./setup.sh obs://liulingjun-demo/datasets/ImageNet2012/setup.sh
Start at 2022-08-02 11:37:30.682067437 +0000 UTC
Parallel: 5 Jobs: 5
Threshold: 50.00MB PartSize: auto
VerifyLength: false VerifyMd5: false
CheckpointDir: /home/ma-user/.obsutil_checkpoint
[-----------------------------------------------] 100.00% ?/s 2.08KB/2.08KB 7ms
Upload successfully, 2.08KB, n/a, /home/ma-user/work/obsutil_linux_arm64_5.4.6/setup.sh --> obs://liulingjun-demo/datasets/ImageNet2012/setup.sh, cost [7], status [200], request id [000001825E58BF84810320C8531F8EAE]
[ma-user@notebook-87136e07-2e4c-4ec6-962b-8e74e7fb09d0 obsutil_linux_arm64_5.4.6]$ ./obsutil cp /home/ma-user/work/MyDocuments obs://liulingjun-demo/cache/ -f -r
Start at 2022-08-02 12:08:35.277367633 +0000 UTC
Parallel: 5 Jobs: 5
Threshold: 50.00MB PartSize: auto
VerifyLength: false VerifyMd5: false
CheckpointDir: /home/ma-user/.obsutil_checkpoint
OutputDir: /home/ma-user/.obsutil_output
[-------------------] 100.00% tps:43.04 544.25MB/s 157/157 1.94GB/1.94GB 3.849s
Succeed count: 157 Failed count: 0
Succeed bytes: 1.94GB
Metrics [max cost:2154 ms, min cost:4 ms, average cost:73.24 ms, average tps:40.77, transfered size:1.94GB]
Task id: 83e02e86-9257-4ed6-8601-8849a8da08ef
3.4.2 下载文件
# 下载单文件
./obsutil cp obs://liulingjun-demo/datasets/ImageNet2012/setup.sh /home/ma-user/work/setup.sh
# 下载文件夹
./obsutil cp obs://liulingjun-demo/cache/MyDocuments /home/ma-user/work -f -r
[ma-user@notebook-87136e07-2e4c-4ec6-962b-8e74e7fb09d0 obsutil_linux_arm64_5.4.6]$ ./obsutil cp obs://liulingjun-demo/datasets/ImageNet2012/imagenet.zip /home/ma-user/work/imagenet.zip
Start at 2022-08-02 12:12:54.886233201 +0000 UTC
Parallel: 5 Jobs: 5
Threshold: 50.00MB PartSize: auto
VerifyLength: false VerifyMd5: false
CheckpointDir: /home/ma-user/.obsutil_checkpoint
TempFileDir: /home/ma-user/work/obsutil_linux_arm64_5.4.6
Waiting to prepare the temp file [152601096259].
[------------------------------] 100.00% 248.97MB/s 142.12GB/142.12GB 9m44.751s
Waiting to rename temporary file...
Download successfully, 142.12GB, n/a, obs://liulingjun-demo/datasets/ImageNet2012/imagenet.zip --> /home/ma-user/work/imagenet.zip, cost [585762], status [206], request id [000001825E792D378102BBAD917AAEDA]
[ma-user@notebook-87136e07-2e4c-4ec6-962b-8e74e7fb09d0 obsutil_linux_arm64_5.4.6]$ ./obsutil cp obs://liulingjun-demo/cache/MyDocuments /home/ma-user/work/MyDocuments -f -r
Start at 2022-08-02 12:03:47.02708488 +0000 UTC
Parallel: 5 Jobs: 5
Threshold: 50.00MB PartSize: auto
VerifyLength: false VerifyMd5: false
CheckpointDir: /home/ma-user/.obsutil_checkpoint
OutputDir: /home/ma-user/.obsutil_output
TempFileDir: /home/ma-user/work/obsutil_linux_arm64_5.4.6
[-------------------] 100.00% tps:35.95 610.33MB/s 157/157 1.94GB/1.94GB 3.454s
Succeed count: 157 Failed count: 0
Succeed bytes: 1.94GB
Metrics [max cost:2826 ms, min cost:4 ms, average cost:97.12 ms, average tps:33.82, transfered size:1.94GB]
Task id: afd2f620-4c49-43e0-96f7-c23117169d3d
四、FAQ
Q:ModelArts SDK、OBS SDK和MoXing的区别?
ModelArts SDK、OBS SDK和MoXing的区别?
Q:obsutil更新密钥配置
[ma-user obsutil_linux_arm64_5.4.6]$./obsutil cp obs://lljyoyo-demo/datasets/imagenet/val.zip /home/ma-user/work/ImageNet2012/val.zip
Start at 2022-08-08 02:32:43.146320594 +0000 UTC
Parallel: 5 Jobs: 5
Threshold: 50.00MB PartSize: auto
VerifyLength: false VerifyMd5: false
CheckpointDir: /home/ma-user/.obsutil_checkpoint
TempFileDir: /home/ma-user/work/obsutil_linux_arm64_5.4.6
Download failed, obs://lljyoyo-demo/datasets/imagenet/val.zip --> /home/ma-user/work/ImageNet2012/val.zip, cost [0], status [403], error code [], error message [], request id [000001827B4C25A28104BC80BBDB73D2]
错误原因:
访问密钥配置有误。
解决办法:
更新密钥配置
./obsutil config -interactive
Q:路径错误,找不到文件
[ma-user obsutil_linux_arm64_5.4.6]$./obsutil cp obs://lljyoyo-demo/datasets/imagenet/val.zip /home/ma-user/work/ImageNet2012/val.zip
Start at 2022-08-08 02:52:53.423657564 +0000 UTC
Parallel: 5 Jobs: 5
Threshold: 50.00MB PartSize: auto
VerifyLength: false VerifyMd5: false
CheckpointDir: /home/ma-user/.obsutil_checkpoint
TempFileDir: /home/ma-user/work/obsutil_linux_arm64_5.4.6
Download failed, obs://lljyoyo-demo/datasets/imagenet/val.zip --> /home/ma-user/work/ImageNet2012/val.zip, cost [0], status [404], error code [], error message [], request id [000001827B5E994B830357FE82973B0A]
[ma-user obsutil_linux_arm64_5.4.6]$./obsutil cp ./setup.sh obs://liulingjun-demo/datasets/ImageNet2012/setup.shStart at 2022-08-08 02:51:31.717462394 +0000 UTC
Parallel: 5 Jobs: 5
Threshold: 50.00MB PartSize: auto
VerifyLength: false VerifyMd5: false
CheckpointDir: /home/ma-user/.obsutil_checkpoint
[----------------------------------------------] 100.00% ?/s 2.08KB/2.08KB 14ms
Upload failed, /home/ma-user/work/obsutil_linux_arm64_5.4.6/setup.sh --> obs://liulingjun-demo/datasets/ImageNet2012/setup.sh, cost [13], status [404], error code [NoSuchBucket], error message [The specified bucket does not exist], request id [000001827B5D5A4F83DF8EAE50594AF8]
错误原因:
路径错误,找不到文件
解决办法:
输入正确的文件路径
- 点赞
- 收藏
- 关注作者
评论(0)