Android腾讯云对象存储上传图片

Posted 编程小弟弟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android腾讯云对象存储上传图片相关的知识,希望对你有一定的参考价值。

最近使用腾讯云对象存储,项目比较赶,想找个现有的复制结果找了半天,全是官方文档,最后乖乖的自己写了,记录一下以后用的时候方便。

腾讯云对象存储官方文档地址:https://cloud.tencent.com/document/product/436/12159

 

public class CosServiceUtils 
    String region = "存储桶所属地区";
    CosXmlService cosXmlService;
    UpLoadCallBack upLoadCallBack;//自己定义一个接口接收返回的东西
    public  void initCosService(Context context,String tmpSecretId,String tmpSecretKey,String sessionToken,Long startTime,Long expiredTime)
        CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
                .setRegion(region)
                .isHttps(true) // 使用 HTTPS 请求
                .builder();
        cosXmlService = new CosXmlService(context,
                serviceConfig, new BasicLifecycleCredentialProvider()
            @Override
            protected QCloudLifecycleCredentials fetchNewCredentials() throws QCloudClientException 
                return new SessionQCloudCredentials(tmpSecretId,tmpSecretKey,sessionToken,startTime,expiredTime);
            
        );
    
    public void setUpLoadCallBack(UpLoadCallBack upLoadCallBack)
        this.upLoadCallBack = upLoadCallBack;
    
    public void upLoadFile(Context context,String imgurl)
        TransferConfig transferConfig = new TransferConfig.Builder().build();
        TransferManager transferManager = new TransferManager(cosXmlService,
                transferConfig);
        String bucket = "你自己的BucketName"; //存储桶,格式:BucketName-APPID
        String cosPath = "userimg/"+MyUtils.INSTANCE.TimestampToDate(System.currentTimeMillis()/1000+"","yyyyMMddHHmmss")+".jpg"; //对象在存储桶中的位置标识符,即称对象键(这里我用的时间来当做图片的名字)
        String srcPath = imgurl;
        String uploadId = null;
        // 上传文件
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);
        COSXMLUploadTask cosxmlUploadTask = transferManager.upload(putObjectRequest, uploadId);
        //设置返回结果回调
        cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() 
            @Override
            public void onSuccess(CosXmlRequest request, CosXmlResult result) 
                COSXMLUploadTask.COSXMLUploadTaskResult cOSXMLUploadTaskResult =
                        (COSXMLUploadTask.COSXMLUploadTaskResult) result;
                upLoadCallBack.onSuccess(cOSXMLUploadTaskResult.accessUrl);
            
            @Override
            public void onFail(CosXmlRequest request,
                               CosXmlClientException clientException,
                               CosXmlServiceException serviceException) 
                if (clientException != null) 
                    clientException.printStackTrace();
                 else 
                    serviceException.printStackTrace();
                
                upLoadCallBack.onFail();
            
        );

    

 

以上是关于Android腾讯云对象存储上传图片的主要内容,如果未能解决你的问题,请参考以下文章

Android腾讯云对象存储上传图片

Android腾讯云对象存储上传图片

小程序开发:上传图片到腾讯云

微信小程序基于腾讯云对象存储的图片上传

腾讯云对象存储

腾讯云对象存储