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

本文介绍 Post 表单预签名的示例代码。

示例代码

以下代码用于使用 POST 表单预签名向 node-sdk-test-bucket 桶添加 putObject-from-preSignedPostSignature-test 对象时,生成对应的 POST 表单签名数据。

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 = 'putObject-from-preSignedPostSignature-test';

    const res = await client.preSignedPostSignature({
      Bucket: bucketName,
      Key: objectName,
      Expires: 3600,
    });
    // 预签名内容
    console.log('preSignedPostSignature:', res.Signature);
  } catch (error) {
    handleError(error);
  }
}

main();

相关文档

关于 POST 表单预签名的详细信息,请参见基于浏览器上传的表单中包含签名