HarmonyOS 鸿蒙Next obs sdk需要适配进度展示

HarmonyOS 鸿蒙Next obs sdk需要适配进度展示 【问题描述】:obs sdk需要适配“上传文件进度展示”的功能 :https://gitcode.com/HuaweiCloudDeveloper/obs-sdk-harmony

【问题现象】:不涉及

【版本信息】:不涉及

【复现代码】:不涉及

【尝试解决方案】:不涉及

3 回复

楼主,当前是不支持进度的,可以到华为云工单平台提需求。

https://console.huaweicloud.com/ticket/?region=cn-north-4#/ticketindex/createTicketService

更多关于HarmonyOS 鸿蒙Next obs sdk需要适配进度展示的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在 HarmonyOS NEXT 中,OBS SDK 需使用 ArkTS/TS/JS 接口,进度展示依赖 request.UploadTaskrequest.DownloadTaskprogress 回调。若 SDK 未封装,可基于 Response 流或自定义流量监控实现。

OBS SDK for HarmonyOS 已内置ProgressListener,只需在构造上传请求时设置监听即可实现进度展示。核心代码片段如下:

import { ObsClient, PutObjectInput, ProgressListener, ProgressStatus } from '@huaweicloud/obs-harmony';

// 创建ObsClient实例
const obsClient = new ObsClient({
  accessKeyId: 'your-ak',
  secretAccessKey: 'your-sk',
  server: 'https://your-endpoint'
});

// 自定义进度监听实现
class MyProgressListener implements ProgressListener {
  onProgress(status: ProgressStatus): void {
    // status.transferredBytes: 已传输字节数
    // status.totalBytes: 总字节数(文件字节数)
    const percent = Math.round((status.transferredBytes * 100) / status.totalBytes);
    console.info(`上传进度: ${percent}%`);
    // 此处可通过状态管理更新UI进度条
  }
}

// 构造上传请求
const input: PutObjectInput = {
  bucketName: 'your-bucket',
  objectKey: 'example.jpg',
  filePath: '/data/storage/el2/base/haps/entry/files/example.jpg', // 本地文件沙箱路径
  progressListener: new MyProgressListener()
};

// 执行上传
obsClient.putObject(input).then((result) => {
  console.info('上传成功', result);
}).catch((err) => {
  console.error('上传失败', err);
});

在UI层使用@State配合Progress组件刷新即可同步显示百分比。若需暂停/恢复,可借助obsClient.abortRequest(requestId)。以上适配可直接复用上述接口,无需额外封装。

回到顶部