Python SDK

<p><strong>1.&nbsp;简介</strong></p> <ul> <li>OBS Python SDK 适用于 Python3版本</li> <li>本文档主要介绍 OBS Python SDK 的接口使用及注意事项</li> <li>并且假设您是平安云用户,且已经开通了OBS服务</li> <li>如果您还没有开通或者还不了解平安云 OBS 服务,请登录OBS产品主页进行了解</li> </ul> <p>&nbsp;</p> <p><strong>2. SDK下载</strong></p> <p>Python SDK 开发包最新版本&nbsp;&nbsp;<a href="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/sdk%2Fobs-python-sdk-1.1.0.zip?response-content-disposition=attachment%3Bfilename%3Dobs-python-sdk-1.1.0.zip">v1.1.0</a></p> <p>&nbsp;</p> <p><strong>3.&nbsp;API概览</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>API</strong></p> </td> <td> <p><strong>描述</strong></p> </td> </tr> <tr> <td> <p>put_object</p> </td> <td> <p>以字符串/文件/网络流作为上传对象,若该bucket下存在该object_key则更新该对象,并支持添加自定义元数据</p> </td> </tr> <tr> <td> <p>put_object_multipart</p> </td> <td> <p>对于大文件上传,可以切分成片上传。在以下情况下,应该考虑使用分片上传:</p> <p>&Oslash;&nbsp; 文件大于 100M</p> <p>&Oslash;&nbsp; 网络环境差,上传易导致失败</p> <p>需要支持断点续传</p> </td> </tr> <tr> <td> <p>get_object</p> </td> <td> <p>下载指定对象</p> </td> </tr> </tbody> </table> <p>&nbsp;</p> <p><strong>4.&nbsp;API描述</strong></p> <p><strong>(1) put_object(上传字符串)</strong></p> <p><strong>接口描述</strong></p> <p>以字符串为上传对象,若该bucket下存在该object_key则更新该对象,并支持添加自定义元数据。</p> <p><strong>请求参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>是否必须</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>bucket_name</em></p> </td> <td> <p><em>string</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>保存对象的 bucket名称</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>object_key</em></p> </td> <td> <p><em>string</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>对象 key,需自行实现策略保证同一 bucket 下 object_key 唯一, 不能为空,首字符不能为路径符&ldquo;/&rdquo;</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>data</em></p> </td> <td> <p><em>string</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>待上传的字符串</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>object_metadata</em></p> </td> <td> <p>ObjectMetadata</p> </td> <td> <p><em>false</em></p> </td> <td> <p><em>可选参数,自定义元数据</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> </tbody> </table> <p><strong>响应参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>putObjectResult</em></p> </td> <td> <p><em>PutObjectResult</em></p> </td> <td> <p><em>PutObjectResult返回对象</em></p> </td> <td> <p>&nbsp;</p> </td> </tr> </tbody> </table> <p><strong>实例</strong></p> <p><strong>Demo:</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="vertical-align:top"> <p>bucket_name = &ldquo;YourBucketName&rdquo;</p> <p>object_key = &ldquo;YourObjectKey&rdquo;</p> <p>upload_content = &ldquo;YourContent&rdquo;</p> <p>&nbsp;</p> <p>object_metadata = ObjectMetadata()</p> <p>object_metadata.add_user_metadata(&ldquo;MetadataKey&rdquo;,&ldquo;MetadataValue&rdquo;)</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>ret = obs.put_object(bucket_name, object_key, upload_content,object_metadata)</p> <p>print(ret.get_e_tag())</p> </td> </tr> </tbody> </table> <p>&nbsp;</p> <p><strong>(2) put_object(上传文件)</strong></p> <p><strong>接口描述</strong></p> <p>以文件对象为上传对象,若对象存在则更新,并支持添加自定义元数据。</p> <p><strong>请求参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>是否必须</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>bucket_name</em></p> </td> <td> <p><em>string</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>保存对象的 bucket名称</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>object_key</em></p> </td> <td> <p><em>string</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>对象 key,需自行实现策略保证同一 bucket 下 object_key 唯一, 不能为空,首字符不能为路径符&ldquo;/&rdquo;</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>data</em></p> </td> <td> <p><em>file</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>待上传的文件对象</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>object_metadata</em></p> </td> <td> <p>ObjectMetadata</p> </td> <td> <p><em>false</em></p> </td> <td> <p><em>可选参数,自定义元数据</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> </tbody> </table> <p><strong>响应参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>putObjectResult</em></p> </td> <td> <p><em>PutObjectResult</em></p> </td> <td> <p><em>PutObjectResult返回对象</em></p> </td> <td> <p>&nbsp;</p> </td> </tr> </tbody> </table> <p><strong>实例</strong></p> <p><strong>Demo1:</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="vertical-align:top"> <p>bucket_name = &ldquo;YourBucketName&rdquo;</p> <p>object_key = &ldquo;YourObjectKey&rdquo;</p> <p>local_file_upload_path = &ldquo;YourLocalUploadFilePath&rdquo;</p> <p>&nbsp;</p> <p><strong>with </strong>open(local_file_upload_path,&quot;rb&quot;) <strong>as </strong>from_file:</p> <p>ret = obs.put_object(bucket_name, object_key, from_file)</p> </td> </tr> </tbody> </table> <p><strong>Demo2: 使用简单接口上传本地文件</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="vertical-align:top"> <p>bucket_name = &ldquo;YourBucketName&rdquo;</p> <p>object_key = &ldquo;YourObjectKey&rdquo;</p> <p>local_file_upload_path = &ldquo;YourLocalUploadFilePath&rdquo;</p> <p>&nbsp;</p> <p>ret = obs.put_object_from_file(bucket_name, object_key, local_file_upload_path)</p> </td> </tr> </tbody> </table> <p>&nbsp;</p> <p><strong>(3) put_object(上传网络流)</strong></p> <p><strong>接口描述</strong></p> <p>以网络流为上传对象,若对象存在则更新,并支持添加自定义元数据<strong>。</strong></p> <p><strong>请求参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>是否必须</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>bucket_name</em></p> </td> <td> <p><em>string</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>保存对象的 bucket名称</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>object_key</em></p> </td> <td> <p><em>string</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>对象 key,需自行实现策略保证同一 bucket 下 object_key 唯一, 不能为空,首字符不能为路径符&ldquo;/&rdquo;</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>data</em></p> </td> <td> <p><em>Input_stream</em></p> </td> <td> <p><em>true</em></p> </td> <td> <p><em>待上传的文件对象</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> <tr> <td> <p><em>object_metadata</em></p> </td> <td> <p>ObjectMetadata</p> </td> <td> <p><em>false</em></p> </td> <td> <p><em>可选参数,自定义元数据</em></p> </td> <td> <p><em>&nbsp;</em></p> </td> </tr> </tbody> </table> <p><strong>响应参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>putObjectResult</em></p> </td> <td> <p><em>PutObjectResult</em></p> </td> <td> <p><em>PutObjectResult返回对象</em></p> </td> <td> <p>&nbsp;</p> </td> </tr> </tbody> </table> <p><strong>实例</strong></p> <p><strong>Demo:</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="vertical-align:top"> <p><strong>import </strong>requests</p> <p>bucket_name = &ldquo;YourBucketName&rdquo;</p> <p>object_key = &ldquo;YourObjectKey&rdquo;</p> <p>&nbsp;</p> <p>input_stream = requests.get(&quot;http://alm.paic.com.cn/project/2008/board/&quot;, stream=<strong>True</strong>)</p> <p><br /> ret = obs.put_object(bucket_name, object_key, input_stream)</p> </td> </tr> </tbody> </table> <p>&nbsp;</p> <p><strong>(4) put_object_multipart(分片上传)</strong></p> <p><strong>接口描述</strong></p> <p>对于大文件上传,可以切分成片上传。在以下情况下,应该考虑使用分片上传:</p> <ul> <li>文件大于 100M</li> <li>网络环境差,上传易导致失败</li> </ul> <p>需要支持断点续传</p> <p><strong>请求参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数</strong></p> <p><strong>名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>是否</strong></p> <p><strong>必须</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>upload_file_request</em></p> </td> <td> <p><em>MutilpartUploadFileRequest</em></p> </td> <td> <p>true</p> </td> <td> <p><em>分片上传参数 upload_file_request的说明:</em></p> <p><em>每个分片(除了最后一个分片)的大小必须不小于 5M,最后一个分片的大小无限制;</em></p> <p><em>若任何一个分片(非最后一个分片)小于 5M,每个分片的上传不会失败,然而分片完成操作(complete)会失败。</em></p> <p><em>enable_checkpoint 用于设置是否启用断点续传。默认启用。通过调用 upload_file_request.set_enable_checkpoint(False) 关闭断点续传功能。</em></p> <p><em>checkpoint_file_path 用于设置断点续传 ucp 文件的保存位置。默认与待上传文件位于同一个目录下面。若非特殊需求,不需要更改此设置。</em></p> <p><em>upload_notifier 用于设置分片上传的通知接收者。可以实现一个 mutilpart_upload_notifier,并调用 upload_file_request.set_upload_notifier 来接收分片上传的进展的通知,以达到如进度感知等目的。</em></p> </td> <td> <p>&nbsp;</p> </td> </tr> </tbody> </table> <p>响应参数</p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> </tbody> </table> <p><strong>实例</strong></p> <p><strong>Demo:</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="vertical-align:top"> <p><strong>def notify_upload</strong>(upload_id, state, total_parts, finish_parts):</p> <p>&nbsp;</p> <p>bucket_name = &ldquo;YourBucketName&rdquo;</p> <p>object_key = &ldquo;YourObjectKey&rdquo;</p> <p>multipart_upload_file_path = &ldquo;UploadFilePath&rdquo;</p> <p>&nbsp;</p> <p>multipart_request = MultipartUploadFileRequest()</p> <p><br /> multipart_request.set_bucket_name(bucket_name)<br /> multipart_request.set_object_key(object_key)<br /> multipart_request.set_upload_file_path(multipart_upload_file_path)<br /> multipart_request.set_upload_notifier(notify_upload)<br /> <br /> obs.put_object_multipart(multipart_request)</p> </td> </tr> </tbody> </table> <p>&nbsp;</p> <p><strong>(5) get_object(下载文件)</strong></p> <p><strong>接口描述</strong></p> <p>下载指定对象。</p> <p><strong>请求参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>是否必须</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>bucket_name</em></p> </td> <td> <p>string</p> </td> <td> <p>true</p> </td> <td> <p><em>保存对象的 bucket名称</em></p> </td> <td> <p>&nbsp;</p> </td> </tr> <tr> <td> <p><em>object_key</em></p> </td> <td> <p>string</p> </td> <td> <p>true</p> </td> <td> <p><em>对象 key</em></p> </td> <td> <p>&nbsp;</p> </td> </tr> </tbody> </table> <p><strong>响应参数</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <p><strong>参数名称</strong></p> </td> <td> <p><strong>参数类型</strong></p> </td> <td> <p><strong>参数描述</strong></p> </td> <td> <p><strong>备注</strong></p> </td> </tr> <tr> <td> <p><em>s3Object</em></p> </td> <td> <p><em>S3Object</em></p> </td> <td> <p><em>S3Object 返回对象,可以获取对象流自己保存,也可以传入文件对象直接存储</em></p> </td> <td> <p>&nbsp;</p> </td> </tr> </tbody> </table> <p><strong>实例</strong></p> <p><strong>Demo:</strong></p> <table border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="vertical-align:top"> <p>bucket_name = &ldquo;YourBucketName&rdquo;</p> <p>object_key = &ldquo;YourObjectKey&rdquo;</p> <p>to_file_path = &ldquo;DownloadFilePath&rdquo;</p> <p>&nbsp;</p> <p>s3_object = obs.get_object(bucket_name, object_key)</p> <p>&nbsp;</p> <p>#读取对象流自己存储</p> <p><strong>with </strong>open(to_file_path, &quot;wb&quot;) <strong>as </strong>to_file:</p> <p><br /> &nbsp;&nbsp;&nbsp; shutil.copyfileobj(s3_object.get_object_stream(), to_file)</p> <p>&nbsp;</p> <p>#传入文件对象直接存储</p> <p><br /> <strong>with </strong>open(to_file_path, &quot;wb&quot;) <strong>as </strong>to_file:</p> <p><br /> &nbsp;&nbsp;&nbsp; s3_object.to_file(to_file)</p> <p>&nbsp;</p> <p>#获取自定义元数据</p> <p>print(s3_object.get_object_metadata().get_user_metadata())</p> <p>&nbsp;</p> </td> </tr> </tbody> </table> <p>&nbsp;</p>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询 - 7x24 小时

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

7x24 小时,急速解答

工单支持

解决云产品相关技术问题