Bootstrap

阿里云OSS简单使用(JAVA)

1.安装

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.8.0</version>
</dependency>
  1. 配置accesskey
    在springboot项目中创建accesskey.properties文件,内容如下:
endpoint=对象存储的地域节点
accessKeyId=最好使用访问控制 RAM
accessKeySecret=***
bucketName=***
  1. 创建OSSUtils功能类进行测试
public String[] getAccessKey() {
        Properties prop = new Properties();
        String projectPath = System.getProperty("user.dir");
        String[] str = new String[4];
        String accessProperties = projectPath + "\\src\\main\\java\\设置为自己项目的路径\\accesskey.properties";
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(accessProperties));
            prop.load(in);     ///加载属性列表
            str[0] = prop.getProperty("endpoint");
            str[1] = prop.getProperty("accessKeyId");
            str[2] = prop.getProperty("accessKeySecret");
            str[3] = prop.getProperty("bucketName");
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
  1. 查询所有文件
public void getFilesMsg() {
        OSSUtils ossUtils = new OSSUtils();
        String[] str = ossUtils.getAccessKey();
        String endpoint = str[0];
        String accessKeyId = str[1];
        String accessKeySecret = str[2];
        String bucketName = str[3];

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 构造ListObjectsRequest请求。
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);

        // 列出文件。
        ObjectListing listing = ossClient.listObjects(listObjectsRequest);

        // 遍历所有文件。
        System.out.println("Objects:");
        for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
            System.out.println("getKey = " + objectSummary.getKey());
            System.out.println("getSize = " + objectSummary.getSize());
            System.out.println("getBucketName = " + objectSummary.getBucketName());
            System.out.println("getETag = " + objectSummary.getETag());
            System.out.println("getLastModified = " + objectSummary.getLastModified());
            System.out.println("getOwner = " + objectSummary.getOwner());
            System.out.println("-------------------------------------------------");
        }

        // 关闭OSSClient。
        ossClient.shutdown();
    }
  1. 删除文件
public void deleteFiles() {
        OSSUtils ossUtils = new OSSUtils();
        String[] str = ossUtils.getAccessKey();
        String endpoint = str[0];
        String accessKeyId = str[1];
        String accessKeySecret = str[2];
        String bucketName = str[3];

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 删除文件。key等同于ObjectName,表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
        List<String> keys = new ArrayList<String>();
        keys.add("test");
        keys.add("test1");

        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();

        // 关闭OSSClient。
        ossClient.shutdown();
    }
  1. 上传文件
public void filesUpload(String filePath, String fileName) {
        OSSUtils ossUtils = new OSSUtils();
        String[] str = ossUtils.getAccessKey();
        String endpoint = str[0];
        String accessKeyId = str[1];
        String accessKeySecret = str[2];
        String bucketName = str[3];

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 带进度条的上传。
            ossClient.putObject(new PutObjectRequest(bucketName, fileName, new File("C:\\Users\\Elijah\\Desktop\\新建文本文档.txt")).
                    <PutObjectRequest>withProgressListener(new PutObjectProgressListener()));

        } catch (Exception e) {
            e.printStackTrace();
        }
        // 关闭OSSClient。
        ossClient.shutdown();
    }
  1. 断点续传下载文件
public void downloadFiles() {
        OSSUtils ossUtils = new OSSUtils();
        String[] str = ossUtils.getAccessKey();
        String endpoint = str[0];
        String accessKeyId = str[1];
        String accessKeySecret = str[2];
        String bucketName = str[3];

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 下载请求,10个任务并发下载,启动断点续传。
        DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, objectName);
        downloadFileRequest.setDownloadFile("te.txt");
        downloadFileRequest.setPartSize(1 * 1024 * 1024);
        downloadFileRequest.setTaskNum(10);
        downloadFileRequest.setEnableCheckpoint(true);
        downloadFileRequest.setCheckpointFile("download.ucp");

        DownloadFileResult downloadRes = null;

        try {
            // 下载文件。
            downloadRes = ossClient.downloadFile(downloadFileRequest);
            // 下载成功时,会返回文件元信息。
            downloadRes.getObjectMetadata();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        // 关闭OSSClient。
        ossClient.shutdown();
    }
;