阿里云对象存储OSS--实现随时随地上传文件到阿里云

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xmc281141947/article/details/72844631

需求背景:消费者多批次回馈我司生产的车载智能后视镜出现死机、连不上服务器等问题,因产品已经出到全国各地不方便去取异常log,也不可能要求消费者把log传给我们分析。

需求目标:公司内部实现远程后台上传问题机型的log。

必备条件:后视镜有SIM卡且能够联网(远程控制需要联网)、问题后视镜的IMEI号((=International Mobile Equipment Identity)

过程有点曲折,下文我尽量描述的详细些方便大家实现类似的需求。
其它的云端存储我都试过,要么关闭了开发者模式,要么开发文档不全面,最终选择了阿里云。

一、阿里云端准备工作
1、注册阿里云账户

2、登录账户进入阿里云平台,点击左上角控制台

这里写图片描述

3、进入控制台后点击右上角“产品与服务”,然后分别开通“对象存储OSS”和“访问控制”

这里写图片描述

4、对象存储OSS:开通后需要购买流量包,我买的是5块钱40G/使用6个月,还需要创建文件夹,注意购买流量包和创建文件有个区域选择,填你所在的区域,完成后如下图

这里写图片描述

5、访问控制: 访问控制开通后需要创建用户,生成accessKeyId和accessKeySecret,这两个KEY保存起来后面程序中要用

这里写图片描述

二、安卓代码部分

1、下载阿里云demo,按下图步骤

这里写图片描述

点击支持——API

这里写图片描述

点击“开放了API产品”

这里写图片描述

进入后可以看到开发列表几十项,看的很烦躁,我直接下它的demo来调试,找到下图箭头位置,sample目录,点击查看里面是GitHub代码例子,直接下载出来

这里写图片描述

2、调整代码,demo里面有个MainActivity,代码调整如下,实现了遍历文件夹,上传文件夹全部文件

public class MainActivity extends Activity {
    private OSS oss;

    // 运行sample前需要配置以下字段为有效的值
    private static final String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";//固定,我所在地域是华南,城市要改成shenzhen
    private static final String accessKeyId = "LTAIboGV8y0e";//固定
    private static final String accessKeySecret = "Z1S5Nzi4XnzgtYihPQeQB165";//固定
    private static final String testBucketName = "jimi0208";//上传到 ALi OSS 中的文件夹名字,固定命名

    private static final String uploadFilePath = "/storage/sdcard0/C6Log/CarRecorderLog/";

    private static final String TAG = "MainActivity===";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKeyId, accessKeySecret);
        ALiApplication.saveLogFile(TAG + credentialProvider+"====="+credentialProvider.toString());

        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
        OSSLog.enableLog();
        oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf);


        try {
            Log.i(TAG, "uploadFilePath : " + uploadFilePath);
            File uploadFile = new File(uploadFilePath);
            InputStream input = new FileInputStream(uploadFile);
            long fileLength = uploadFile.length();
            ALiApplication.saveLogFile(TAG + "fileLength:"+fileLength);
            Log.i(TAG, "fileLength : " + fileLength);
        } catch (Exception e) {
            e.printStackTrace();
        }


        // 上传
        final ArrayList<String> listFileName = new ArrayList<String>();
        final String dvrPath = Environment.getExternalStorageDirectory().getPath()+"/C6Log/CarRecorderLog/";//这个路径根据自己的要上传的路径修改
        Log.i(TAG, "dvrPath" + dvrPath);//此处加打印查看路径是否正确
        getAllFileName(dvrPath,listFileName);

        Button upload = (Button) findViewById(R.id.upload);
        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int count = 0 ;
                        for(String uploadObj:listFileName){//循环文件夹
                            Log.i(TAG, "run()++=listFileName: " + listFileName.get(count));
                            ALiApplication.saveLogFile(TAG + listFileName.toString());
                            String uploadObject = listFileName.get(count);//第一次是获取list 0位置的文件名
                            if(uploadObject != null){
                                new PutObjectSamples(oss, testBucketName, uploadObject, dvrPath + uploadObject).asyncPutObjectFromLocalFile();
                            }
                            count++;//count每次加一
                        }
                    }
                }).start();
            }
        });

    }


    public static void getAllFileName(String path, ArrayList<String> fileName) {
        File file = new File(path);
         if (!file.exists()) {
                return;
               }
        File[] files = file.listFiles();
        ALiApplication.saveLogFile(TAG + "getAllFileName:"+ path);
        ALiApplication.saveLogFile(TAG + "getAllFileName:"+ files);

        String[] names = file.list();
        ALiApplication.saveLogFile(TAG + "getAllFileName:"+ names);

        if (names != null){
            fileName.addAll(Arrays.asList(names));
        }
        if (files != null){
            for (File a : files) {
                if (a.isDirectory()) {
                    getAllFileName(a.getAbsolutePath(), fileName);
                }
            }
        }else{
            ALiApplication.saveLogFile(TAG + "files====null");
        }
    }

}

// 运行sample前需要配置以下字段为有效的值
private static final String endpoint = “http://oss-cn-shenzhen.aliyuncs.com“;//固定,我所在地域是华南,城市要改成shenzhen
private static final String accessKeyId = “LTAIboGV8y0e”;//固定
private static final String accessKeySecret = “Z1S5Nzi4XnzgtYihPQeQB165”;//固定
private static final String testBucketName = “jimi0208”;//上传到 ALi OSS 中的文件夹名字,固定命名
这几个值需要调整
第一个不同的地区要改不同的城市
第二、三个值是“访问控制”里面创建的用户KEY
第四个值是“对象存储OSS”中创建的文件夹

3、上传完成后如下图,我上传的是.txt文件,可以直接获取地址打开链接来查看
这里写图片描述

完整的代码下载地址:阿里云OSS存储
也可以直接联系我QQ:1715499699

最后,demo的代码只是实现了上传功能,要实现客户端远程上传log需要将demo改造成一个Service,配置开机自启。当客户反馈某个IMEI号的机器出现问题时,我们给这IMEI号的机器推送一个上传log的指令,实现远程上传log,便于分析异常原因。

猜你喜欢

转载自blog.csdn.net/xmc281141947/article/details/72844631