HarmonyOS鸿蒙Next中访问阿里云OSS示例代码

HarmonyOS鸿蒙Next中访问阿里云OSS示例代码

介绍

本示例介绍通过URL签名(v4)方式使用API访问阿里云OSS。

访问阿里云OSS源码链接

效果预览

图片名称

使用说明

进入应用后,展示一个加号图标,点击图标即可选择要传输的图片,选择要传输的文件后,应用进行URL签名(v4)方式,即可使用API访问阿里云OSS。

实现思路

构造规范请求字段

参数说明

参数 说明
method 请求方法,目前Demo中签名只支持GET PUT两种请求
contentType 请求的媒体类型,只有在PUT请求时才需加入到签名中
date 日期,ISO8601格式的日期
dateTime 日期时间,ISO8601格式的日期时间
expires 签名URL的有效时长,单位为秒(s)。最小值为1,最大值为 604800(即7天)
bucketName 存储空间名称
path 阿里云OSS上文件保存的根路径
savePath 阿里云OSS上文件的保存路径,包含文件名,不包含根路径字段
ossRegion 阿里云OSS所在区域
accessKeyId 临时接入KeyId
securityToken 临时安全token

PUT规范请求

PUT
/examplebucket/oss/path/test/test.txt
x-oss-additional-headers=host&x-oss-credential=accessKeyId%2Fcn-shanghai%2Foss%2Faliyun_v4_request&x-oss-date=20240803T130523Z&x-oss-expires=3600&x-oss-security-token=CAIS87WEEDddsada2F==&x-oss-signature-version=OSS4-HMAC-SHA256
content-type:text/plain
host:examplebucket.oss-cn-shanghai.aliyuncs.com

host
UNSIGNED-PAYLOAD

GET规范请求

GET
/examplebucket/oss/path/test/test.txt
x-oss-additional-headers=host&x-oss-credential=accessKeyId%2Fcn-shanghai%2Foss%2Faliyun_v4_request&x-oss-date=20240803T130523Z&x-oss-expires=3600&x-oss-security-token=CAIS87WEEDddsada2F==&x-oss-signature-version=OSS4-HMAC-SHA256
host:examplebucket.oss-cn-shanghai.aliyuncs.com

host
UNSIGNED-PAYLOAD

签名计算

构造sha256Hex()函数,计算SHA256哈希值。

public sha256Hex(data: string): string {
  let sha256 = cryptoFramework.createMd('SHA256');
  sha256.updateSync({ data: new Uint8Array(buffer.from(data, 'utf-8').buffer) });
  let sha256Output = sha256.digestSync();
  let output = buffer.from(sha256Output.data).toString('hex')
  return output;
}

构造zeroFormat(),获取当前日期和时间(ISO8601格式)。

private zeroFormat(num: number, length: number = 2): string {
  let format = '';
  for (let i = 0; i < (length - num.toString().length); i++) {
    format += '0';
  }
  format += num.toString();
  return format;
}

private dateTimeISO8601(): string {
  let date = new Date();
  let rc = this.zeroFormat(date.getFullYear(), 4) + this.zeroFormat(date.getMonth() + 1) +
  this.zeroFormat(date.getDate()) + 'T' + this.zeroFormat(date.getUTCHours()) +
  this.zeroFormat(date.getMinutes()) + this.zeroFormat(date.getSeconds()) + 'Z';
  return rc;
}

构造canonicalRequest()函数,用以构造规范请求字符串,注意的是以下代码字符串顺序不能改变,否则与阿里云OSS上计算的签名不一致。

private canonicalRequest(method: string, date: string, dateTime: string, pathOss: string, contentType: string = ''): string {
  let canonicalRequest = method + "\n";

  canonicalRequest += this.canonicalURI(pathOss);
  canonicalRequest += this.canonicalQueryString(date, dateTime);

  if (method === 'PUT') {
    canonicalRequest += "content-type:" + contentType + '\n';
  }

  canonicalRequest += 'host:' + this.ossInfo.bucket_name + '.' + this.ossInfo.endpoint + '\n';
  canonicalRequest += '\n';
  canonicalRequest += 'host\n';
  canonicalRequest += 'UNSIGNED-PAYLOAD';

  return canonicalRequest;
}

构造signString()函数,用以构造签名字符串,以下代码字符串顺序不能改变,否则与阿里云OSS上计算的签名不一致

private signString(date: string, dateTime: string, canonical: string): string {
  let stringToSign = '';

  stringToSign += 'OSS4-HMAC-SHA256' + '\n';
  stringToSign += dateTime + '\n';
  stringToSign += date + '/' + this.ossRegion() + '/oss/aliyun_v4_request' + '\n';
  stringToSign += canonical;
  console.log(TAG, "stringToSign: " + stringToSign);

  return stringToSign;
}

构造calcSignature()函数,通过调用三方库CryptoJS方法去计算签名。

private calcSignature(date: string, signString: string): string {
  let dateKey: Uint8Array = CryptoJS.HmacSHA256(date, "aliyun_v4" + this.ossKey.AccessKeySecret);
  let regionKey: Uint8Array = CryptoJS.HmacSHA256(this.ossRegion(), dateKey);
  let regionServiceKey: Uint8Array = CryptoJS.HmacSHA256("oss", regionKey);
  let signingKey: Uint8Array = CryptoJS.HmacSHA256("aliyun_v4_request", regionServiceKey);
  let signature: string = CryptoJS.HmacSHA256(signString, signingKey).toString();
  console.log(TAG, "response: signature: " + signature);
  return signature;
}

更多关于HarmonyOS鸿蒙Next中访问阿里云OSS示例代码的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS鸿蒙Next中访问阿里云OSS,可以使用HarmonyOS提供的网络请求能力和阿里云OSS的SDK进行集成。以下是一个简单的示例代码,展示了如何在HarmonyOS中上传文件到阿里云OSS。

  1. 添加依赖:首先需要在build.gradle中添加阿里云OSS的依赖。

    dependencies {
        implementation 'com.aliyun.dpa:oss-android-sdk:2.9.11'
    }
    
  2. 初始化OSS客户端:在HarmonyOS中初始化OSS客户端。

    import com.alibaba.sdk.android.oss.ClientConfiguration;
    import com.alibaba.sdk.android.oss.OSS;
    import com.alibaba.sdk.android.oss.OSSClient;
    import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
    import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
    
    String endpoint = "https://your-endpoint";
    String stsAK = "your-access-key-id";
    String stsSK = "your-access-key-secret";
    String stsToken = "your-sts-token";
    
    OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(stsAK, stsSK, stsToken);
    ClientConfiguration conf = new ClientConfiguration();
    conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒
    conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒
    conf.setMaxConcurrentRequest(5); // 最大并发请求数,默认5个
    conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
    
    OSS oss = new OSSClient(context, endpoint, credentialProvider, conf);
    
  3. 上传文件:使用OSS客户端上传文件。

    import com.alibaba.sdk.android.oss.model.PutObjectRequest;
    import com.alibaba.sdk.android.oss.model.PutObjectResult;
    
    String bucketName = "your-bucket-name";
    String objectKey = "your-object-key";
    String uploadFilePath = "your-file-path";
    
    PutObjectRequest put = new PutObjectRequest(bucketName, objectKey, uploadFilePath);
    
    try {
        PutObjectResult putResult = oss.putObject(put);
        Log.d("PutObject", "Upload Success");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("PutObject", "Upload Fail", e);
    }
    

这段代码展示了如何在HarmonyOS鸿蒙Next中初始化阿里云OSS客户端并上传文件。确保替换代码中的占位符为实际的值。

更多关于HarmonyOS鸿蒙Next中访问阿里云OSS示例代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中访问阿里云OSS,可以通过以下示例代码实现。首先,确保已在项目中引入阿里云OSS SDK,并配置好AccessKeyEndpoint

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;

public class OSSExample {
    public static void main(String[] args) {
        // OSS配置
        String endpoint = "https://your-oss-endpoint";
        String accessKeyId = "your-access-key-id";
        String accessKeySecret = "your-access-key-secret";
        String bucketName = "your-bucket-name";
        String objectName = "your-object-name";
        String filePath = "path/to/your/file";

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath));
        ossClient.putObject(putObjectRequest);

        // 关闭OSSClient
        ossClient.shutdown();
    }
}

此代码展示了如何上传文件到阿里云OSS,请根据实际情况替换endpointaccessKeyIdaccessKeySecretbucketNameobjectNamefilePath

回到顶部