目录
[1.导入依赖到 pom.xml 配置文件:](#1.导入依赖到 pom.xml 配置文件:)
2.在application.yml配置文件中配置OSS的相关信息:
3.在properties包下创建OSS的配置属性类来封装配置文件的配置项:
5.在Controller中使用AliOssUtil工具类进行文件上传:
一.什么是阿里云OSS?
OSS 为 Object Storage Service,即对象存储服务。是阿里云提供的海量、安全、低成本、高可靠的云存储服务。
OSS 具有与平台无关的 RESTful API 接口,可以在任意应用、任意时间、任意地点 存储与访问 任何类型的数据。
简单地理解:OSS 基于网络提供数据存储服务,通过网络可以随时存储、获取 文本、图片、音频、视频等 非结构化数据。
比如网站的 图片、视频等文件就可以存放在 OSS 中(海量数据,自己维护起来麻烦,交给其他人去维护),每次从 OSS 中获取即可。
二.阿里云OSS的使用:
下面这个是阿里云的官网:https://www.aliyun.com
1.导入依赖到 pom.xml 配置文件:
XML
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
2.在application.yml配置文件中配置OSS的相关信息:
XML
sky:
alioss:
endpoint: oss-cn-hangzhou.aliyuncs.com #访问域名
access-key-id: ************************ #访问密钥
access-key-secret: *********************** #加密签名字符串和 OSS 用来验证签名字符串的密钥
bucket-name: sky-take-out-service #存储空间名
3.在properties包下创建OSS的配置属性类来封装配置文件的配置项:
java
@Component
@ConfigurationProperties(prefix = "sky.alioss")
@Data
public class AliOssProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
4.在utils包下创建AliOssUtil类:
通过创建的OSS配置属性类来给AliOssUtil中的四个属性赋值:
java
@Data
@AllArgsConstructor
@Slf4j
public class AliOssUtil {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
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();
}
}
//文件访问路径规则 https://BucketName.Endpoint/ObjectName
StringBuilder stringBuilder = new StringBuilder("https://");
stringBuilder
.append(bucketName)
.append(".")
.append(endpoint)
.append("/")
.append(objectName);
log.info("文件上传到:{}", stringBuilder.toString());
return stringBuilder.toString();
}
}
4.在config包下创建AliOssUtil对象:
将AliOssUtil类交给Spring容器来管理:
java
@Configuration
@Slf4j
public class OssConfiguration {
@Bean
@ConditionalOnMissingBean
public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){
log.info("开始创建阿里云文件上传工具类对象:{}",aliOssProperties);
return new AliOssUtil(aliOssProperties.getEndpoint() ,
aliOssProperties.getAccessKeyId() ,
aliOssProperties.getAccessKeySecret() ,
aliOssProperties.getBucketName());
}
}
5.在Controller中使用AliOssUtil工具类进行文件上传:
java
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
@Autowired
private AliOssUtil aliOssUtil;
@PostMapping("/upload")
@ApiOperation("文件上传")
public Result<String> upload(MultipartFile file){//file这个需与前端提交的参数名保持一致
log.info("文件上传:{}",file);
try {
//原始文件名
String orginaiFilename = file.getOriginalFilename();
//截取原始文件名的后缀
String extension = orginaiFilename.substring(orginaiFilename.lastIndexOf("."));
//构造新文件名称
String objectName = UUID.randomUUID().toString() + extension;
//文件请求路径
String filePath = aliOssUtil.upload(file.getBytes(),objectName);
//返回路径
return Result.success(filePath);
} catch (IOException e) {
log.info("文件上传失败:{}",e);
}
return Result.error(MessageConstant.UPLOAD_FAILED);//文件上传失败
}
}
好了,阿里云OSS对象存储服务的项目操作解释就到这里了,感谢收看!!!