阿里云OSS实现头像上传
程序员文章站
2022-05-15 14:13:50
...
目录结构:
application.yml
server:
port: 8002
spring:
application:
name: service-oss
profiles:
active: dev
aliyun:
oss:
file:
endpoint: oss-cn-beijing.aliyuncs.com
keyid: LTAI4G8q7nBEsUb1NxXC
keysecret: 3FEJ5vh5EttqiiLdviC1dVjuvU
#bucket可以在控制台创建也可以在代码中创建
bucketname: edu-guli-study
ConstantPropertiesUtils:(用于注入配置文件中的阿里云配置信息)
@Component //实现InitializingBean接口可以执行执行赋值后的一些逻辑(由于属性是private的)
public class ConstantPropertiesUtils implements InitializingBean {
@Value("${aliyun.oss.file.endpoint}")
private String endpoint;
@Value("${aliyun.oss.file.keyid}")
private String keyId;
@Value("${aliyun.oss.file.keysecret}")
private String keySecret;
@Value("${aliyun.oss.file.bucketname}")
private String bucketName;
/*定义公有静态常量*/
public static String ENDPOINT;
public static String KEYID;
public static String KEYSECRET;
public static String BUCKETNAME;
@Override
public void afterPropertiesSet() throws Exception {
ENDPOINT = endpoint;
KEYID = keyId;
KEYSECRET = keySecret;
BUCKETNAME = bucketName;
}
}
OssApplication:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = {"com.coderkun"}) //为了swagger扫描到该包下的Controller
public class OssApplication {
public static void main(String[] args) {
SpringApplication.run(OssApplication.class,args);
}
}
OSSController:
@RestController
@Api(description = "oss文件上传")
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class OssController {
@Autowired
private OssService ossService;
//上传头像方法
@PostMapping
public R uploadOssFile(MultipartFile file){
String url = ossService.uploadOssFileAvatar(file);
return R.ok().data("url",url);
}
}
OssService:
public interface OssService {
//上传头像到oss
String uploadOssFileAvatar(MultipartFile file);
}
OssServiceImpl:
@Service
public class OssServiceImpl implements OssService {
@Override
public String uploadOssFileAvatar(MultipartFile file) {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = ConstantPropertiesUtils.ENDPOINT;
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
String accessKeyId = ConstantPropertiesUtils.KEYID;
String accessKeySecret = ConstantPropertiesUtils.KEYSECRET;
String bucketName = ConstantPropertiesUtils.BUCKETNAME;
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流。
InputStream inputStream = file.getInputStream();
//获取文件名称
String fileName = file.getOriginalFilename();
//1.由于文件名重复会覆盖,生成随机文件名
String uuid = UUID.randomUUID().toString().replaceAll("-","");
fileName = uuid + fileName;
//2.把文件按照日期分类
String datePath = new DateTime().toString("yyyy/MM/dd");
fileName = datePath + "/" + fileName; //2020/6/4/dafdf.jpg
//第二个参数:上传到oss的文件路径和文件名称 /aa/bb1.jpg
ossClient.putObject(bucketName,fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//把上传之后oss返回的文件url返回()
//url格式:https://edu-guli-study.oss-cn-beijing.aliyuncs.com/%25U%7EHW%2502P2OH6FXR%29%5B8%60T2A.png
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
return url;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}