更新时间:2023-11-08 gmt 08:00

设置对象属性-九游平台

您可以在上传对象时设置对象属性。对象属性包含对象md5值(用于校验)、对象存储类型、对象自定义元数据。对象属性可以在多种上传方式下(流式上传、文件上传、分段上传),或时进行设置。

对象属性详细说明见下表:

名称

描述

默认值

contentmd5

对象数据的md5值(经过base64编码),提供给obs服务端,校验数据完整性。

storageclass

对象的存储类型,不同的存储类型可以满足客户业务对存储性能、成本的不同诉求。默认与桶的存储类型保持一致,可以设置为与桶的存储类型不同。

标准存储

metadatadict

用户对上传到桶中对象的自定义属性描述,以便对对象进行自定义管理。

contenttype

上传对象时指定的mime类型,定义对象的类型及网页编码,决定浏览器将以什么形式、什么编码读取对象。

二进制流

customcontenttype

上传对象时指定的mime类型,定义对象的类型及网页编码。区别于contenttype参数,customcontenttype参数允许传入任意字符以指定上传对象的mime类型。

设置对象md5值

您可以通过contentmd5来设置对象md5值。以下代码展示如何设置对象md5值:

static obsclient *client;
nsstring *endpoint = @"your-endpoint";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量accesskeyid和secretaccesskey。
// 您可以登录访问管理控制台获取访问密钥ak/sk,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
char* ak_env = getenv("accesskeyid");
char* sk_env = getenv("secretaccesskey");
nsstring *ak = [nsstring stringwithutf8string:ak_env];
nsstring *sk = [nsstring stringwithutf8string:sk_env];
// 初始化身份验证
obsstaticcredentialprovider *credentialprovider = [[obsstaticcredentialprovider alloc] initwithaccesskey:ak secretkey:sk];
    
//初始化服务配置
obsserviceconfiguration *conf = [[obsserviceconfiguration alloc] initwithurlstring:endpoint credentialprovider:credentialprovider];
    
// 初始化client
client = [[obsclient alloc] initwithconfiguration:conf];
    
nsstring *filepath = [[nsbundle mainbundle]pathforresource:@"filename" oftype:@"type"];
//文件上传
obsputobjectwithfilerequest *request = [[obsputobjectwithfilerequest alloc]initwithbucketname:@"bucketname" objectkey:@"objectname" uploadfilepath:filepath];
    
//设置对象md5
request.contentmd5 = @"your md5 which should be encoded by base64"
    
[client putobject:request completionhandler:^(obsputobjectresponse *response, nserror *error){
    nslog(@"%@",response.etag);
}];
  • 对象数据的md5值必须经过base64编码。
  • obs服务端会将该md5值与对象数据计算出的md5值进行对比,如果不匹配则上传失败,返回http 400错误。
  • 如果不设置对象的md5值,obs服务端会忽略对对象数据的md5值校验。

设置对象存储类型

您可以通过storageclass来设置对象存储类型。以下代码展示如何设置对象存储类型:

static obsclient *client;
nsstring *endpoint = @"your-endpoint";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量accesskeyid和secretaccesskey。
// 您可以登录访问管理控制台获取访问密钥ak/sk,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
char* ak_env = getenv("accesskeyid");
char* sk_env = getenv("secretaccesskey");
nsstring *ak = [nsstring stringwithutf8string:ak_env];
nsstring *sk = [nsstring stringwithutf8string:sk_env];
// 初始化身份验证
obsstaticcredentialprovider *credentialprovider = [[obsstaticcredentialprovider alloc] initwithaccesskey:ak secretkey:sk];
    
//初始化服务配置
obsserviceconfiguration *conf = [[obsserviceconfiguration alloc] initwithurlstring:endpoint credentialprovider:credentialprovider];
    
// 初始化client
client = [[obsclient alloc] initwithconfiguration:conf];
nsstring *filepath = [[nsbundle mainbundle]pathforresource:@"filename" oftype:@"type"];
//文件上传
obsputobjectwithfilerequest *request = [[obsputobjectwithfilerequest alloc]initwithbucketname:@"bucketname" objectkey:@"objectname" uploadfilepath:filepath];
    
// 设置对象存储类型
request.storageclass = obsstorageclassstandard;
    
[client putobject:request completionhandler:^(obsputobjectresponse *response, nserror *error){
    nslog(@"%@",response.etag);
}];
  • 如果不设置,对象的存储类型默认与桶的存储类型保持一致。
  • 对象的存储类型分为三类,其含义与一致。
  • 下载归档存储类型的对象前必须将其恢复,参见。

设置对象自定义元数据

您可以通过metadatadict来设置对象自定义元数据。以下代码展示如何设置对象自定义元数据:

static obsclient *client;
nsstring *endpoint = @"your-endpoint";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量accesskeyid和secretaccesskey。
// 您可以登录访问管理控制台获取访问密钥ak/sk,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
char* ak_env = getenv("accesskeyid");
char* sk_env = getenv("secretaccesskey");
nsstring *ak = [nsstring stringwithutf8string:ak_env];
nsstring *sk = [nsstring stringwithutf8string:sk_env];
// 初始化身份验证
obsstaticcredentialprovider *credentialprovider = [[obsstaticcredentialprovider alloc] initwithaccesskey:ak secretkey:sk];
    
//初始化服务配置
obsserviceconfiguration *conf = [[obsserviceconfiguration alloc] initwithurlstring:endpoint credentialprovider:credentialprovider];
    
// 初始化client
client = [[obsclient alloc] initwithconfiguration:conf];
    
// 流式上传字符串
obsputobjectwithdatarequest *request = [[obsputobjectwithdatarequest alloc]initwithbucketname:@"bucketname" objectkey:@"objectname" uploaddata:[@"" datausingencoding:nsutf8stringencoding]];
// 设置对象元数据
request.metadatadict = @{@"meta1":@"value1",@"meta2":@"value2"};
    
[client putobject:request completionhandler:^(obsputobjectresponse *response, nserror *error){
    nslog(@"%@",response);
}];
  • 在上面设置对象自定义元数据示例代码中,用户自定义了一个名称为“meta1”,值为“value1”的元数据和一个名称为“meta2”,值为“value2”的元数据。
  • 一个对象可以有多个元数据,总大小不能超过8kb。
  • 对象的自定义元数据可以通过obsgetobjectmetadatarequest获取,参见获取对象元数据
  • 使用getobject下载对象时,对象的自定义元数据也会同时下载。

设置对象上传类型

您可以通过contenttype来设置对象上传类型。以下代码展示如何通过contenttype参数设置对象上传类型:

static obsclient *client;
nsstring *endpoint = @"your-endpoint";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量accesskeyid和secretaccesskey。
// 您可以登录访问管理控制台获取访问密钥ak/sk,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
char* ak_env = getenv("accesskeyid");
char* sk_env = getenv("secretaccesskey");
nsstring *ak = [nsstring stringwithutf8string:ak_env];
nsstring *sk = [nsstring stringwithutf8string:sk_env];
// 初始化身份验证
obsstaticcredentialprovider *credentialprovider = [[obsstaticcredentialprovider alloc] initwithaccesskey:ak secretkey:sk];
    
//初始化服务配置
obsserviceconfiguration *conf = [[obsserviceconfiguration alloc] initwithurlstring:endpoint credentialprovider:credentialprovider];
    
// 初始化client
client = [[obsclient alloc] initwithconfiguration:conf];
nsstring *filepath = [[nsbundle mainbundle]pathforresource:@"filename" oftype:@"mp4"];
//文件上传
obsputobjectwithfilerequest *request = [[obsputobjectwithfilerequest alloc]initwithbucketname:@"bucketname" objectkey:@"objectname" uploadfilepath:filepath];
    
// 设置对象上传类型
request.contenttype = obscontenttypemp4;
    
[client putobject:request completionhandler:^(obsputobjectresponse *response, nserror *error){
    nslog(@"%@",response.etag);
}];

类型

obs ios sdk对应枚举值

video/mp4

obscontenttypemp4

text/html

obscontenttypehtml

image/png

obscontenttypepng

image/jpeg

obscontenttypejpeg

image/gif

obscontenttypegif

application/pdf

obscontenttypepdf

audio/mp3

obscontenttypemp3

audio/wav

obscontenttypewav

binary/octet-stream

obscontenttypebinary

video/quicktime

obscontenttypemov

application/vnd.apple.mpegurl

obscontenttypem3u8

您还可以通过customcontenttype字段来设置对象上传类型,以下代码展示如何通过customcontenttype参数设置对象上传类型:

static obsclient *client;
nsstring *endpoint = @"your-endpoint";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量accesskeyid和secretaccesskey。
// 您可以登录访问管理控制台获取访问密钥ak/sk,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
char* ak_env = getenv("accesskeyid");
char* sk_env = getenv("secretaccesskey");
nsstring *ak = [nsstring stringwithutf8string:ak_env];
nsstring *sk = [nsstring stringwithutf8string:sk_env];
// 初始化身份验证
obsstaticcredentialprovider *credentialprovider = [[obsstaticcredentialprovider alloc] initwithaccesskey:ak secretkey:sk];
    
//初始化服务配置
obsserviceconfiguration *conf = [[obsserviceconfiguration alloc] initwithurlstring:endpoint credentialprovider:credentialprovider];
    
// 初始化client
client = [[obsclient alloc] initwithconfiguration:conf];
nsstring *filepath = [[nsbundle mainbundle]pathforresource:@"filename" oftype:@"mp4"];
//文件上传
obsputobjectwithfilerequest *request = [[obsputobjectwithfilerequest alloc]initwithbucketname:@"bucketname" objectkey:@"objectname" uploadfilepath:filepath];
    
// 设置对象上传类型
request.customcontenttype = @"video/mp4";
    
[client putobject:request completionhandler:^(obsputobjectresponse *response, nserror *error){
    nslog(@"%@",response.etag);
}];
  • 当前contenttype参数支持指定上表中列出的11种对象类型。如果不设置,默认采用binary/octet-stream类型。
  • 区别于contenttype参数,customcontenttype参数支持以字符串形式指定任意对象上传类型。如果不设置,以contenttype对应类型为对象最终上传类型。
  • 当同时指定了customcontenttype参数及contenttype参数时,对象最终上传类型为customcontenttype指定的上传类型。

相关文档

网站地图