开发者社区> 问答> 正文

OSS如何实现快速搭建移动应用直传服务(二)?

用户如何体验自己的APP上传应用服务器

  1. 把程序部署起来后,记下应用服务器地址如http://abc.com:8080 将示例程序里面的应用服务器修改成上述地址。
  2. 选择数据要上传到哪个Bcuket及区域,修改示例APP程序里面相应Bucket及区域。
  3. 单击[backcolor=transparent]设置按钮,将配置加载。
  4. 选择图片,设置上传OSS文件名,上传。这样就可以在Android上体验OSS服务了,就能通Android示例程序将数据直接上传到OSS了。
  5. 上传成功后,可以看一下数据是否在OSS上了。


核心代码解析



OSS初始化


下面讲解如何利用Android/iOS SDK跟自己的应用服务器,请求Token。
  • [backcolor=transparent]Android版本
  1. [backcolor=transparent]//初始化一个OssService用来上传
  2. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]OssService[backcolor=transparent] initOSS[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] endpoint[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] bucket[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]UIDisplayer[backcolor=transparent] displayer[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  3. [backcolor=transparent]    [backcolor=transparent]OSSCredentialProvider[backcolor=transparent] credentialProvider[backcolor=transparent];
  4. [backcolor=transparent]    [backcolor=transparent]//使用自己的获取STSToken的类
  5. [backcolor=transparent]//从应用服务器控件里面读取应用服务器地址
  6. [backcolor=transparent]    [backcolor=transparent]String[backcolor=transparent] stsServer [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent](([backcolor=transparent]EditText[backcolor=transparent])[backcolor=transparent] findViewById[backcolor=transparent]([backcolor=transparent]R[backcolor=transparent].[backcolor=transparent]id[backcolor=transparent].[backcolor=transparent]stsserver[backcolor=transparent])).[backcolor=transparent]getText[backcolor=transparent]().[backcolor=transparent]toString[backcolor=transparent]();
  7. [backcolor=transparent]    [backcolor=transparent]//STSGetter类,封装如何跟从应用服务器取数据,必须继承于OSSFederationCredentialProvider这个类。 取Token这个取决于您所写的APP跟应用服务器数据的协议设计。
  8. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]stsServer [backcolor=transparent].[backcolor=transparent]equals[backcolor=transparent]([backcolor=transparent]""[backcolor=transparent]))[backcolor=transparent] [backcolor=transparent]{
  9. [backcolor=transparent]        credentialProvider [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]STSGetter[backcolor=transparent]();
  10. [backcolor=transparent]    [backcolor=transparent]}[backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  11. [backcolor=transparent]        credentialProvider [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]STSGetter[backcolor=transparent]([backcolor=transparent]stsServer[backcolor=transparent]);
  12. [backcolor=transparent]    [backcolor=transparent]}
  13. [backcolor=transparent]//获取控件上的bucket名字
  14. [backcolor=transparent]    bucket [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent](([backcolor=transparent]EditText[backcolor=transparent])[backcolor=transparent] findViewById[backcolor=transparent]([backcolor=transparent]R[backcolor=transparent].[backcolor=transparent]id[backcolor=transparent].[backcolor=transparent]bucketname[backcolor=transparent])).[backcolor=transparent]getText[backcolor=transparent]().[backcolor=transparent]toString[backcolor=transparent]();
  15. [backcolor=transparent]//初始化OSSClient
  16. [backcolor=transparent]    [backcolor=transparent]ClientConfiguration[backcolor=transparent] conf [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]ClientConfiguration[backcolor=transparent]();
  17. [backcolor=transparent]    conf[backcolor=transparent].[backcolor=transparent]setConnectionTimeout[backcolor=transparent]([backcolor=transparent]15[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]1000[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 连接超时,默认15秒
  18. [backcolor=transparent]    conf[backcolor=transparent].[backcolor=transparent]setSocketTimeout[backcolor=transparent]([backcolor=transparent]15[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]1000[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// socket超时,默认15秒
  19. [backcolor=transparent]    conf[backcolor=transparent].[backcolor=transparent]setMaxConcurrentRequest[backcolor=transparent]([backcolor=transparent]5[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 最大并发请求数,默认5个
  20. [backcolor=transparent]    conf[backcolor=transparent].[backcolor=transparent]setMaxErrorRetry[backcolor=transparent]([backcolor=transparent]2[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 失败后最大重试次数,默认2次
  21. [backcolor=transparent]    OSS oss [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OSSClient[backcolor=transparent]([backcolor=transparent]getApplicationContext[backcolor=transparent](),[backcolor=transparent] endpoint[backcolor=transparent],[backcolor=transparent] credentialProvider[backcolor=transparent],[backcolor=transparent] conf[backcolor=transparent]);
  22. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OssService[backcolor=transparent]([backcolor=transparent]oss[backcolor=transparent],[backcolor=transparent] bucket[backcolor=transparent],[backcolor=transparent] displayer[backcolor=transparent]);
  23. [backcolor=transparent]}
  • [backcolor=transparent]iOS版本
  1. [backcolor=transparent]// 初始化一个OSSClient实例
  2. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]ossInit [backcolor=transparent]{
  3. [backcolor=transparent]    [backcolor=transparent]// 构造一个获取STSToken的凭证提供器
  4. [backcolor=transparent]    id[backcolor=transparent]<[backcolor=transparent]OSSCredentialProvider[backcolor=transparent]>[backcolor=transparent] credential [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[[backcolor=transparent]OSSFederationCredentialProvider[backcolor=transparent] alloc[backcolor=transparent]][backcolor=transparent] initWithFederationTokenGetter[backcolor=transparent]:^[backcolor=transparent]OSSFederationToken[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]{
  5. [backcolor=transparent]        [backcolor=transparent]// 实现一个函数,同步返回从server获取到的STSToken
  6. [backcolor=transparent]        [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] getFederationToken[backcolor=transparent]];
  7. [backcolor=transparent]    [backcolor=transparent]}];
  8. [backcolor=transparent]    [backcolor=transparent]// 用endpoint、凭证提供器初始化一个OSSClient
  9. [backcolor=transparent]    client [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[[backcolor=transparent]OSSClient[backcolor=transparent] alloc[backcolor=transparent]][backcolor=transparent] initWithEndpoint[backcolor=transparent]:[backcolor=transparent]endPoint credentialProvider[backcolor=transparent]:[backcolor=transparent]credential[backcolor=transparent]];
  10. [backcolor=transparent]}


移动应用从应用服务器取Token


具体APP从应用服务器取Token的方法,必须写到public OSSFederationToken getFederationToken() { }这个函数里面。

注意:这个函数的逻辑依赖于可以自己设定,但是最终结果必须返回这样一个变量 return new OSSFederationToken(ak, sk, token, expiration),其中ak, sk, token, expiration 必须是从应用服务器返回的Body获取的。

在本示例中,用户可以自定义自己的移动应用跟自己应用服务器之前的协议。
  • [backcolor=transparent]Android版本
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]OSSFederationToken[backcolor=transparent] getFederationToken[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent]String[backcolor=transparent] stsJson[backcolor=transparent];
  3. [backcolor=transparent]    [backcolor=transparent]OkHttpClient[backcolor=transparent] client [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OkHttpClient[backcolor=transparent]();
  4. [backcolor=transparent]    [backcolor=transparent]Request[backcolor=transparent] request [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Request[backcolor=transparent].[backcolor=transparent]Builder[backcolor=transparent]().[backcolor=transparent]url[backcolor=transparent]([backcolor=transparent]stsServer[backcolor=transparent]).[backcolor=transparent]build[backcolor=transparent]();
  5. [backcolor=transparent]    [backcolor=transparent]try[backcolor=transparent] [backcolor=transparent]{
  6. [backcolor=transparent]        [backcolor=transparent]Response[backcolor=transparent] response [backcolor=transparent]=[backcolor=transparent] client[backcolor=transparent].[backcolor=transparent]newCall[backcolor=transparent]([backcolor=transparent]request[backcolor=transparent]).[backcolor=transparent]execute[backcolor=transparent]();
  7. [backcolor=transparent]        [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]response[backcolor=transparent].[backcolor=transparent]isSuccessful[backcolor=transparent]())[backcolor=transparent] [backcolor=transparent]{
  8. [backcolor=transparent]            stsJson [backcolor=transparent]=[backcolor=transparent] response[backcolor=transparent].[backcolor=transparent]body[backcolor=transparent]().[backcolor=transparent]string[backcolor=transparent]();
  9. [backcolor=transparent]        [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  10. [backcolor=transparent]            [backcolor=transparent]throw[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]IOException[backcolor=transparent]([backcolor=transparent]"Unexpected code "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] response[backcolor=transparent]);
  11. [backcolor=transparent]        [backcolor=transparent]}
  12. [backcolor=transparent]    [backcolor=transparent]}
  13. [backcolor=transparent]    [backcolor=transparent]catch[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]IOException[backcolor=transparent] e[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  14. [backcolor=transparent]        e[backcolor=transparent].[backcolor=transparent]printStackTrace[backcolor=transparent]();
  15. [backcolor=transparent]        [backcolor=transparent]Log[backcolor=transparent].[backcolor=transparent]e[backcolor=transparent]([backcolor=transparent]"GetSTSTokenFail"[backcolor=transparent],[backcolor=transparent] e[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
  16. [backcolor=transparent]        [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent];
  17. [backcolor=transparent]    [backcolor=transparent]}
  18. [backcolor=transparent]    [backcolor=transparent]try[backcolor=transparent] [backcolor=transparent]{
  19. [backcolor=transparent]        [backcolor=transparent]JSONObject[backcolor=transparent] jsonObjs [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]JSONObject[backcolor=transparent]([backcolor=transparent]stsJson[backcolor=transparent]);
  20. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] ak [backcolor=transparent]=[backcolor=transparent] jsonObjs[backcolor=transparent].[backcolor=transparent]getString[backcolor=transparent]([backcolor=transparent]"AccessKeyId"[backcolor=transparent]);
  21. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] sk [backcolor=transparent]=[backcolor=transparent] jsonObjs[backcolor=transparent].[backcolor=transparent]getString[backcolor=transparent]([backcolor=transparent]"AccessKeySecret"[backcolor=transparent]);
  22. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] token [backcolor=transparent]=[backcolor=transparent] jsonObjs[backcolor=transparent].[backcolor=transparent]getString[backcolor=transparent]([backcolor=transparent]"SecurityToken"[backcolor=transparent]);
  23. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] expiration [backcolor=transparent]=[backcolor=transparent] jsonObjs[backcolor=transparent].[backcolor=transparent]getString[backcolor=transparent]([backcolor=transparent]"Expiration"[backcolor=transparent]);
  24. [backcolor=transparent]        [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OSSFederationToken[backcolor=transparent]([backcolor=transparent]ak[backcolor=transparent],[backcolor=transparent] sk[backcolor=transparent],[backcolor=transparent] token[backcolor=transparent],[backcolor=transparent] expiration[backcolor=transparent]);
  25. [backcolor=transparent]    [backcolor=transparent]}
  26. [backcolor=transparent]    [backcolor=transparent]catch[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]JSONException[backcolor=transparent] e[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  27. [backcolor=transparent]        [backcolor=transparent]Log[backcolor=transparent].[backcolor=transparent]e[backcolor=transparent]([backcolor=transparent]"GetSTSTokenFail"[backcolor=transparent],[backcolor=transparent] e[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
  28. [backcolor=transparent]        e[backcolor=transparent].[backcolor=transparent]printStackTrace[backcolor=transparent]();
  29. [backcolor=transparent]        [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent];
  30. [backcolor=transparent]    [backcolor=transparent]}}
  • [backcolor=transparent]iOS版本
  1. [backcolor=transparent]NSURL [backcolor=transparent]*[backcolor=transparent] url [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSURL [backcolor=transparent]URLWithString[backcolor=transparent]:[backcolor=transparent]STSServer[backcolor=transparent]];
  2. [backcolor=transparent]NSURLRequest[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] request [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSURLRequest[backcolor=transparent] requestWithURL[backcolor=transparent]:[backcolor=transparent]url[backcolor=transparent]];
  3. [backcolor=transparent]OSSTaskCompletionSource[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] tcs [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]OSSTaskCompletionSource[backcolor=transparent] taskCompletionSource[backcolor=transparent]];
  4. [backcolor=transparent]NSURLSession[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] session [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSURLSession[backcolor=transparent] sharedSession[backcolor=transparent]];
  5. [backcolor=transparent]NSURLSessionTask[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] sessionTask [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]session dataTaskWithRequest[backcolor=transparent]:[backcolor=transparent]request
  6. [backcolor=transparent]                                            completionHandler[backcolor=transparent]:^([backcolor=transparent]NSData[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]data[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]NSURLResponse[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]response[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]NSError[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]error[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  7. [backcolor=transparent]                                                [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]error[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  8. [backcolor=transparent]                                                    [backcolor=transparent][[backcolor=transparent]tcs setError[backcolor=transparent]:[backcolor=transparent]error[backcolor=transparent]];
  9. [backcolor=transparent]                                                    [backcolor=transparent]return[backcolor=transparent];
  10. [backcolor=transparent]                                                [backcolor=transparent]}
  11. [backcolor=transparent]                                                [backcolor=transparent][[backcolor=transparent]tcs setResult[backcolor=transparent]:[backcolor=transparent]data[backcolor=transparent]];
  12. [backcolor=transparent]                                            [backcolor=transparent]}];
  13. [backcolor=transparent][[backcolor=transparent]sessionTask resume[backcolor=transparent]];
  14. [backcolor=transparent]// 实现这个回调需要同步返回Token,所以要waitUntilFinished
  15. [backcolor=transparent][[backcolor=transparent]tcs[backcolor=transparent].[backcolor=transparent]task waitUntilFinished[backcolor=transparent]];
  16. [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]tcs[backcolor=transparent].[backcolor=transparent]task[backcolor=transparent].[backcolor=transparent]error[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  17. [backcolor=transparent]    [backcolor=transparent]// 如果网络请求出错,返回nil表示无法获取到Token。该次请求OSS会失败。
  18. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]nil[backcolor=transparent];
  19. [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  20. [backcolor=transparent]    [backcolor=transparent]// 从网络请求返回的内容中解析JSON串拿到Token的各个字段,组成STSToken返回
  21. [backcolor=transparent]    [backcolor=transparent]NSDictionary[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]object[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSJSONSerialization[backcolor=transparent] [backcolor=transparent]JSONObjectWithData[backcolor=transparent]:[backcolor=transparent]tcs[backcolor=transparent].[backcolor=transparent]task[backcolor=transparent].[backcolor=transparent]result
  22. [backcolor=transparent]                                                            options[backcolor=transparent]:[backcolor=transparent]kNilOptions
  23. [backcolor=transparent]                                                              error[backcolor=transparent]:[backcolor=transparent]nil[backcolor=transparent]];
  24. [backcolor=transparent]    [backcolor=transparent]OSSFederationToken[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] token [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]OSSFederationToken[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent]];[backcolor=transparent]ni
  25. [backcolor=transparent]    token[backcolor=transparent].[backcolor=transparent]tAccessKey [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]object[backcolor=transparent] objectForKey[backcolor=transparent]:@[backcolor=transparent]"AccessKeyId"[backcolor=transparent]];
  26. [backcolor=transparent]    token[backcolor=transparent].[backcolor=transparent]tSecretKey [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]object[backcolor=transparent] objectForKey[backcolor=transparent]:@[backcolor=transparent]"AccessKeySecret"[backcolor=transparent]];
  27. [backcolor=transparent]    token[backcolor=transparent].[backcolor=transparent]tToken [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]object[backcolor=transparent] objectForKey[backcolor=transparent]:@[backcolor=transparent]"SecurityToken"[backcolor=transparent]];
  28. [backcolor=transparent]    token[backcolor=transparent].[backcolor=transparent]expirationTimeInGMTFormat [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]object[backcolor=transparent] objectForKey[backcolor=transparent]:@[backcolor=transparent]"Expiration"[backcolor=transparent]];
  29. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] token[backcolor=transparent];
  30. [backcolor=transparent]}


源码下载


[backcolor=transparent]示例程序
[backcolor=transparent]应用服务器代码示例的下载

展开
收起
青衫无名 2017-10-18 10:06:21 1900 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载