阿里云oss文件上传
所需依赖
1 2 3 4 5
| <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency>
|
其次获取到目标阿里云的AccessKey ID和AccessKey Secret填入yml中,如图所示
1 2 3 4 5 6 7
| aliyun: oss: file: endpoint: oss-cn-hangzhou.aliyuncs.com // 节点,此处为(华东1)杭州 keyid: LxxxxxxxxxxxxxW keysecret: txxxxxxxxxxxRM bucketname: leebook // 目标桶名称
|
编写OSS工具类OssUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package com.ll.aliyunoss.utils;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;
@Component public class OssUtils 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 KEY_ID; public static String KEY_SECRET; public static String BUCKET_NAME;
@Override public void afterPropertiesSet() throws Exception { ENDPOINT = endpoint; KEY_ID = keyid; KEY_SECRET = keysecret; BUCKET_NAME = bucketname; } }
|
具体执行代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| @Override public String uploadFile(MultipartFile file) { String endpoint = OssUtils.ENDPOINT; String keyId = OssUtils.KEY_ID; String keySecret = OssUtils.KEY_SECRET; String bucketName = OssUtils.BUCKET_NAME;
try { OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
InputStream inputStream = file.getInputStream();
String filename = file.getOriginalFilename();
String datePath = new DateTime().toString("yyyy-MM-dd"); filename = datePath + filename;
ossClient.putObject(bucketName,filename,inputStream); ossClient.shutdown();
String url = "https://" + bucketName + "." + endpoint + "/" + filename;
return url; } catch (Exception e) { e.printStackTrace(); return null; } }
|
如果是想直接获取返回地址,则可以用以下的方法,但是注意的是,这个方法必须设置过期时间,这里天坑!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static String uploadFile(MultipartFile multipartFile) throws IOException { OSSClient ossClient = new OSSClient(ENDPOINT, KEY_ID, KEY_SECRET); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String fileName = multipartFile.getOriginalFilename(); String suffixName = fileName.substring(fileName.lastIndexOf(".")); String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + suffixName; String objectName = sdf.format(new Date()) + "/" + finalFileName; ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType("image/jpg"); ossClient.putObject(BUCKET_NAME, objectName, new ByteArrayInputStream(multipartFile.getBytes()),objectMetadata); Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); String url = ossClient.generatePresignedUrl(BUCKET_NAME, objectName, expiration).toString(); ossClient.shutdown(); return url;
|
此外,如果你把图片上传了oss,但是预览尺寸需要指定,则可以采用以下方法,此处设置的预览尺寸是高80宽80,可自行修改
1
| https://your-bucket-name.oss-cn-hangzhou.aliyuncs.com/your-image.jpg?x-oss-process=image/resize,m_fill,w_80,h_80
|