九游平台/ 对象存储服务 obs/ sdk参考/ / / 设置对象属性(c sdk)
更新时间:2025-02-18 gmt 08:00

设置对象属性(c sdk)-九游平台

开发过程中,您有任何问题可以在github上,或者在中发帖求助。

功能说明

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

接口约束

  • 您必须是桶拥有者或拥有设置对象属性的权限,才能设置对象属性。建议使用iam或桶策略进行授权,如果使用iam则需授予obs:object:modifyobjectmetadata权限,如果使用桶策略则需授予modifyobjectmetadata权限。相关授权方式介绍可参见obs权限控制概述,配置方式详见、。
  • obs支持的region与endpoint的对应关系,详细信息请参见。
  • 一个对象可以有多个元数据,总大小不能超过8kb。
  • 当前元数据名称不支持非ascii码字符,元数据值包含非ascii码字符时需进行base64编码。
  • 如果不设置存储类别,对象的存储类别默认与桶的存储类别保持一致。

方法定义

void set_object_metadata(const obs_options *options, obs_object_info *object_info, 
	obs_put_properties *put_properties,
	server_side_encryption_params *encryption_params,
	obs_response_handler *handler, void *callback_data);

请求参数说明

表1 请求参数列表

参数名称

参数类型

是否必选

描述

options

const obs_options*

必选

参数解释:

请求桶的上下文,,通过obs_options设置ak、sk、endpoint、bucket、超时时间、临时鉴权。

约束限制:

object_info

obs_object_info *

必选

参数解释:

对象名和版本号。

约束限制:

非多版本对象,version设置为0。

put_properties

obs_put_properties*

必选

参数解释:

上传对象属性。

约束限制:

encryption_params

server_side_encryption_params *

可选

参数解释:

上传对象加密设置。

约束限制:

handler

obs_response_handler *

必选

参数解释:

回调结构体,结构体内所有成员都是回调函数的指针,用于设置处理接口响应数据的回调函数。您可以通过设置回调函数,把服务端的响应数据复制到您的自定义回调数据callback_data中。

约束限制:

callback_data

void *

可选

参数解释:

用户自定义回调数据。

约束限制:

取值范围:

默认取值:

表2 obs_options

参数名称

参数类型

是否必选

描述

bucket_options

obs_bucket_context

必选

参数解释:

桶相关设置。

约束限制:

request_options

obs_http_request_option

必选

参数解释:

请求相关设置。

约束限制:

temp_auth

temp_auth_configure*

可选

参数解释:

用于临时计算签名的结构体,不使用时请设置为null。

约束限制:

表3 obs_bucket_context

参数名称

参数类型

是否必选

描述

host_name

char *

必选

参数解释

连接obs的服务地址,请求使用的主机名,是指存放资源的服务器的域名,就是终端节点endpoint。

示例:host_name = "obs.cn-north-4.myhuaweicloud.com";

约束限制:

不需要带“http://”或“https://”前缀,通过obs_protocol控制是使用http协议还是https协议。

取值范围:

您可以从查看obs当前开通的服务地址。

默认取值:

bucket_name

char *

必选

参数解释

桶名。

约束限制:

  • 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。
  • 桶命名规则如下:
    • 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。
    • 禁止使用ip地址。
    • 禁止以“-”或“.”开头及结尾。
    • 禁止两个“.”相邻(如:“my..bucket”)。
    • 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。
  • 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。

取值范围:

默认取值:

usecname

bool

可选

参数解释

是否通过自定义域名访问obs服务。

约束限制:

取值范围:

true:通过自定义域名访问obs服务。

false:不通过自定义域名访问obs服务。

默认取值:

false

protocol

obs_protocol

可选

参数解释

是使用http协议还是https协议。

约束限制:

取值范围:

请详见obs_protocol

默认取值:

obs_protocol_https(默认使用https协议)

access_key

char *

必选

参数解释

访问密钥中的ak。

约束限制:

取值范围:

请详见。

默认取值:

secret_access_key

char *

必选

参数解释

访问密钥中的sk。

约束限制:

请详见。

取值范围:

默认取值:

storage_class

obs_storage_class

可选

参数解释

创桶时可指定的桶的存储类别。

约束限制:

取值范围:

请详见obs_storage_class

默认取值:

obs_storage_class_standard(指标准存储类别)

token

char *

可选

参数解释

临时访问密钥中的securitytoken。

约束限制:

取值范围:

请详见。

默认取值:

epid

char *

可选

参数解释

创桶时可指定的企业项目id,开通企业项目的用户可以从企业项目服务获取。

约束限制:

epid格式为uuid,未开通企业项目的用户可以不带该头域。

示例:9892d768-2d13-450f-aac7-ed0e44c2585f

默认取值:

bucket_type

obs_bucket_type

可选

参数解释

创桶时,指定是对象桶还是并行文件系统。

约束限制:

取值范围:

请详见obs_bucket_type

默认取值:

obs_bucket_object(指对象桶)

bucket_list_type

obs_bucket_list_type

可选

参数解释

列举桶时,确定列举桶的类型:所有桶、对象桶、并行文件系统。

约束限制:

取值范围:

请详见obs_bucket_list_type

表4 obs_storage_class

枚举值

说明

obs_storage_class_standard

标准存储。

标准存储拥有低访问时延和较高的吞吐量,适用于有大量热点对象(平均一个月多次)或小对象(<1mb),且需要频繁访问数据的业务场景。

obs_storage_class_standard_ia

低频访问存储。

低频访问存储适用于不频繁访问(平均一年少于12次)但在需要时也要求能够快速访问数据的业务场景。

obs_storage_class_glacier

归档存储。

归档存储适用于很少访问(平均一年访问一次)数据的业务场景。

obs_storage_class_deep_archive

深度归档存储(受限公测)

适用于长期不访问(平均几年访问一次)数据的业务场景。

表5 obs_http_request_option

参数名称

参数类型

是否必选

描述

connect_time

int

必选

参数解释

建立http/https连接的超时时间(单位:毫秒)。

约束限制:

取值范围:

[10000, 60000]

默认取值:

60000

max_connected_time

int

必选

参数解释

请求超时时间(单位:秒)。

约束限制:

取值范围:

默认取值:

0(指永远不会主动断开链接)

proxy_auth

char*

可选

参数解释

代理认证信息,格式为username:password。

约束限制:

取值范围:

默认取值:

proxy_host

char*

可选

参数解释

代理服务器的ip地址或主机名。

约束限制:

取值范围:

默认取值:

表6 obs_protocol

枚举值

说明

obs_protocol_https

使用https协议访问。

obs_protocol_http

使用http协议访问。

表7 obs_bucket_type

枚举值

说明

obs_bucket_object

对象桶。

obs_bucket_pfs

并行文件系统。

表8 obs_bucket_list_type

枚举值

说明

obs_bucket_list_all

列举所有桶。

obs_bucket_list_object

列举所有对象桶。

obs_bucket_list_pfs

列举所有并行文件系统。

表9 temp_auth_configure

参数名称

参数类型

是否必选

描述

expires

long long int

必选

参数解释:

临时鉴权的有效期(单位:秒)。

约束限制:

取值范围:

[0-630720000]

默认取值

temp_auth_callback

void(*temp_auth_callback)(char *temp_auth_url,

uint64_t temp_auth_url_len,

char*temp_auth_headers,

uint64_t temp_auth_headers_len,

void*callback_data)

必选

参数解释:

用户自定义回调函数指针,用于将临时url以及涉及的计算签名头域记录到用户自定义回调数据中。

约束限制:

callback_data

void *

必选

参数解释:

用户自定义回调数据。

约束限制:

取值范围:

默认取值

表10 temp_auth_callback

参数名称

参数类型

是否必选

描述

temp_auth_url

char *

必选

参数解释:

临时鉴权的url。obs九游平台的服务支持用户构造一个特定操作的url,这个url的query参数中会包含用户ak、签名、有效期等信息,任何拿到这个url的人均可执行临时鉴权操作,obs服务收到这个请求后认为该请求就是签发url用户自己在执行操作。例如构造一个携带签名信息的下载对象的url,拿到相应url的人都能下载这个对象,但该url只在expires指定的失效时间内有效。

约束限制:

取值范围:

默认取值

temp_auth_url_len

uint64_t

必选

参数解释:

临时鉴权的url的长度。

约束限制:

取值范围:

默认取值

temp_auth_headers

char *

必选

参数解释:

临时鉴权的headers。

约束限制:

取值范围:

默认取值

temp_auth_headers_len

uint64_t

必选

参数解释:

临时鉴权的headers的数量。

约束限制:

取值范围:

默认取值

callback_data

void *

必选

参数解释:

用户自定义回调数据。

约束限制:

取值范围:

默认取值

表11 obs_object_info

参数名称

参数类型

是否必选

描述

key

char *

必选

参数解释:

对象名。

约束限制:

取值范围:

默认取值:

version_id

char *

可选

参数解释:

对象版本号,如果非多版本对象,请把version_id设置为null。

约束限制:

取值范围:

默认取值:

表12 obs_put_properties

参数名称

参数类型

是否必选

描述

content_type

char *

必选

参数解释:

指定对象被下载时的文件类型。

约束限制:

取值范围:

参见http标准头域content-type的取值。

默认取值:

md5

char *

必选

参数解释:

对象数据的md5值(经过base64编码),提供给obs服务端,校验数据完整性。obs服务端会将该md5值与对象数据计算出的md5值进行对比,如果不匹配则上传失败,返回http 400错误。

约束限制:

对象数据的md5值必须经过base64编码。如果不设置对象的md5值,obs服务端会忽略对对象数据的md5值校验。

取值范围:

按照rfc 1864标准计算出消息体的md5摘要字符串,即消息体128-bit md5值经过base64编码后得到的字符串。

示例:n58ig6hfm7vqi4k0vnwpog==

默认取值:

cache_control

char *

必选

参数解释:

指定对象被下载时的网页的缓存行为。

约束限制:

取值范围:

参见http标准头域cache-control的取值。

默认取值:

content_disposition_filename

char *

必选

参数解释:

指定对象被下载时的名称。假设设置为test.txt则相当于添加了content-disposition: attachment; filename=test.txt头域。

约束限制:

取值范围:

参见http标准头域content-disposition的取值。

默认取值:

content_encoding

char *

必选

参数解释:

指定对象被下载时的内容编码格式。

约束限制:

取值范围:

参见http标准头域content-encoding的取值。

默认取值:

website_redirect_location

char *

必选

参数解释:

当桶设置了website配置,可以将获取这个对象的请求重定向到桶内另一个对象或一个外部的url。

例如,重定向请求到桶内另一对象:

x-obs-website-redirect-location:/anotherpage.html

或重定向请求到一个外部url:

x-obs-website-redirect-location:http://www.example.com/

约束限制:

必须以“/”、“http://”或“https://”开头,长度不超过2kb。

取值范围:

默认取值:

get_conditions

obs_get_conditions *

可选

参数解释:

指定复制对象时的一系列参数。

约束限制:

start_byte

uint64_t

可选

参数解释:

指定复制对象的起始位置。

约束限制:

取值范围:

[0~对象长度-1),单位:字节。

默认取值:

0(即从对象的第一个字节开始复制)

byte_count

uint64_t

可选

参数解释:

指定复制的长度。

约束限制:

  • 取值必须大于0。
  • 如果“start_byte byte_count”大于对象长度-1,实际仍取对象长度-1,单位为字节。

取值范围:

默认取值:

upload_limit

uint64_t

可选

参数解释:

对单链接请求的带宽限制。

约束限制:

取值范围:

[819200,838860800],单位 bit/s。

默认取值:

expires

int64_t

可选

参数解释:

obs请求中expires头的值。指定object被下载时的网页的缓存过期时间。

约束限制:

取值范围:

默认取值:

obs_expires

int64_t

可选

参数解释:

指定对象过期时间,单位是天。过期之后对象会被自动删除。

约束限制:

设置的天数计算出的过期时间不能早于当前时间,如10天前上传的对象,不能设置小于10的值。

取值范围:

大于0的整数值。

默认取值:

canned_acl

obs_canned_acl

可选

参数解释:

权限控制策略。

约束限制:

取值范围:

请详见obs_canned_acl

az_redundancy

obs_az_redundancy

可选

参数解释:

指定复制对象时的一系列参数。

约束限制:

取值范围:

请详见obs_az_redundancy

meta_data_count

int

可选

参数解释:

meta_data数组中元素的个数。

约束限制:

取值范围:

默认取值:

meta_data

obs_name_value*

可选

参数解释:

对象的自定义元数据。obs支持用户使用自定义的元数据,以便对对象进行自定义管理。当用户获取此对象或查询此对象元数据时,加入的自定义元数据将会在返回的消息头中出现。

约束限制

  • 如果请求携带了此头域,那么响应的消息中应该包含此消息头。
  • 所有自定义元数据大小的总和不超过8k。单个自定义元数据大小的计算方式为:每个键和值的utf-8编码中的字节总数。
  • 自定义元数据的key值不区分大小写,obs统一转为小写进行存储。value值区分大小写。
  • 自定义元数据key-value对都必须符合us-ascii。如果一定要使用非ascii码或不可识别字符,需要客户端自行做编解码处理,可以采用url编码或者base64编码,服务端不会做解码处理。例如x-obs-meta-中文:中文经url编码后发送,“中文”的url编码为:中文,则响应为x-obs-meta-中文: 中文

metadata_action

metadata_action_indicator

可选

参数解释:

元数据操作指示符。

约束限制:

取值范围:

请详见metadata_action_indicator

server_callback

obs_upload_file_server_callback

可选

参数解释:

服务端回调相关参数。

约束限制:

表13 obs_canned_acl

枚举值

说明

obs_canned_acl_private

私有读写,桶或对象的所有者拥有完全控制的权限,其他任何人都没有访问权限。

obs_canned_acl_public_read

公共读私有写,设在桶上,所有人可以获取该桶内对象列表、桶内多段任务、桶的元数据、桶的多版本。

设在对象上,所有人可以获取该对象内容和元数据。

obs_canned_acl_public_read_write

公共读写,设在桶上,所有人可以获取该桶内对象列表、桶内多段任务、桶的元数据、上传对象删除对象、初始化段任务、上传段、合并段、复制段、取消多段上传任务。

设在对象上,所有人可以获取该对象内容和元数据。

obs_canned_acl_public_read_delivered

桶公共读,桶内对象公共读,设在桶上,所有人可以获取该桶内对象列表、桶内多段任务、桶的元数据,可以获取该桶内对象的内容和元数据。

不能应用于对象。

obs_canned_acl_public_read_write_delivered

桶公共读写,桶内对象公共读写,设在桶上,所有人可以获取该桶内对象列表、桶内多段任务、桶的元数据、上传对象、删除对象、初始化段任务、上传段、合并段、复制段、取消多段上传任务,可以获取该桶内对象的内容和元数据。

不能应用于对象。

表14 obs_get_conditions

参数名称

参数类型

是否必选

描述

start_byte

uint64_t

可选

参数解释:

指定下载对象的起始位置。

约束限制:

取值范围:

[0~对象长度-1),单位:字节。

默认取值:

0(即从对象的第一个字节开始下载)

byte_count

uint64_t

可选

参数解释:

指定下载的长度。

约束限制:

  • 取值必须大于0。
  • 如果“start_byte byte_count”大于对象长度-1,实际仍取对象长度-1,单位为字节。

取值范围:

默认取值:

download_limit

uint64_t

可选

参数解释:

对单链接请求的带宽限制。

约束限制:

取值范围:

[819200,838860800],单位 bit/s。

默认取值:

if_modified_since

int64_t

可选

参数解释:

如果对象在指定的时间后有修改,则请求成功,否则返回错误。

约束限制:

取值范围:

默认取值:

if_not_modified_since

int64_t

可选

参数解释:

如果对象在指定的时间后没有修改,则请求成功,否则返回错误。

约束限制:

取值范围:

默认取值:

if_match_etag

char *

可选

参数解释:

指定一个预设的etag值,如果下载或复制对象的etag值与该参数值相同,则请求成功,否则返回错误。

约束限制:

取值范围:

长度为32的字符串。

默认取值:

if_not_match_etag

char *

可选

参数解释:

指定一个预设的etag值,如果下载或复制对象的etag值与该参数值不相同,则请求成功,否则返回错误。

约束限制:

取值范围:

长度为32的字符串。

默认取值:

image_process_config

image_process_configure *

可选

参数解释:

图片处理相关参数。

约束限制:

表15 metadata_action_indicator

枚举值

说明

obs_no_metadata_action

默认的无效值。

obs_replace

表示使用当前请求中携带的头域完整替换,未指定的元数据会被删除。

obs_replace_new

表示对于已经存在值的元数据进行替换,不存在值的元数据进行赋值,未指定的元数据保持不变(自定义元数据作替换处理)。

表16 obs_upload_file_server_callback

参数名称

参数类型

是否必选

描述

callback_url

char *

必选

参数解释:

对象上传成功之后,obs向此url发送回调请求,请求方法为post。

支持设置多个url,以英文分号(;)分隔,最多支持10个。

callbackurl需要做url编码。例如:“http://www.example.com/中文?key=中文名”需要编码成“http://www.example.com/中文?key=中文名”。

约束限制:

取值范围:

默认取值:

callback_host

char *

可选

参数解释:

发起回调请求的host头域的值,如果不设置,会使用callbackurl解析出来的host。

约束限制:

取值范围:

默认取值:

callback_body_type

char *

可选

参数解释:

发起回调请求的content-type头域的值。支持application/x-www-form-urlencoded、application/json。如果不设置,默认为

application/json。

约束限制:

取值范围:

默认取值:

表17 image_process_configure

参数名称

参数类型

是否必选

描述

image_process_mode

image_process_mode_type

可选

参数解释:

图片处理参数头。

约束限制:

取值范围:

请详见image_process_mode_type

cmds_stylename

char *

可选

参数解释:

图片处理相关参数。

约束限制:

取值范围:

默认取值:

表18 image_process_mode_type

枚举值

说明

obs_image_process_invalid_mode

默认的无效值。

obs_image_process_cmd

图片处理参数以image开头。

obs_image_process_style

图片处理参数以style开头。

表19 server_side_encryption_params

参数名称

参数类型

是否必选

描述

encryption_type

obs_encryption_type

可选

参数解释:

加密类型。

约束限制:

取值范围:

请详见obs_encryption_type

kms_server_side_encryption

char *

可选

参数解释:

使用该头域表示对象使用的服务端加密方式是sse-kms。

约束限制:

取值范围

  • kms
  • aes256

默认取值:

kms_key_id

char *

可选

参数描述:

密钥id。当加密方式为sse-kms且使用指定密钥加密时,需输入密钥id。

约束限制:

当您设置了kms_server_side_encryption且赋值为“kms”,即选择kms加密方式时,才能使用该头域指定加密密钥。

取值范围:

默认取值:

当您选择使用kms加密方式,但未设置此头域时,默认的主密钥将会被使用。如果默认主密钥不存在,系统将默认创建并使用。

ssec_customer_algorithm

char *

可选

参数解释:

该头域表示加密对象使用的算法。

约束限制:

仅sse-c方式下使用该头域。

取值范围:

aes256(指aes256加密算法)

默认取值:

ssec_customer_key

char *

可选

参数解释:

该头域表示加密对象使用的密钥。

约束限制:

仅sse-c方式下使用该头域。

取值范围:

256位密钥的base64编码。

默认取值:

des_ssec_customer_algorithm

char *

可选

参数解释:

该头域表示解密源对象使用的算法。

约束限制:

仅sse-c方式下使用该头域。

取值范围:

默认取值:

des_ssec_customer_key

char *

可选

参数解释:

该头域表示解密源对象使用的密钥。

约束限制:

仅sse-c方式下使用该头域。

取值范围:

默认取值:

表20 obs_encryption_type

枚举值

说明

obs_encryption_kms

使用kms加密方式。

obs_encryption_ssec

使用sse-c加密方式。

表21 obs_response_handler

参数名称

参数类型

是否必选

描述

properties_callback

obs_response_properties_callback *

必选

参数解释:

响应回调函数指针,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中。

约束限制:

complete_callback

obs_response_complete_callback *

必选

参数解释:

结束回调函数指针,可以在这个回调中把obs_statusobs_error_details的内容记录到callback_data(用户自定义回调数据)中。

约束限制:

表22 obs_response_properties_callback

参数名称

参数类型

是否必选

描述

properties

const obs_response_properties*

必选

参数解释:

响应头域中的参数,建议将其内容记录到callback_data(用户自定义回调数据)中。

约束限制:

callback_data

void *

必选

参数解释:

用户自定义回调数据指针。

约束限制:

取值范围:

默认取值

表23 obs_response_complete_callback

参数名称

参数类型

是否必选

描述

status

obs_status

必选

参数解释:

sdk内部的请求状态码。

约束限制:

取值范围:

请详见obs_status

error_details

const obs_error_details*

必选

参数解释:

响应回调函数指针,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中。

约束限制:

callback_data

void *

必选

参数解释:

用户自定义回调数据指针。

约束限制:

取值范围:

默认取值

表24 obs_response_properties

参数名称

参数类型

是否必选

描述

request_id

const char *

可选

参数解释:

由obs创建来唯一确定本次请求的值,可以通过该值来定位问题。

约束限制:

取值范围:

默认取值

request_id2

const char *

可选

参数解释:

帮助定位问题的特殊符号。

约束限制:

取值范围:

默认取值

content_type

const char *

可选

参数解释:

对象的文件类型(mime类型)。content-type(mime)用于标识发送或接收数据的类型,浏览器根据该参数来决定数据的打开方式。

约束限制:

取值范围:

默认取值

content_length

uint64_t

可选

参数解释:

响应消息体的字节长度。

约束限制:

取值范围:

默认取值

server

const char *

可选

参数解释:

http请求中的server头域。

约束限制:

取值范围:

默认取值

etag

const char *

可选

参数解释:

对象的base64编码的128位md5摘要。etag是对象内容的唯一标识,可以通过该值识别对象内容是否有变化。比如上传对象时etag为a,下载对象时etag为b,则说明对象内容发生了变化。etag只反映变化的内容,而不是其元数据。上传的对象或复制操作创建的对象,都有唯一的etag。

约束限制:

当对象是服务端加密的对象时,etag值不是对象的md5值。

取值范围:

长度为32的字符串。

默认取值:

expiration

const char *

可选

参数解释:

对象的详细过期信息。

约束限制:

取值范围:

大于0的整型数,单位:天。

默认取值:

website_redirect_location

const char *

可选

参数解释:

当桶设置了website配置,就可以设置对象元数据的这个属性,website接入点返回301重定向响应,将获取这个对象的请求重定向到该属性指定的桶内另一个对象或一个外部的url,该参数指明对象的重定向地址。

例如,重定向请求到桶内另一对象:

x-obs-website-redirect-location:/anotherpage.html

或重定向请求到一个外部url:

x-obs-website-redirect-location:http://www.example.com/

obs将这个值从头域中取出,保存在对象的元数据“websiteredirectlocation”中。

约束限制:

  • 必须以“/”、“http://”或“https://”开头,长度不超过2kb。
  • obs仅支持为桶根目录下的对象设置重定向,不支持为桶中文件夹下的对象设置重定向。

取值范围:

默认取值:

version_id

const char *

可选

参数解释:

对象的版本号。如果该对象无版本号,则为null。

约束限制:

取值范围:

长度为32的字符串。

默认取值

meta_data_count

int

可选

参数解释:

meta_data数组中的元素个数。

约束限制:

取值范围:

默认取值:

meta_data

const obs_name_value *

可选

参数解释:

对象的自定义元数据。obs支持用户使用以“x-obs-meta-”开头的消息头来加入自定义的元数据,以便对对象进行自定义管理。当用户获取此对象或查询此对象元数据时,加入的自定义元数据将会在返回的消息头中出现。

约束限制:

use_server_side_encryption

char

可选

参数解释:

如果开启了服务端加密,会被置为'\1'。

约束限制:

取值范围:

默认取值:

allow_origin

const char *

可选

参数解释:

当桶设置了cors配置,如果请求中的origin满足服务端的cors配置,则在响应中包含这个origin。

约束限制:

取值范围:

符合cors协议的取值范围。

默认取值:

allow_headers

const char *

可选

参数解释:

当桶设置了cors配置,如果请求的headers满足服务端的cors配置,则在响应中包含这个headers。

约束限制:

最多可填写一个“*”通配符,不支持&、:、<、空格以及中文字符。

取值范围:

符合cors协议的取值范围。

默认取值:

max_age

const char *

可选

参数解释:

桶cors规则中的maxageseconds。maxageseconds指请求来源的客户端可以对跨域请求返回结果的缓存时间。

约束限制:

每个corsrule可以包含至多一个maxageseconds。

取值范围:

大于等于0的整型数,单位:秒。

默认取值:

3000

allow_methods

const char *

可选

参数解释:

当桶设置了cors配置,如果请求的access-control-request-method满足服务端的cors配置,则在响应中包含这条rule中的methods。

约束限制:

取值范围:

  • get
  • put
  • head
  • post
  • delete

取值范围:

expose_headers

const char *

可选

参数解释:

桶cors规则中的exposeheader。exposeheader是指cors规则允许响应中可返回的附加头域,给客户端提供额外的信息。默认情况下浏览器只能访问以下头域:content-length、content-type,如果需要访问其他头域,需要在附加头域中配置。

约束限制:

不支持*、&、:、<、空格以及中文字符。

取值范围:

默认取值:

storage_class

const char *

可选

参数解释:

对象的存储类别。

约束限制:

仅当对象为非标准存储类别时,会返回此头域。

取值范围:

  • warm(指低频存储)
  • cold(指归档存储)
  • deep_archive(指深度归档存储)

默认取值:

server_side_encryption

const char *

可选

参数解释:

该头域表示服务端的加密方式。

示例:x-obs-server-side-encryption:kms

约束限制:

如果服务端加密是sse-kms方式,响应包含该头域。

取值范围:

  • kms(指sse-kms加密方式)
  • obs(指sse-obs加密方式)

默认取值:

kms_key_id

const char *

可选

参数描述:

密钥id。当加密方式为sse-kms且使用指定密钥加密时,需输入密钥id。

约束限制:

当您设置了server_side_encryption且赋值为“kms”,即选择kms加密方式时,才能使用该头域指定加密密钥。

取值范围:

默认取值:

当您选择使用kms加密方式,但未设置此头域时,默认的主密钥将会被使用。如果默认主密钥不存在,系统将默认创建并使用。

customer_algorithm

const char *

可选

参数解释:

如果服务端加密是sse-c方式,响应包含该头域,该头域表示解密使用的算法。

约束限制:

取值范围:

aes256(指aes256解密算法)

默认取值:

customer_key_md5

const char *

可选

参数解释:

如果服务端加密是sse-c方式,响应包含该头域,该头域表示解密使用的密钥的md5值。

约束限制:

由密钥值经过md5加密再经过base64编码后得到,示例:4xvb3tbntn tieva0/fgaq==。

取值范围:

密钥id md5的base64值。

默认取值:

bucket_location

const char *

可选

参数解释:

桶的区域位置信息。

约束限制:

取值范围:

默认取值:

obs_version

const char *

可选

参数解释:

桶所在的obs服务版本号。

约束限制:

取值范围:

  • 3.0:最新版本的桶。
  • --:表示老版本的桶。

默认取值:

restore

const char *

可选

参数解释:

标识对象的恢复状态。

示例:正在恢复ongoing-request="true";已恢复 ongoing-request="false", expiry-date="wed, 7 nov 2012 00:00:00 gmt"。其中expiry-date表示对象恢复后的失效时间。

约束限制:

仅当对象为归档或深度归档存储类别,并且处于正在恢复或已经恢复时,会返回此头域。

取值范围:

默认取值:

obs_object_type

const char *

可选

参数解释:

对象的类型。

约束限制:

仅当对象为非normal对象时,会返回此头域。

取值范围:

appendable

默认取值:

obs_next_append_position

const char *

可选

参数解释:

指明下一次请求应该提供的position。

约束限制:

仅当对象为appendable对象时,会返回此头域。

取值范围:

默认取值:

obs_head_epid

const char *

可选

参数解释:

当前桶的企业项目id,开通企业项目的用户可以从企业项目服务获取。

约束限制:

格式为uuid,未开通企业项目的用户可以不带该头域。

取值范围:

默认取值:

reserved_indicator

const char *

可选

参数解释:

帮助定位问题的特殊符号。

约束限制:

取值范围:

默认取值:

表25 obs_error_details

参数名称

参数类型

描述

message

const char*

参数解释:

错误响应消息体xml中具体错误更全面、详细的英文解释。

约束限制:

取值范围:

取值范围可参见错误码

默认取值:

resource

const char*

参数解释:

该错误相关的桶或对象资源。

约束限制:

取值范围:

默认取值:

further_details

const char*

参数解释:

错误响应消息体xml中的furtherdetails元素的值。

约束限制:

取值范围:

默认取值:

extra_details_count

int

参数解释:

错误响应消息体xml中的其他元素的数量。

约束限制:

取值范围:

默认取值:

extra_details

obs_name_value*

参数解释:

错误响应消息体xml中的其他元素的值。

约束限制:

error_headers_count

int

参数解释:

error_headers的头域数量。

约束限制:

取值范围:

默认取值:

error_headers

char**

参数解释:

响应头域中包含error的所有头域。

约束限制:

取值范围:

默认取值:

表26 obs_name_value

参数名称

参数类型

是否必选

描述

name

char *

可选

参数解释:

属性的键。

约束限制:

取值范围:

默认取值:

value

char *

可选

参数解释:

属性的值。

约束限制:

取值范围:

默认取值:

表27 obs_status

枚举值

说明

obs_status_ok

请求成功。

obs_status_initcurlfailed

初始化curl失败。

obs_status_internalerror

内部错误。

obs_status_outofmemory

本地环境内存不足。

obs_status_failedtoiinitializerequest

初始化请求失败。

obs_status_connectionfailed

网络连接失败。

obs_status_xmlparsefailure

xml解析失败。

obs_status_namelookuperror

域名解析失败。

obs_status_failedtoconnect

无法连接到服务端。

obs_status_partialfile

网络传输。

obs_status_invalidparameter

参数非法。

obs_status_notoken

当前并发数已经超过最大并发数(默认值1000),通过set_online_request_max_count函数去调整最大并发数。

obs_status_openfilefailed

打开文件失败。

obs_status_accessdenied

请求被拒绝。

obs_status_malformedpolicy

请求policy格式不正确。

obs_status_malformedxml

请求xml格式不正确。

obs_status_methodnotallowed

请求方法不允许。

obs_status_signaturedoesnotmatch

签名不匹配,检查ak、sk、token是否对应或有误。

obs_status_serviceunavailable

服务端异常。

obs_status_slowdown

请求频率过快。

代码示例:设置对象mime类型

以下示例展示在文件上传对象时如何设置对象mime类型(如果不设置对象mime类型,c sdk会根据上传对象的后缀名自动判断对象mime类型,如.xml判断为application/xml文件;.html判断为text/html文件。):
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include"esdkobs.h"
#include
#include
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_statusresponse_properties_callback(constobs_response_properties*properties,void*callback_data);
intput_file_data_callback(intbuffer_size,char*buffer,
void*callback_data);
voidput_file_complete_callback(obs_statusstatus,
constobs_error_details*error,
void*callback_data);
typedefstructput_file_object_callback_data
{
file*infile;
uint64_tcontent_length;
obs_statusret_status;
}put_file_object_callback_data;
uint64_topen_file_and_get_length(char*localfile,put_file_object_callback_data*data);
intmain()
{
// 以下代码以文件上传为例展示如何设置对象mime类型
// 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源
obs_initialize(obs_init_all);
obs_optionsoptions;
// 创建并初始化options,该参数包括访问域名(host_name)、访问密钥(access_key_id和acces_key_secret)、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
init_obs_options(&options);
// host_name填写桶所在的endpoint, 此处以华北-北京四为例,其他地区请按实际情况填写。
options.bucket_options.host_name="obs.cn-north-4.myhuaweicloud.com";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全
// 本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量access_key_id和secret_access_key
options.bucket_options.access_key=getenv("access_key_id");
options.bucket_options.secret_access_key=getenv("secret_access_key");
// 填写bucket名称,例如example-bucket-name
char*bucketname="example-bucket-name";
options.bucket_options.bucket_name=bucketname;
// 初始化上传对象属性
obs_put_propertiesput_properties;
init_put_properties(&put_properties);
// 上传对象名
char*key="example_put_file_test";
// 上传的文件
charfile_name[256]="./example_local_file_test.txt";
uint64_tcontent_length=0;
// 初始化存储上传数据的结构体
put_file_object_callback_datadata;
memset(&data,0,sizeof(put_file_object_callback_data));
// 打开文件,并获取文件长度
content_length=open_file_and_get_length(file_name,&data);
// 设置回调函数
obs_put_object_handlerputobjecthandler=
{
{&response_properties_callback,&put_file_complete_callback},
&put_file_data_callback
};
// 设置mime类型
put_properties.content_type="text/plain";
put_object(&options,key,content_length,&put_properties,0,&putobjecthandler,&data);
if(obs_status_ok==data.ret_status){
printf("put object from file successfully. \n");
}
else
{
printf("put object failed(%s).\n",
obs_get_status_name(data.ret_status));
}
if(data.infile!=null){
fclose(data.infile);
}
// 释放分配的全局资源
obs_deinitialize();
}
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_statusresponse_properties_callback(constobs_response_properties*properties,void*callback_data)
{
if(properties==null)
{
printf("error! obs_response_properties is null!");
if(callback_data!=null)
{
obs_sever_callback_data*data=(obs_sever_callback_data*)callback_data;
printf("server_callback buf is %s, len is %llu",
data->buffer,data->buffer_len);
returnobs_status_ok;
}
else{
printf("error! obs_sever_callback_data is null!");
returnobs_status_ok;
}
}
// 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
print_nonnull("request_id",request_id);
print_nonnull("request_id2",request_id2);
print_nonnull("content_type",content_type);
if(properties->content_length){
printf("content_length: %llu\n",properties->content_length);
}
print_nonnull("server",server);
print_nonnull("etag",etag);
print_nonnull("expiration",expiration);
print_nonnull("website_redirect_location",website_redirect_location);
print_nonnull("version_id",version_id);
print_nonnull("allow_origin",allow_origin);
print_nonnull("allow_headers",allow_headers);
print_nonnull("max_age",max_age);
print_nonnull("allow_methods",allow_methods);
print_nonnull("expose_headers",expose_headers);
print_nonnull("storage_class",storage_class);
print_nonnull("server_side_encryption",server_side_encryption);
print_nonnull("kms_key_id",kms_key_id);
print_nonnull("customer_algorithm",customer_algorithm);
print_nonnull("customer_key_md5",customer_key_md5);
print_nonnull("bucket_location",bucket_location);
print_nonnull("obs_version",obs_version);
print_nonnull("restore",restore);
print_nonnull("obs_object_type",obs_object_type);
print_nonnull("obs_next_append_position",obs_next_append_position);
print_nonnull("obs_head_epid",obs_head_epid);
print_nonnull("reserved_indicator",reserved_indicator);
inti;
for(i=0;i<properties->meta_data_count;i){
printf("x-obs-meta-%s: %s\n",properties->meta_data[i].name,
properties->meta_data[i].value);
}
returnobs_status_ok;
}
intput_file_data_callback(intbuffer_size,char*buffer,
void*callback_data)
{
put_file_object_callback_data*data=
(put_file_object_callback_data*)callback_data;
intret=0;
if(data->content_length){
inttoread=((data->content_length>(unsigned)buffer_size)?
(unsigned)buffer_size:data->content_length);
ret=fread(buffer,1,toread,data->infile);
}
uint64_toriginalcontentlength=data->content_length;
data->content_length-=ret;
if(data->content_length){
printf("%llu bytes remaining ",(unsignedlonglong)data->content_length);
printf("(%d%% complete) ...\n",
(int)(((originalcontentlength-data->content_length)*100)/originalcontentlength));
}
returnret;
}
voidput_file_complete_callback(obs_statusstatus,
constobs_error_details*error,
void*callback_data)
{
put_file_object_callback_data*data=(put_file_object_callback_data*)callback_data;
data->ret_status=status;
}
uint64_topen_file_and_get_length(char*localfile,put_file_object_callback_data*data)
{
uint64_tcontent_length=0;
constchar*body=0;
if(!content_length)
{
structstatstatbuf;
if(stat(localfile,&statbuf)==-1)
{
fprintf(stderr,"\nerror: failed to stat file %s: ",
localfile);
return0;
}
content_length=statbuf.st_size;
}
if(!(data->infile=fopen(localfile,"rb")))
{
fprintf(stderr,"\nerror: failed to open input file %s: ",
localfile);
return0;
}
data->content_length=content_length;
returncontent_length;
}

代码示例:设置对象存储类别

以下示例展示如何在上传对象时设置对象存储类别:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include"esdkobs.h"
#include
#include
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_statusresponse_properties_callback(constobs_response_properties*properties,void*callback_data);
intput_file_data_callback(intbuffer_size,char*buffer,
void*callback_data);
voidput_file_complete_callback(obs_statusstatus,
constobs_error_details*error,
void*callback_data);
typedefstructput_file_object_callback_data
{
file*infile;
uint64_tcontent_length;
obs_statusret_status;
}put_file_object_callback_data;
uint64_topen_file_and_get_length(char*localfile,put_file_object_callback_data*data);
intmain()
{
// 以下示例展示如何在上传对象时设置对象存储类别:
// 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
obs_initialize(obs_init_all);
obs_optionsoptions;
// 创建并初始化options,该参数包括访问域名(host_name)、访问密钥(access_key_id和acces_key_secret)、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
init_obs_options(&options);
// host_name填写桶所在的endpoint, 此处以华北-北京四为例,其他地区请按实际情况填写。
options.bucket_options.host_name="obs.cn-north-4.myhuaweicloud.com";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
// 本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量access_key_id和secret_access_key。
options.bucket_options.access_key=getenv("access_key_id");
options.bucket_options.secret_access_key=getenv("secret_access_key");
// 填写bucket名称,例如example-bucket-name。
char*bucketname="example-bucket-name";
options.bucket_options.bucket_name=bucketname;
//设置存储类别为归档存储
options.bucket_options.storage_class=obs_storage_class_glacier;
// 初始化上传对象属性
obs_put_propertiesput_properties;
init_put_properties(&put_properties);
// 上传对象名
char*key="example_put_file_test_cold";
// 上传的文件
charfile_name[256]="./example_local_file_test.txt";
uint64_tcontent_length=0;
// 初始化存储上传数据的结构体
put_file_object_callback_datadata;
memset(&data,0,sizeof(put_file_object_callback_data));
// 打开文件,并获取文件长度
content_length=open_file_and_get_length(file_name,&data);
// 设置回调函数
obs_put_object_handlerputobjecthandler=
{
{&response_properties_callback,&put_file_complete_callback},
&put_file_data_callback
};
put_object(&options,key,content_length,&put_properties,0,&putobjecthandler,&data);
if(obs_status_ok==data.ret_status){
printf("put object from file successfully. \n");
}
else
{
printf("put object failed(%s).\n",
obs_get_status_name(data.ret_status));
}
if(data.infile!=null){
fclose(data.infile);
}
// 释放分配的全局资源
obs_deinitialize();
}
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_statusresponse_properties_callback(constobs_response_properties*properties,void*callback_data)
{
if(properties==null)
{
printf("error! obs_response_properties is null!");
if(callback_data!=null)
{
obs_sever_callback_data*data=(obs_sever_callback_data*)callback_data;
printf("server_callback buf is %s, len is %llu",
data->buffer,data->buffer_len);
returnobs_status_ok;
}
else{
printf("error! obs_sever_callback_data is null!");
returnobs_status_ok;
}
}
// 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
print_nonnull("request_id",request_id);
print_nonnull("request_id2",request_id2);
print_nonnull("content_type",content_type);
if(properties->content_length){
printf("content_length: %llu\n",properties->content_length);
}
print_nonnull("server",server);
print_nonnull("etag",etag);
print_nonnull("expiration",expiration);
print_nonnull("website_redirect_location",website_redirect_location);
print_nonnull("version_id",version_id);
print_nonnull("allow_origin",allow_origin);
print_nonnull("allow_headers",allow_headers);
print_nonnull("max_age",max_age);
print_nonnull("allow_methods",allow_methods);
print_nonnull("expose_headers",expose_headers);
print_nonnull("storage_class",storage_class);
print_nonnull("server_side_encryption",server_side_encryption);
print_nonnull("kms_key_id",kms_key_id);
print_nonnull("customer_algorithm",customer_algorithm);
print_nonnull("customer_key_md5",customer_key_md5);
print_nonnull("bucket_location",bucket_location);
print_nonnull("obs_version",obs_version);
print_nonnull("restore",restore);
print_nonnull("obs_object_type",obs_object_type);
print_nonnull("obs_next_append_position",obs_next_append_position);
print_nonnull("obs_head_epid",obs_head_epid);
print_nonnull("reserved_indicator",reserved_indicator);
inti;
for(i=0;i<properties->meta_data_count;i){
printf("x-obs-meta-%s: %s\n",properties->meta_data[i].name,
properties->meta_data[i].value);
}
returnobs_status_ok;
}
intput_file_data_callback(intbuffer_size,char*buffer,
void*callback_data)
{
put_file_object_callback_data*data=
(put_file_object_callback_data*)callback_data;
intret=0;
if(data->content_length){
inttoread=((data->content_length>(unsigned)buffer_size)?
(unsigned)buffer_size:data->content_length);
ret=fread(buffer,1,toread,data->infile);
}
uint64_toriginalcontentlength=data->content_length;
data->content_length-=ret;
if(data->content_length){
printf("%llu bytes remaining ",(unsignedlonglong)data->content_length);
printf("(%d%% complete) ...\n",
(int)(((originalcontentlength-data->content_length)*100)/originalcontentlength));
}
returnret;
}
voidput_file_complete_callback(obs_statusstatus,
constobs_error_details*error,
void*callback_data)
{
put_file_object_callback_data*data=(put_file_object_callback_data*)callback_data;
data->ret_status=status;
}
uint64_topen_file_and_get_length(char*localfile,put_file_object_callback_data*data)
{
uint64_tcontent_length=0;
constchar*body=0;
if(!content_length)
{
structstatstatbuf;
if(stat(localfile,&statbuf)==-1)
{
fprintf(stderr,"\nerror: failed to stat file %s: ",
localfile);
return0;
}
content_length=statbuf.st_size;
}
if(!(data->infile=fopen(localfile,"rb")))
{
fprintf(stderr,"\nerror: failed to open input file %s: ",
localfile);
return0;
}
data->content_length=content_length;
returncontent_length;
}

代码示例:设置对象自定义元数据

以下示例展示如何在上传对象时设置对象自定义元数据:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include"esdkobs.h"
#include
#include
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_statusresponse_properties_callback(constobs_response_properties*properties,void*callback_data);
intput_file_data_callback(intbuffer_size,char*buffer,
void*callback_data);
voidput_file_complete_callback(obs_statusstatus,
constobs_error_details*error,
void*callback_data);
typedefstructput_file_object_callback_data
{
file*infile;
uint64_tcontent_length;
obs_statusret_status;
}put_file_object_callback_data;
uint64_topen_file_and_get_length(char*localfile,put_file_object_callback_data*data);
intmain()
{
// 以下示例展示如何在上传对象时设置对象自定义元数据:
// 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
obs_initialize(obs_init_all);
obs_optionsoptions;
// 创建并初始化options,该参数包括访问域名(host_name)、访问密钥(access_key_id和acces_key_secret)、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
init_obs_options(&options);
// host_name填写桶所在的endpoint, 此处以华北-北京四为例,其他地区请按实际情况填写。
options.bucket_options.host_name="obs.cn-north-4.myhuaweicloud.com";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
// 本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量access_key_id和secret_access_key。
options.bucket_options.access_key=getenv("access_key_id");
options.bucket_options.secret_access_key=getenv("secret_access_key");
// 填写bucket名称,例如example-bucket-name。
char*bucketname="example-bucket-name";
options.bucket_options.bucket_name=bucketname;
// 初始化上传对象属性
obs_put_propertiesput_properties;
init_put_properties(&put_properties);
// 上传对象名
char*key="example_put_file_test_with_matadatas";
// 上传的文件
charfile_name[256]="./example_local_file_test.txt";
uint64_tcontent_length=0;
// 初始化存储上传数据的结构体
put_file_object_callback_datadata;
memset(&data,0,sizeof(put_file_object_callback_data));
// 打开文件,并获取文件长度
content_length=open_file_and_get_length(file_name,&data);
// 设置回调函数
obs_put_object_handlerputobjecthandler=
{
{&response_properties_callback,&put_file_complete_callback},
&put_file_data_callback
};
//设置自定义元数据
obs_name_valueexample_matadatas[2]={0};
example_matadatas[0].name="example-property1";
example_matadatas[0].value="example-property-value1";
example_matadatas[1].name="example-property2";
example_matadatas[1].value="example-property-value2";
put_properties.meta_data=&example_matadatas;
//设置自定义元数据数量
put_properties.meta_data_count=2;
put_object(&options,key,content_length,&put_properties,0,&putobjecthandler,&data);
if(obs_status_ok==data.ret_status){
printf("put object from file successfully. \n");
}
else
{
printf("put object failed(%s).\n",
obs_get_status_name(data.ret_status));
}
if(data.infile!=null){
fclose(data.infile);
}
// 释放分配的全局资源
obs_deinitialize();
}
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_statusresponse_properties_callback(constobs_response_properties*properties,void*callback_data)
{
if(properties==null)
{
printf("error! obs_response_properties is null!");
if(callback_data!=null)
{
obs_sever_callback_data*data=(obs_sever_callback_data*)callback_data;
printf("server_callback buf is %s, len is %llu",
data->buffer,data->buffer_len);
returnobs_status_ok;
}
else{
printf("error! obs_sever_callback_data is null!");
returnobs_status_ok;
}
}
// 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
print_nonnull("request_id",request_id);
print_nonnull("request_id2",request_id2);
print_nonnull("content_type",content_type);
if(properties->content_length){
printf("content_length: %llu\n",properties->content_length);
}
print_nonnull("server",server);
print_nonnull("etag",etag);
print_nonnull("expiration",expiration);
print_nonnull("website_redirect_location",website_redirect_location);
print_nonnull("version_id",version_id);
print_nonnull("allow_origin",allow_origin);
print_nonnull("allow_headers",allow_headers);
print_nonnull("max_age",max_age);
print_nonnull("allow_methods",allow_methods);
print_nonnull("expose_headers",expose_headers);
print_nonnull("storage_class",storage_class);
print_nonnull("server_side_encryption",server_side_encryption);
print_nonnull("kms_key_id",kms_key_id);
print_nonnull("customer_algorithm",customer_algorithm);
print_nonnull("customer_key_md5",customer_key_md5);
print_nonnull("bucket_location",bucket_location);
print_nonnull("obs_version",obs_version);
print_nonnull("restore",restore);
print_nonnull("obs_object_type",obs_object_type);
print_nonnull("obs_next_append_position",obs_next_append_position);
print_nonnull("obs_head_epid",obs_head_epid);
print_nonnull("reserved_indicator",reserved_indicator);
inti;
for(i=0;i<properties->meta_data_count;i){
printf("x-obs-meta-%s: %s\n",properties->meta_data[i].name,
properties->meta_data[i].value);
}
returnobs_status_ok;
}
intput_file_data_callback(intbuffer_size,char*buffer,
void*callback_data)
{
put_file_object_callback_data*data=
(put_file_object_callback_data*)callback_data;
intret=0;
if(data->content_length){
inttoread=((data->content_length>(unsigned)buffer_size)?
(unsigned)buffer_size:data->content_length);
ret=fread(buffer,1,toread,data->infile);
}
uint64_toriginalcontentlength=data->content_length;
data->content_length-=ret;
if(data->content_length){
printf("%llu bytes remaining ",(unsignedlonglong)data->content_length);
printf("(%d%% complete) ...\n",
(int)(((originalcontentlength-data->content_length)*100)/originalcontentlength));
}
returnret;
}
voidput_file_complete_callback(obs_statusstatus,
constobs_error_details*error,
void*callback_data)
{
put_file_object_callback_data*data=(put_file_object_callback_data*)callback_data;
data->ret_status=status;
}
uint64_topen_file_and_get_length(char*localfile,put_file_object_callback_data*data)
{
uint64_tcontent_length=0;
constchar*body=0;
if(!content_length)
{
structstatstatbuf;
if(stat(localfile,&statbuf)==-1)
{
fprintf(stderr,"\nerror: failed to stat file %s: ",
localfile);
return0;
}
content_length=statbuf.st_size;
}
if(!(data->infile=fopen(localfile,"rb")))
{
fprintf(stderr,"\nerror: failed to open input file %s: ",
localfile);
return0;
}
data->content_length=content_length;
returncontent_length;
}

相关链接

  • 关于上传对象-put上传的api说明,请参见put上传
  • 更多关于上传对象的代码示例,请参见。
  • 上传对象过程中返回的错误码含义、问题原因及处理措施可参考obs错误码
  • 上传对象常见问题请参见上传对象失败

相关文档

网站地图