0%

OSS服务端上传图片

application.yml配置

1
2
3
4
5
6
7
ali:
accessKeyId: LTAILIFNinrJIEDr #阿里云API的密钥Access Key ID
accessKeySecret: EqIUz04aHepDN3znBdRNL3FAddAiBp #阿里云API的密钥Access Key Secret
endpoint: https://oss-ap-northeast-1.aliyuncs.com # 地域站点,以北京站点为例
bucketName: go-one #存储空间 即仓库名称 文件根目录
# url: https://xxxx.oss-cn-hangzhou.aliyuncs.com/ #资源访问url
objectName: avatar #对象/文件目录 仓库中的某个文件夹

OSSConfig配置类

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
package com.xy.goone.config.oss;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
* @Description: 阿里云 OSS 配置信息
* @author fumei.jiang
* @date 2019-09-29 16:25
*/
@Data
@Configuration
public class OSSConfig {

private static final long serialVersionUID = -119396871324982279L;

@Value("${ali.accessKeyId}")
private String accessKeyId;

@Value("${ali.accessKeySecret}")
private String accessKeySecret;

@Value("${ali.endpoint}")
private String endpoint;

@Value("${ali.bucketName}")
private String bucketName;

@Value("${ali.objectName}")
private String fileDir;

@Value("${ali.downloadFirDir}")
private String downloadFirDir;

}

OssClientUtil.java

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
package com.xy.goone.common.util;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.*;
import com.xy.goone.config.oss.OSSConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
* @author fumei.jiang
* @date 2019-09-26 17:03
* OSS上传工具类
*/
@Slf4j
public class OSSClientUtil {

public OSSClientUtil() {
}

/**
* oss 工具客户端
*/
private volatile static OSSClient ossClient = null;

/**
* @param ossConfig
* @param file
* @return
*/
public static String upload(OSSConfig ossConfig, MultipartFile file) throws IOException {
// 判断所要上传的图片是否是图片,图片可以预览,其他文件不提供通过URL预览、
String fileName = file.getOriginalFilename();
//initOSS(ossConfig);
OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
String key = "avatar/" + UUID.randomUUID().toString().replaceAll("-", "")+fileName.substring(fileName.lastIndexOf("."));//AliUploadController.randomFileName() + ".class";
ossClient.putObject("go-one", key, file.getInputStream());
ossClient.shutdown();
log.info("url: {}", key);
return ossConfig.getEndpoint().replace("https://", "https://" + ossConfig.getBucketName()+ ".")+"/"+key;
}
/**
* 初始化 oss 客户端
*
* @param ossConfig
* @return
*/
private static OSSClient initOSS(OSSConfig ossConfig) {
if (ossClient == null) {
synchronized (OSSClientUtil.class) {
if (ossClient == null) {
ossClient = new OSSClient(ossConfig.getEndpoint(),
new DefaultCredentialProvider(ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret()),
new ClientConfiguration());
}
}
}
return ossClient;
}

}
欣赏此文?求鼓励,求支持!