用户如何体验自己的APP上传应用服务器
- 把程序部署起来后,记下应用服务器地址如http://abc.com:8080 将示例程序里面的应用服务器修改成上述地址。
- 选择数据要上传到哪个Bcuket及区域,修改示例APP程序里面相应Bucket及区域。
- 单击[backcolor=transparent]设置按钮,将配置加载。
- 选择图片,设置上传OSS文件名,上传。这样就可以在Android上体验OSS服务了,就能通Android示例程序将数据直接上传到OSS了。
- 上传成功后,可以看一下数据是否在OSS上了。
核心代码解析
OSS初始化
下面讲解如何利用Android/iOS SDK跟自己的应用服务器,请求Token。
- [backcolor=transparent]Android版本
- [backcolor=transparent]//初始化一个OssService用来上传
- [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]{
- [backcolor=transparent] [backcolor=transparent]OSSCredentialProvider[backcolor=transparent] credentialProvider[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]//使用自己的获取STSToken的类
- [backcolor=transparent]//从应用服务器控件里面读取应用服务器地址
- [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]();
- [backcolor=transparent] [backcolor=transparent]//STSGetter类,封装如何跟从应用服务器取数据,必须继承于OSSFederationCredentialProvider这个类。 取Token这个取决于您所写的APP跟应用服务器数据的协议设计。
- [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]{
- [backcolor=transparent] credentialProvider [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]STSGetter[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] credentialProvider [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]STSGetter[backcolor=transparent]([backcolor=transparent]stsServer[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]//获取控件上的bucket名字
- [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]();
- [backcolor=transparent]//初始化OSSClient
- [backcolor=transparent] [backcolor=transparent]ClientConfiguration[backcolor=transparent] conf [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]ClientConfiguration[backcolor=transparent]();
- [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秒
- [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秒
- [backcolor=transparent] conf[backcolor=transparent].[backcolor=transparent]setMaxConcurrentRequest[backcolor=transparent]([backcolor=transparent]5[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 最大并发请求数,默认5个
- [backcolor=transparent] conf[backcolor=transparent].[backcolor=transparent]setMaxErrorRetry[backcolor=transparent]([backcolor=transparent]2[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 失败后最大重试次数,默认2次
- [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]);
- [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]);
- [backcolor=transparent]}
- [backcolor=transparent]iOS版本
- [backcolor=transparent]// 初始化一个OSSClient实例
- [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]ossInit [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// 构造一个获取STSToken的凭证提供器
- [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]{
- [backcolor=transparent] [backcolor=transparent]// 实现一个函数,同步返回从server获取到的STSToken
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] getFederationToken[backcolor=transparent]];
- [backcolor=transparent] [backcolor=transparent]}];
- [backcolor=transparent] [backcolor=transparent]// 用endpoint、凭证提供器初始化一个OSSClient
- [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]];
- [backcolor=transparent]}
移动应用从应用服务器取Token
具体APP从应用服务器取Token的方法,必须写到public OSSFederationToken getFederationToken() { }这个函数里面。
注意:这个函数的逻辑依赖于可以自己设定,但是最终结果必须返回这样一个变量 return new OSSFederationToken(ak, sk, token, expiration),其中ak, sk, token, expiration 必须是从应用服务器返回的Body获取的。
在本示例中,用户可以自定义自己的移动应用跟自己应用服务器之前的协议。
- [backcolor=transparent]Android版本
- [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]OSSFederationToken[backcolor=transparent] getFederationToken[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] stsJson[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]OkHttpClient[backcolor=transparent] client [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OkHttpClient[backcolor=transparent]();
- [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]();
- [backcolor=transparent] [backcolor=transparent]try[backcolor=transparent] [backcolor=transparent]{
- [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]();
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]response[backcolor=transparent].[backcolor=transparent]isSuccessful[backcolor=transparent]())[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] stsJson [backcolor=transparent]=[backcolor=transparent] response[backcolor=transparent].[backcolor=transparent]body[backcolor=transparent]().[backcolor=transparent]string[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [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]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]catch[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]IOException[backcolor=transparent] e[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] e[backcolor=transparent].[backcolor=transparent]printStackTrace[backcolor=transparent]();
- [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]());
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]try[backcolor=transparent] [backcolor=transparent]{
- [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]);
- [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]);
- [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]);
- [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]);
- [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]);
- [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]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]catch[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]JSONException[backcolor=transparent] e[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [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]());
- [backcolor=transparent] e[backcolor=transparent].[backcolor=transparent]printStackTrace[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]}}
- [backcolor=transparent]iOS版本
- [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]];
- [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]];
- [backcolor=transparent]OSSTaskCompletionSource[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] tcs [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]OSSTaskCompletionSource[backcolor=transparent] taskCompletionSource[backcolor=transparent]];
- [backcolor=transparent]NSURLSession[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] session [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSURLSession[backcolor=transparent] sharedSession[backcolor=transparent]];
- [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
- [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]{
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]error[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]tcs setError[backcolor=transparent]:[backcolor=transparent]error[backcolor=transparent]];
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]tcs setResult[backcolor=transparent]:[backcolor=transparent]data[backcolor=transparent]];
- [backcolor=transparent] [backcolor=transparent]}];
- [backcolor=transparent][[backcolor=transparent]sessionTask resume[backcolor=transparent]];
- [backcolor=transparent]// 实现这个回调需要同步返回Token,所以要waitUntilFinished
- [backcolor=transparent][[backcolor=transparent]tcs[backcolor=transparent].[backcolor=transparent]task waitUntilFinished[backcolor=transparent]];
- [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]{
- [backcolor=transparent] [backcolor=transparent]// 如果网络请求出错,返回nil表示无法获取到Token。该次请求OSS会失败。
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]nil[backcolor=transparent];
- [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// 从网络请求返回的内容中解析JSON串拿到Token的各个字段,组成STSToken返回
- [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
- [backcolor=transparent] options[backcolor=transparent]:[backcolor=transparent]kNilOptions
- [backcolor=transparent] error[backcolor=transparent]:[backcolor=transparent]nil[backcolor=transparent]];
- [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
- [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]];
- [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]];
- [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]];
- [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]];
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] token[backcolor=transparent];
- [backcolor=transparent]}
源码下载
[backcolor=transparent]示例程序
[backcolor=transparent]应用服务器代码示例的下载