SDK集成

添加依赖

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

主要代码:

public class OSSTest {
    @Test
    public void oss() throws FileNotFoundException {
        // Endpoint以杭州为例,其它Region请按实际情况填写。
        String endpoint = "oss-cn-shenzhen.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 上传文件流。
        InputStream inputStream = new FileInputStream("<yourlocalFile>");
        ossClient.putObject("<yourBucketName>", "<yourObjectName>", inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
    }
}

endpoint的取值:

accessKeyId和accessKeySecret需要创建一个RAM账号:

创建用户完毕后,会得到一个 “AccessKey ID” 和 “AccessKeySecret”,然后复制这两个值到代码的 “AccessKey ID”和“AccessKeySecret”。

另外还需要添加访问控制权限:

SpringCloud集成

官方文档:

https://help.aliyun.com/knowledge_detail/108650.html

pom.xml文件中添加如下依赖。

以 Spring Boot 2.1.4.RELEASE 和 Spring Cloud Greenwich.SR1 为例。

 <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.1.4.RELEASE</version>
     <relativePath/>
 </parent>

  <dependencies>
     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
         <version>2.1.1.RELEASE</version>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
 </dependencies>

 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>Finchley.SR1</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>                        

在 application.yml 中配置key,secret和endpoint相关信息

spring:
  cloud:
    alicloud:
      access-key: your
      secret-key: your
      oss:
        endpoint: oss-cn-shenzhen.aliyuncs.com #your oss地域

添加一个测试类 OssApplication

package com.example.demo;

import com.aliyun.oss.OSSClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author 乐心湖
 * @date 2020/9/15 14:18
 **/
@SpringBootTest
public class OssApplication {
    @Autowired
    private OSSClient ossClient;

    @Test
    public void upload() throws FileNotFoundException {
        // 上传文件流。
        InputStream inputStream = new FileInputStream("D:\\业务模板图.png");
        ossClient.putObject("laketrading", "2.png", inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
        System.out.println("上传成功.");
    }
}

写在最后,我们这样设计web项目的话,每次都要讲文件上传到服务端再由服务端上传到阿里云oss,这种情况是不理想的。一般前后端分离项目时我们会采用官方文档中的最佳实践,使用服务端签名后直传的方式,这样既能减少服务器压力,也能拥有良好的安全性。

为什么还是需要通过服务端呢,采用JavaScript客户端直接签名(参见JavaScript客户端签名直传)时,AccessKey ID和AcessKey Secret会暴露在前端页面,因此存在严重的安全隐患。


Last modification:September 15, 2020
如果觉得我的文章对你有用,请随意赞赏