企业网站建设费用怎么入账,响应式网站建设特色,沈阳网吧什么时候恢复营业,男男床做视频网站在线目录
一.什么是阿里云OSS#xff1f;
二.阿里云OSS的使用#xff1a;
1.导入依赖到 pom.xml 配置文件#xff1a;
2.在application.yml配置文件中配置OSS的相关信息#xff1a;
3.在properties包下创建OSS的配置属性类来封装配置文件的配置项#xff1a;
4.在utils包…目录
一.什么是阿里云OSS
二.阿里云OSS的使用
1.导入依赖到 pom.xml 配置文件
2.在application.yml配置文件中配置OSS的相关信息
3.在properties包下创建OSS的配置属性类来封装配置文件的配置项
4.在utils包下创建AliOssUtil类
4.在config包下创建AliOssUtil对象
5.在Controller中使用AliOssUtil工具类进行文件上传 一.什么是阿里云OSS
OSS 为 Object Storage Service即对象存储服务。是阿里云提供的海量、安全、低成本、高可靠的云存储服务。
OSS 具有与平台无关的 RESTful API 接口可以在任意应用、任意时间、任意地点 存储与访问 任何类型的数据。
简单地理解OSS 基于网络提供数据存储服务通过网络可以随时存储、获取 文本、图片、音频、视频等 非结构化数据。
比如网站的 图片、视频等文件就可以存放在 OSS 中海量数据自己维护起来麻烦交给其他人去维护每次从 OSS 中获取即可。
二.阿里云OSS的使用
下面这个是阿里云的官网https://www.aliyun.com
1.导入依赖到 pom.xml 配置文件
dependencygroupIdcom.aliyun.oss/groupIdartifactIdaliyun-sdk-oss/artifactId
/dependency
2.在application.yml配置文件中配置OSS的相关信息
sky:alioss:endpoint: oss-cn-hangzhou.aliyuncs.com #访问域名access-key-id: ************************ #访问密钥access-key-secret: *********************** #加密签名字符串和 OSS 用来验证签名字符串的密钥 bucket-name: sky-take-out-service #存储空间名
3.在properties包下创建OSS的配置属性类来封装配置文件的配置项
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中的四个属性赋值
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/ObjectNameStringBuilder 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容器来管理
Configuration
Slf4j
public class OssConfiguration {BeanConditionalOnMissingBeanpublic AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){log.info(开始创建阿里云文件上传工具类对象:{},aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint() ,aliOssProperties.getAccessKeyId() ,aliOssProperties.getAccessKeySecret() ,aliOssProperties.getBucketName());}
}
5.在Controller中使用AliOssUtil工具类进行文件上传
RestController
RequestMapping(/admin/common)
Api(tags 通用接口)
Slf4j
public class CommonController {Autowiredprivate AliOssUtil aliOssUtil;PostMapping(/upload)ApiOperation(文件上传)public ResultString 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对象存储服务的项目操作解释就到这里了感谢收看