You need to enable JavaScript to run this app.
导航
普通预签名(Harmony SDK)
最近更新时间:2024.12.23 17:29:48首次发布时间:2024.12.23 17:29:48

TosClient 在向服务端发起请求时,默认会在请求 header 里包含签名。SDK 也支持构造带签名的 URL,您可以直接用该 URL 发起 HTTP 请求,也可以将该 URL 共享给第三方实现访问授权。

生成用于上传对象的预签名 URL

以下代码展示如何生成用于上传对象的预签名 URL。

import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: "Provide your ak",
  accessKeySecret: "Provide your sk",
  securityToken: 'Provide your securityToken',
  region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,"Provide your region" 填写为 cn-beijing。
  endpoint: "Provide your endpoint", // 填写域名地址
});
function handleError(error) {
  if (error instanceof TosClientError) {
    console.log('Client Err Msg:', error.message);
    console.log('Client Err Stack:', error.stack);
  } else if (error instanceof TosServerError) {
    console.log('Request ID:', error.requestId);
    console.log('Response Status Code:', error.statusCode);
    console.log('Response Header:', error.headers);
    console.log('Response Err Code:', error.code);
    console.log('Response Err Msg:', error.message);
  } else {
    console.log('unexpected exception, message: ', error);
  }
}

async function main() {
  try {
    // 存储桶名称
    const bucketName = 'harmonyos-sdk-test-bucket';
    const objectName =  'harmonyos-sdk-test-object';
    const res = await client.preSignedURL({
      Bucket: bucketName,
      Key:  objectName,
    })
    console.log('PreSignedUrl:', res.SignedUrl);
    await axios.put<string, AxiosResponse<string, Any>>(res.SignedUrl, 'put-content');
    const res2 = await client.headObject({
      Bucket: bucketName,
      Key: objectName,
    });
    console.log('date length: ', res2.ContentLength)

  } catch (error) {
    handleError(error);
  }
}
main();

生成用于下载对象的预签名 URL

以下代码展示如何生成用于下载对象的预签名 URL 。

import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk';

// 创建客户端
const client = new TosClient({
  accessKeyId: "Provide your ak",
  accessKeySecret: "Provide your sk",
  securityToken: 'Provide your securityToken',
  region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,"Provide your region" 填写为 cn-beijing。
  endpoint: "Provide your endpoint", // 填写域名地址
});
function handleError(error) {
  if (error instanceof TosClientError) {
    console.log('Client Err Msg:', error.message);
    console.log('Client Err Stack:', error.stack);
  } else if (error instanceof TosServerError) {
    console.log('Request ID:', error.requestId);
    console.log('Response Status Code:', error.statusCode);
    console.log('Response Header:', error.headers);
    console.log('Response Err Code:', error.code);
    console.log('Response Err Msg:', error.message);
  } else {
    console.log('unexpected exception, message: ', error);
  }
}

async function main() {
  try {
    // 存储桶名称
    const bucketName = 'harmonyos-sdk-test-bucket';
    const objectName =  'harmonyos-sdk-test-object';
    const res = await client.preSignedURL({
      Bucket: bucketName,
      Key:  objectName,
      HttpMethod: 'GET',
    })
    console.log('PreSignedUrl:', res.SignedUrl);
    const res2 = await axios.get<string, AxiosResponse<string, Any>>(res.SignedUrl);
    console.log('date length: ', res2.data)

  } catch (error) {
    handleError(error);
  }
}
main();