什么情况下可以使用阿里云oss服务存储图片?
对图片的访问速度有高要求时使用,方便用户快速的(比如在网页页面中)访问到图像
参考:41 尚上优选项目-平台管理端-商品信息管理模块-阿里云OSS介绍_哔哩哔哩_bilibili
1. 准备工作
(1)开通“对象存储OSS”服务
申请阿里云账号
实名认证
开通“对象存储OSS”服务
进入管理控制台
(2)创建Bucket
输入名称,选择区域,选择标准存储、公共读
(3)创建accessKeys
(4)使用SDK
如何使用JavaSDK简单上传文件_对象存储(OSS)-阿里云帮助中心
2. 示例代码
(1)pom.xml引入依赖
<dependencies><!-- 阿里云oss依赖 --><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.9.1</version></dependency><!-- 日期工具栏依赖 --><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.10.1</version></dependency>
</dependencies>
(2)application-dev.yml添加配置
aliyun:endpoint: oss-cn-beijing.aliyuncs.comkeyid: LTAxxxxxxx9skeysecret: lCaDcmvxxxxxxxxxxxxxxxx5FgMbucketname: ssyx-guigu
(3)添加FileUploadController方法
package com.atguigu.ssyx.product.service.impl;import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.atguigu.ssyx.product.service.FileUploadService;
import org.joda.time.DateTime;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.io.InputStream;
import java.util.UUID;@Service
public class FileUploadServiceImpl implements FileUploadService {@Value("${aliyun.endpoint}") //知识点:使用@Value读取application-dev.yml文件中的属性值private String endpoint;@Value("${aliyun.keyid}")private String keyid;@Value("${aliyun.keysecret}")private String keysecret;@Value("${aliyun.bucketname}")private String bucketName;@Overridepublic String uploadFile(MultipartFile file) {//参考文档:https://help.aliyun.com/zh/oss/developer-reference/simple-upload-11?spm=a2c4g.11186623.help-menu-31815.d_3_2_0_3_0_0.6ef81855hqyaRP// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);try {InputStream inputStream = file.getInputStream();// 创建PutObjectRequest对象。String objectName = file.getOriginalFilename();String uuid = UUID.randomUUID().toString().replace("-", "");objectName = uuid + objectName;String timeStr = new DateTime().toString("yyyy/MM/dd");objectName = timeStr + "/" + objectName;PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);// 设置该属性可以返回response, 如果不设置,返回的response为空putObjectRequest.setProcess("true");// 创建PutObject请求。PutObjectResult result = ossClient.putObject(putObjectRequest);System.out.println(result.getResponse().getStatusCode());System.out.println(result.getResponse().getErrorResponseAsString());System.out.println(result.getResponse().getUri());String fileUrl = result.getResponse().getUri();//文件最终的上传路径结果//String url ="https://" + bucketName + "." + endpoint + "/" + filename;return fileUrl;} catch (Exception 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();}}return null;}
}
(4)上传成功后,图像位置:
访问路径: