利用阿里云OSS实现文件流传输

首先在阿里云购买OSS服务并设置公共读,开启设置bucket后

我们可以在利用SpringBoot去实现文件流传输

首先要导包

1
2
3
4
5
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>XX.XX.XX</version>
</dependency>

配置好配置文件yaml

1
2
3
4
5
6
7
sky:
alioss:
endpoint: 终端uri
#oss-cn-beijing.aliyuncs.com
access-key-id: 秘钥id
access-key-secret: 你的秘钥
bucket-name: 你的Bucket名

定义工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
@Component
public class AliOssUtil {
//注入值
@Value("${sky.alioss.endpoint}")
private String endpoint;
@Value("${sky.alioss.access-key-id}")
private String accessKeyId;
@Value("${sky.alioss.access-key-secret}")
private String accessKeySecret;
@Value("${sky.alioss.bucket-name}")
private String bucketName;

/**
* 文件上传
*
* @param bytes
* @param objectName
* @return
*/
public String upload(byte[] bytes, String objectName) {

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

try {
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}

StringBuilder stringBuilder = new StringBuilder("https://");
stringBuilder
.append(bucketName)
.append(".")
.append(endpoint)
.append("/")
.append(objectName);

log.info("文件上传到:{}", stringBuilder.toString());
//返回生成图片的URL
return stringBuilder.toString();
}
}

开始具体实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   @PostMapping("/upload")
//Spring为我们提供了一个接口类用于封装前端传输过来的流对象
public Result upload(MultipartFile file) throws IOException {
//接口方法用于获取最后一个 指定字符串的索引
int extendIndex = file.getOriginalFilename().lastIndexOf(".");
//截取后缀名
String extendString = file.getOriginalFilename().substring(extendIndex);
//文件名name
String ObjectName = UUID.randomUUID().toString() + extendString;
//上传文件
String upload = aliOssUtil.upload(file.getBytes(), ObjectName);
return Result.success(upload);
}

之后生成的URL就可以返回到前端,前端使用URL即可读取图片