对象元信息是对象的属性描述,包括 HTTP 标准属性(HTTP Header)和用户自定义元数据(User Meta)两种。
通过 TOS Harmony SDK 可以添加和修改已经上传对象的元数据信息。TOS 支持携带以下 7 个请求头来修改对象元数据,在下载对象或者 HEAD 对象时,这些值会设置到 HTTP 头域中返回给客户端。请求头如下所示:
Content-Type | 对象类型。 |
---|---|
Cache-Control | 指定该对象被下载时网页的缓存行为。 |
Expires | RFC2616 中定义的缓存失效时间。 |
Content-Disposition | 对象被下载时的名称。 |
Content-Encoding | 对象被下载时的内容编码类型。 |
Content-Language | 对象被下载时的内容语言格式。 |
x-tos-meta-* | 使用 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) { if (error instanceof TosClientError) { console.log('Client Err Name:', error.name); 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 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() { const meta: Record<string, string> = {}; meta.key = 'value'; try { const res = await client.putObject({ Bucket: bucketName, Key: objectKey, Content: buffer.from('Hello TOS').buffer, // 用户自定义元数据信息 Meta: meta, // 设置对象类型 ContentType: 'text/html', // 设置对象下载时内容语言格式 ContentLanguage: 'zh-cn', // 设置缓存策略 CacheControl: 'no-store', // 设置缓存过期时间 Expires: new Date('2028/01/01'), // 设置下载内容下载时的名称 ContentDisposition: 'example.png', // 设置下载时编码类型 ContentEncoding: 'deflate', }); console.log('putObject Request ID:', res.RequestId); } catch (error) { handleError(error); } } main();
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 Name:', error.name); 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 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 = 'harmony-sdk-test-bucket'; const objectName = 'example_dir/multipartExample'; // 本地 example_dir 文件夹下的 multipartExample 文件 const filePath = './example_dir/multipartExample'; // 初始化分片,指定对象权限为私有,存储类型为低频并设置元数据信息 const meta: Record<string, string> = {}; meta.key = 'value'; const res = await client.createMultipartUpload({ Bucket: bucketName, Key: objectName, Meta: meta, // 设置对象类型 ContentType: 'text/html', // 设置对象下载时内容语言格式 ContentLanguage: 'zh-cn', // 设置缓存策略 CacheControl: 'no-store', // 设置缓存过期时间 Expires: new Date('2028/01/01'), // 设置下载内容下载时的名称 ContentDisposition: 'example.png', // 设置下载时编码类型 ContentEncoding: 'deflate', }); console.log('CreateMultipartUploadV2 Upload ID:', res.UploadId); // 获取本地文件信息并准备进行分片 const stats = await fs.statSync(filePath); // 文件总大小 const totalSize = stats.size; // part size 大小设置为 5M const partSize = 5 * 1024 * 1024; let offset = 0; // partNumber 编号从 1 开始 let partNumber = 1; const partsInfo: UploadedPart[] = []; while (offset < totalSize) { const uploadResult = await client.uploadPart({ Bucket: bucketName, Key: objectName, PartNumber: partNumber, UploadID: res.UploadId, Content: fs.createReadStream(filePath, { start: offset, end: offset + partSize - 1, }), }); const eTag = uploadResult.ETag; console.log(`partNumber${partNumber} Etag:`, eTag); partsInfo.push(new UploadedPart(partNumber, eTag)); partNumber++; offset += partSize; } console.log('parts information', partsInfo); // 完成分片上传 const completeRes = await client.completeMultipartUpload({ Bucket: bucketName, Key: objectName, UploadID: res.UploadId, Parts: partsInfo, }); console.log('result status:', completeRes.StatusCode); } catch (error) { handleError(error); } } main();
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 Name:', error.name); 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 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() { const meta: Record<string, string> = {}; meta.key = 'value'; try { const res = await client.setObjectMeta({ Bucket: bucketName, Key: objectKey, // 用户自定义元数据信息 Meta: meta, // 设置对象类型 ContentType: 'text/html', // 设置对象下载时内容语言格式 ContentLanguage: 'zh-cn', // 设置缓存策略 CacheControl: 'no-store', // 设置缓存过期时间 Expires: new Date('2028/01/01'), // 设置下载内容下载时的名称 ContentDisposition: 'example.png', // 设置下载时编码类型 ContentEncoding: 'deflate', }); console.log('putObject Request ID:', res.RequestId); } catch (error) { handleError(error); } } main();
注意
获取对象元数据前,您必须具有 tos:GetObject
权限,具体操作请参见 IAM 策略概述。
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 Name:', error.name); 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 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'; const objectKey = 'example_dir/example.txt'; async function main() { try { const res = await client.headObject({ Bucket: bucketName, Key: objectKey, }); console.log('headObject Request ID:', res.RequestId); // 查看内容语言格式 console.log('headObject Response ContentLanguage:', res.ContentLength); // 查看下载时的名称 console.log('headObject Response ContentDisposition:', res.ContentDisposition); // 查看编码类型 console.log('headObject Response ContentEncoding:', res.ContentEncoding); // 查看缓存策略 console.log('headObject Response CacheControl:', res.CacheControl); // 查看对象类型 console.log('headObject Response ContentType:', res.ContentType); // 查看缓存过期时间 console.log('headObject Response Expires:', res.Expires); } catch (error) { handleError(error); } } main();
关于设置对象元数据的 API 文档,请参见 SetObjectMeta。