更新时间:2023-11-08 gmt 08:00
流式上传-九游平台
流式上传使用obsputobjectwithdatarequest作为对象的数据源。您可以通过putobject上传您的数据流到obs。以下代码展示了如何进行流式上传:
上传字符串
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:[@"hello" datausingencoding:nsutf8stringencoding]]; // 上传文件进度 request.uploadprogressblock = ^(int64_t bytessent, int64_t totalbytessent, int64_t totalbytesexpectedtosend) { nslog(@"%0.1f%%",(float)floor(totalbytessent*10000/totalbytesexpectedtosend)/100); }; [client putobject:request completionhandler:^(obsputobjectresponse *response, nserror *error){ nslog(@"%@",response); }];
上传网络流
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" uploaddataurl:[nsurl urlwithstring:@"dateurl"]]; // 上传文件进度 request.uploadprogressblock = ^(int64_t bytessent, int64_t totalbytessent, int64_t totalbytesexpectedtosend) { nslog(@"%0.1f%%",(float)floor(totalbytessent*10000/totalbytesexpectedtosend)/100); }; [client putobject:request completionhandler:^(obsputobjectresponse *response, nserror *error){ nslog(@"%@",response); }];
上传文件流
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"]; // 文件流上传 nsdata *uploaddata = [nsdata datawithcontentsoffile:filepath]; obsputobjectwithdatarequest *request = [[obsputobjectwithdatarequest alloc]initwithbucketname:@"bucketname" objectkey:@"test/image1" uploaddata:uploaddata]; // 上传文件进度 request.uploadprogressblock = ^(int64_t bytessent, int64_t totalbytessent, int64_t totalbytesexpectedtosend) { nslog(@"%0.1f%%",(float)floor(totalbytessent*10000/totalbytesexpectedtosend)/100); }; [client putobject:request completionhandler:^(obsputobjectresponse *response, nserror *error){ nslog(@"%@",response); }];

- 推荐使用的形式上传本地文件,而不是文件流形式。
- 大文件上传建议使用分段上传。
- 上传的内容大小不能超过5gb。
父主题:
相关文档
意见反馈
文档内容是否对您有帮助?
提交成功!非常感谢您的反馈,我们会继续努力做到更好!
您可在查看反馈及问题处理状态。
系统繁忙,请稍后重试
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨