Aliyun OSSClient JAVA SDK 实现文件上传,下载

整理一下关于阿里云OSS API的相关实现

  Tips:

    1> 任何第三方工具的接入都要先找官方的帮助文档

    2> 把 Github 作为最好的代码搜索工具

 

具体的实现:

1. 需要的maven依赖:

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.11.2</version>
</dependency>

 

2. 需要的属性, 相应的属性需要配置在yaml中

    @Value("${OSS.endpoint}")
    private String endpoint;
    @Value("${OSS.accessKeyId}")
    private String accessKeyId;
    @Value("${OSS.accessKeySecret}")
    private String accessKeySecret;
    @Value("${OSS.bucketName}")
    private String bucketName;

    yaml:

OSS:
  endpoint: http://oss-cn-hangzhou.aliyuncs.com
  accessKeyId: *************
  accessKeySecret: ******************
  bucketName: my-bucket

 

3.OSSClient对象的创建:

    public OSSClient ossClient() {
        ClientConfiguration conf = new ClientConfiguration();
        conf.setSupportCname(true);
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        OSSClient ossClient = new OSSClient(endpoint, credentialsProvider, conf);
        return ossClient;
    }

官方文档也提供了相应的实现示例,可以参考:

https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.931.726f7a74qDvLRY

 

4. 通过ossClient实现文件的上传,下载,删除等功能,官方的帮助文档写的非常详细,直接上链接:

OSSClient帮助文档: 

https://help.aliyun.com/product/31815.html?spm=a2c4g.11186623.6.540.296f5338D4UArU

Java SDK的多种文件上传方式: 

https://help.aliyun.com/document_detail/32013.html?spm=a2c4g.11186623.2.12.172e62e7dDPtWm

Java SDK的文件下载方式:

https://help.aliyun.com/document_detail/32014.html?spm=a2c4g.11186623.6.961.64c26d138zC2qB

 

posted @ 2021-01-16 16:20  JamesPlay  阅读(1909)  评论(0编辑  收藏  举报