本文介绍如何快速使用 TOS Harmony SDK 完成常见操作,例如创建存储桶、上传、下载和删除对象等。
桶是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象数据。以下代码展示如何创建一个存储桶。
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", // 填写域名地址 }); // 存储桶名称 const bucketName = 'harmonyos-sdk-test-bucket'; function handleError(error: 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 { // 创建桶 await client.createBucket({ Bucket: bucketName, }); // 列举所有桶,将列举出刚刚创建的桶 const { data } = await client.listBuckets(); // `theBucket` 即为刚刚创建的桶 const theBucket = data.Buckets.find(it => it.Name === bucketName); console.log('the bucket info', theBucket); } catch (error) { handleError(error); } } main();
说明
关于创建桶的更多示例,请参见创建桶(Harmony SDK)。
存储桶创建成功后,可以往桶中上传对象,以下代码展示如何上传一个对象到已创建的桶中。
说明
关于上传对象的更多示例,请参见上传对象。
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: 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 = 'example_dir/example.txt'; // 目标对象 // 上传对象 await client.putObject({ Bucket: bucketName, Key: objectName, Content: Content: buffer.from('a123').buffer, }); // 查询刚刚上传对象的大小 const res = await client.headObject({ Bucket: bucketName, Key: objectName, }); // object size: 4 console.log('object size:', res.ContentLength); } catch (error) { handleError(error); } } main();
说明
关于上传对象的更多示例,请参见上传对象概述(Harmony SDK)。
以下代码展示如何从桶中下载对象。
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: 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); } } const toString = (arr: ArrayBuffer) => { return buffer.from(arr).toString(); } async function main() { try { const bucketName = 'harmonyos-sdk-test-bucket'; // 目标桶 const objectName = 'example_dir/example.txt'; // 目标对象 // 返回 res.Content 为 stream const res = await client.getObject({ Bucket: bucketName, Key: objectName, }); // 获取返回的 stream 中的所有内容 let allContent = '' while (true) { const buf = reader.read(10000); allContent += toString(buf) if (buf.byteLength === 0) { reader.close(); break; } } console.log('object size:', allContent.length); } catch (error) { handleError(error); } } main();
说明
关于下载对象的更多示例,请参见下载对象概述(Harmony SDK)。
以下代码展示如何列举桶中的对象。
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: 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 res = await client.listObjectsType2({ Bucket: bucketName, MaxKeys: 10, }) console.log(`listObjectsType2 bucketName: ${bucketName}, len is ${res.Contents.length}`); } catch (error) { handleError(error); } } main();
说明
关于列举对象的更多示例,请参见列举对象 V2(Harmony SDK)。
如下代码展示如何从桶中删除对象。
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: 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 = 'delete-object-test'; // 目标对象 const res = await client.deleteObject({ Bucket: bucketName, Key: objectName, }); console.log(`deleteObject bucketName: ${bucketName} status: ${res3.StatusCode}`); } catch (error) { handleError(error); } } main();
说明
关于删除对象的更多示例,请参见删除对象(Harmony SDK)。