阿里云oss

tech2023-09-15  103

1、oss开通

开通成功后,打开管理控制台 创建AccessKey密钥,包含id和密码

2、oss使用

https://help.aliyun.com/document_detail/32009.html?spm=a2c4g.11186623.6.805.74786328u2crN2 <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency> import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.PutObjectRequest; import java.io.File; public class TestOss { public static void main(String[] args) { // Endpoint以杭州为例,其它Region请按实际情况填写。 String endpoint = "http://oss-cn-beijing.aliyuncs.com"; //要变成自己。 // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 String accessKeyId = ""; //id String accessKeySecret = "";//密钥 // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 创建PutObjectRequest对象。 //<yourObjectName>:你上传到oss后的名称。 PutObjectRequest putObjectRequest = new PutObjectRequest("aaa-hdw-edu", "test1.jpg", new File("C:\\Users\\Yellow\\Desktop\\QQ图片20200903163352.jpg")); // 上传文件。 ossClient.putObject(putObjectRequest); // 关闭OSSClient。 ossClient.shutdown(); } }

3、springboot使用oss上传图片

<!-- oss--> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> </dependency> <!-- 日期工具栏依赖 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency>

配置yml中aliyun的信息,默认不提醒,适用于service注入属性

server: port: 7002 spring: application: name: oss-service aliyun: oss: file: endpoint: oss-cn-beijing.aliyuncs.com keyid: keysecret: bucketname: aaa-hdw-edu

service层实现

import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.hdw.ossService.service.OssService; 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.IOException; import java.io.InputStream; import java.util.UUID; @Service public class OssServiceImpl implements OssService { @Value("${aliyun.oss.file.endpoint}") private String endpoint; @Value("${aliyun.oss.file.keyid}") private String accessKeyId; @Value("${aliyun.oss.file.keysecret}") private String accessKeySecret ; @Value("${aliyun.oss.file.bucketname}") private String bucketname ; //图片上传 public String uploadImage(MultipartFile file) { try { // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上传文件流。 InputStream inputStream = file.getInputStream(); //构建日期路径:avatar/2019/02/26/文件名 String filePath = new DateTime().toString("yyyy/MM/dd"); //文件名:uuid.扩展名 String original = file.getOriginalFilename(); //获取上传的文件名 a.jpg String fileName = UUID.randomUUID().toString(); String fileType = original.substring(original.lastIndexOf(".")); String newName = fileName + fileType; String fileUrl = filePath + "/" + newName; ossClient.putObject(bucketname,fileUrl , inputStream); // 关闭OSSClient。 ossClient.shutdown(); return "http://"+bucketname+"."+endpoint+"/"+fileUrl; }catch (Exception e){ e.printStackTrace(); } return null; } }

控制层controller

@RestController @Api(tags = "图片上传") @RequestMapping("ossService/eduoss") public class OssController { @Autowired private OssService ossService; @PostMapping("/uploadimage") @ApiOperation("上传头像") public R upLoadImage(MultipartFile file){ String url = ossService.uploadImage(file); return R.ok().data("url",url); } }
最新回复(0)