TOS 中只有对象的概念,内部使用扁平结构存储数据,为方便您对对象进行分组并简化权限管理,您可以使用目录层次来组织对象。
以下代码用于创建 exampledir 的目录。
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); } } const bucketName = 'harmonyos-sdk-test-bucket'; async function main() { try { // 创建目录 const key = 'exampledir/'; const res = await client.putObject({ Bucket: bucketName, Key: key, }); console.log('putObject Request ID: ', res.RequestId); // 创建多级目录 const key2 = 'exampledir/test/'; const res2 = await client.putObject({ Bucket: bucketName, Key: key2, }); console.log('putObject Request ID: ', res2.RequestId); } catch (error) { handleError(error); } } main();
TOS 只有对象的概念, 可通过创建一个大小为 0 并且以斜线 /
结尾的对象, 模拟目录的功能。
通过 delimiter
和 prefix
两个参数可以模拟目录的功能:
delimiter
为 /
同时设置 prefix
为空, 可返回根目录下的对象和子目录信息。delimiter
为 /
同时设置 prefix
为子目录(subfiledir), 可返回子目录的对象和次级目录。以下代码用于列举根目录下的对象和子目录。
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); } } function printObjectContents(contents: ListedObject[]) { console.log('Contents Length:', contents.length); for (const obj of contents) { // 对象 Key console.log('Object Key:', obj.Key); // 对象最后修改时间 console.log('Object LastModified:', obj.LastModified); // 对象 Etag console.log('Object ETag:', obj.ETag); // 对象大小 console.log('Object Size:', obj.Size); // 对象 Owner console.log('Object Owner:', obj.Owner); // 对象存储类型 console.log('Object StorageClass:', obj.StorageClass); // 对象 CRC64 console.log('Object HashCrc64ecma:', obj.HashCrc64ecma); } } const bucketName = 'harmonyos-sdk-test-bucket'; async function main() { try { // 列举目录下的文件和子目录 let continuationToken = ''; while (true) { const res = await client.listObjectsType2({ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, Delimiter: '/', }); console.log('Sub Dir Length:', res.CommonPrefixes.length); for (const prefix of res.CommonPrefixes) { console.log('Sub Dir:', prefix.Prefix); } if (!res.NextContinuationToken) { break } continuationToken = res.NextContinuationToken; } } catch (error) { handleError(error); } } main();
以下代码用于删除存储桶中 exampledir/
目录下的所有文件。
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); } } function printObjectContents(contents: ListedObject[]) { console.log('Contents Length:', contents.length); for (const obj of contents) { // 对象 Key console.log('Object Key:', obj.Key); // 对象最后修改时间 console.log('Object LastModified:', obj.LastModified); // 对象 Etag console.log('Object ETag:', obj.ETag); // 对象大小 console.log('Object Size:', obj.Size); // 对象 Owner console.log('Object Owner:', obj.Owner); // 对象存储类型 console.log('Object StorageClass:', obj.StorageClass); // 对象 CRC64 console.log('Object HashCrc64ecma:', obj.HashCrc64ecma); } } const bucketName = 'harmonyos-sdk-test-bucket'; async function main() { try { // 删除目录下所有文件 const dir = 'exampledir/'; let continuationToken = ''; while (true) { const res = await client.listObjectsType2({ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, Prefix: dir, }); for (const obj of res.Contents) { const res = await client.deleteObject({ Bucket: bucketName, Key: obj.Key, }); console.log('deleteObject %s Request ID:', obj.Key, res.RequestId); } if (!res.IsTruncated || !res.NextContinuationToken) { break; } continuationToken = res.NextContinuationToken; } } catch (error) { handleError(error); } } main();