取消分段上传任务(HarmonyOS SDK)
功能说明
通过分段上传任务的ID,取消指定桶中的分段上传任务。
您可以选择取消多段上传任务,取消多段上传任务之后无法再次使用该上传ID上传任何段。然后,OBS将释放被取消的多段上传任务中的每个段数据的所有存储。如果有多段上传已在进行中,即使您已执行中止操作,它们仍可以上传成功或失败。如果要释放所有分段使用的所有存储,必须在完成所有多段上传后再取消多段上传任务。
接口约束
- 您必须是桶拥有者或拥有取消分段上传任务的权限,才能取消分段上传任务。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:object:AbortMultipartUpload权限,如果使用桶策略则需授予AbortMultipartUpload权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。
- OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
方法定义
1 | abortMultipartUpload(input: AbortMultipartUploadInput): Response<BaseResponseOutput> |
请求参数
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| input | 必选 | 参数解释: 取消分段上传任务接口入参,详见AbortMultipartUploadInput。 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| Bucket | string | 必选 | 参数解释: 桶名。 约束限制:
默认取值: 无 |
| Key | string | 必选 | 参数解释: 对象名。对象名是对象在存储桶中的唯一标识。对象名是对象在桶中的完整路径,路径中不包含桶名。 例如,您对象的访问地址为examplebucket.obs.cn-north-4.myhuaweicloud.com/folder/test.txt 中,对象名为folder/test.txt。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 |
| UploadId | string | 必选 | 参数解释: 分段上传任务的ID。例如:000001648453845DBB78F2340DD460D8。 取值范围: 长度为32的字符串。 默认取值: 无 |
返回结果
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| CommonMsg | 参数解释: 接口调用完成后的公共信息,包含HTTP状态码,操作失败的错误码等,详见ICommonMsg。 | |
| InterfaceResult | 参数解释: 操作成功后的结果数据,详见BaseResponseOutput。 约束限制: 当Status大于300时为空。 |
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| Status | number | 参数解释: OBS服务端返回的HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 |
| Code | string | 参数解释: OBS服务端返回的错误码。 |
| Message | string | 参数解释: OBS服务端返回的错误描述。 |
| HostId | string | 参数解释: OBS服务端返回的请求服务端ID。 |
| RequestId | string | 参数解释: OBS服务端返回的请求ID。 |
| Id2 | string | 参数解释: OBS服务端返回的请求ID2。 |
| Indicator | string | 参数解释: OBS服务端返回的详细错误码。 |
代码示例
本示例用于取消名为examplebucket桶中的分段上传任务,并且对象名是example/objectname,上传任务的ID是00000188677110424014075CC4A77xxx。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // 引入依赖包 import ObsClient, { AbortMultipartUploadInput } from '@obs/esdk-obs-harmony'; import { process } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; const processManager = new process.ProcessManager(); // 创建ObsClient实例 const obsClient = new ObsClient({ // 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险 // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html AccessKeyId: processManager.getEnvironmentVar("ACCESS_KEY_ID"), SecretAccessKey: processManager.getEnvironmentVar("SECRET_ACCESS_KEY"), // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入 // SecurityToken: processManager.getEnvironmentVar("SECURITY_TOKEN"), // Server填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写 Server: "https://obs.cn-north-4.myhuaweicloud.com", }); async function abortMultipartUpload() { try { const params: AbortMultipartUploadInput = { // 指定存储桶名称 Bucket: "examplebucket", // 指定对象,此处以 example/objectname 为例 Key: "example/objectname", // 指定多段上传任务号,此处以 00000188677110424014075CC4A77xxx 为例 UploadId: "00000188677110424014075CC4A77xxx", }; // 取消分段上传任务 const result = await obsClient.abortMultipartUpload(params); if (result.CommonMsg.Status < 300) { hilog.info(0x0001, 'OBS-Harmony', "Abort multipart upload successful!"); hilog.info(0x0001, 'OBS-Harmony', "RequestId: %s", result.CommonMsg.RequestId); return; } hilog.error(0x0001, 'OBS-Harmony', "An ObsError was found, which means your request sent to OBS was rejected with an error response."); hilog.error(0x0001, 'OBS-Harmony', "Status: %d", result.CommonMsg.Status); hilog.error(0x0001, 'OBS-Harmony', "Code: %s", result.CommonMsg.Code); hilog.error(0x0001, 'OBS-Harmony', "Message: %s", result.CommonMsg.Message); hilog.error(0x0001, 'OBS-Harmony', "RequestId: %s", result.CommonMsg.RequestId); } catch (error) { const err = error as BusinessError; hilog.error(0x0001, 'OBS-Harmony', 'An Exception occurred: code=%d, message: %s', err.code, err.message); } } abortMultipartUpload(); |