tlias管理系统--@ConfigurationProperties配置类的方式优化阿里云OSS工具类
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
package com.inmind.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "aliyun.oss")
|
||||||
|
public class AliOssProperties {
|
||||||
|
private String endpoint;
|
||||||
|
private String bucketName;
|
||||||
|
private String region;
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ import com.aliyun.oss.common.comm.SignVersion;
|
|||||||
import com.aliyun.oss.model.PutObjectRequest;
|
import com.aliyun.oss.model.PutObjectRequest;
|
||||||
import com.aliyun.oss.model.PutObjectResult;
|
import com.aliyun.oss.model.PutObjectResult;
|
||||||
import com.aliyuncs.exceptions.ClientException;
|
import com.aliyuncs.exceptions.ClientException;
|
||||||
|
import com.inmind.config.AliOssProperties;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@@ -20,13 +22,15 @@ import java.util.UUID;
|
|||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class AliOSSUtils {
|
public class AliOSSUtils {
|
||||||
|
@Autowired
|
||||||
|
private AliOssProperties aliOssProperties;
|
||||||
|
|
||||||
@Value("${aliyun.oss.endpoint}")
|
/*@Value("${aliyun.oss.endpoint}")
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
@Value("${aliyun.oss.bucketName}")
|
@Value("${aliyun.oss.bucketName}")
|
||||||
private String bucketName;
|
private String bucketName;
|
||||||
@Value("${aliyun.oss.region}")
|
@Value("${aliyun.oss.region}")
|
||||||
private String region ;
|
private String region ;*/
|
||||||
|
|
||||||
// private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
|
// private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
|
||||||
// private String bucketName = "inmind-test1";
|
// private String bucketName = "inmind-test1";
|
||||||
@@ -36,6 +40,11 @@ public class AliOSSUtils {
|
|||||||
* 实现上传图片到OSS
|
* 实现上传图片到OSS
|
||||||
*/
|
*/
|
||||||
public String upload(MultipartFile file) throws IOException, ClientException {
|
public String upload(MultipartFile file) throws IOException, ClientException {
|
||||||
|
String endpoint = aliOssProperties.getEndpoint();
|
||||||
|
String bucketName = aliOssProperties.getBucketName();
|
||||||
|
String region = aliOssProperties.getRegion();
|
||||||
|
|
||||||
|
|
||||||
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
|
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
|
||||||
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||||
// 获取上传的文件的输入流
|
// 获取上传的文件的输入流
|
||||||
|
|||||||
Reference in New Issue
Block a user