android客户端直传文件到阿里云OSS服务器详细流程代码

 

1.阿里云官方文档

https://help.aliyun.com/document_detail/31920.html?spm=a2c4g.11186623.2.12.2a1cbdce9IWQFh#title-zp8-uz7-br6  

 

2.拼接后的地址

   http://image.chongdm.com/zhaozhuang/20200924/1600931173684.png

3.后台返回的数据格式必须解析如下:

接口:http://api.chongdm.com/gateway/oss/getOssToken
//正确返回
{
    "StatusCode":200,
    "AccessKeyId":"STS.3p***dgagdasdg",
    "AccessKeySecret":"rpnwO9***tGdrddgsR2YrTtI",
   "SecurityToken":"CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s˙∂˙∂3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVtbzI=",
   "Expiration":"2017-12-12T07:49:09Z",
}
//错误返回
{
    "StatusCode":500,
    "ErrorCode":"InvalidAccessKeyId.NotFound",
    "ErrorMessage":"Specified access key is not found."
}

 3.app端上传文件路径,需要自己拼接

private var stsServer: String = "http://api.chongdm.com/gateway/oss/getOssToken"   
private  var oss: OSS? = null

fun initOSS() {        
        val credentialProvider: OSSCredentialProvider
        credentialProvider = OSSAuthCredentialsProvider(stsServer)
        val conf = ClientConfiguration()
        conf.connectionTimeout = 15 * 1000 // 连接超时,默认15秒
        conf.socketTimeout = 15 * 1000 // socket超时,默认15秒
        conf.maxConcurrentRequest = 5 // 最大并发请求书,默认5个
        conf.maxErrorRetry = 2 // 失败后最大重试次数,默认2次
        oss = OSSClient(applicationContext, endpoint, credentialProvider, conf)


    }
fun postToOss(path: String) {

        val OssPath = "zhaozhuang/" + DateUtils.getCurrentDataDefaultString()+"/"+TimeUtils.getNowMills()+".png"
        // 构造上传请求。
        val put =
            PutObjectRequest(bucketName, OssPath, path)

        // 异步上传时可以设置进度回调。
        put.progressCallback = OSSProgressCallback { request, currentSize, totalSize ->
            Log.d(
                "PutObject",
                "currentSize: $currentSize totalSize: $totalSize"
            )
        }
        val task: OSSAsyncTask<*> = oss!!.asyncPutObject(
            put,
            object : OSSCompletedCallback<PutObjectRequest?, PutObjectResult> {
                override fun onSuccess(
                    request: PutObjectRequest?,
                    result: PutObjectResult
                ) {
                    Log.d("PutObject", "UploadSuccess")
                    Log.d("ETag", result.eTag)
                    Log.d("RequestId", result.toString())
                }

                override fun onFailure(
                    request: PutObjectRequest?,
                    clientExcepion: ClientException,
                    serviceException: ServiceException
                ) {
                    // 请求异常。
                    clientExcepion?.printStackTrace()
                    if (serviceException != null) {
                        // 服务异常。
                        Log.e("ErrorCode", serviceException.errorCode)
                        Log.e("RequestId", serviceException.requestId)
                        Log.e("HostId", serviceException.hostId)
                        Log.e("RawMessage", serviceException.rawMessage)
                    }
                }
            })
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄毛火烧雪下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值