阿里云OSS文档
阿里云服务文档
Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown
参考博客
解决方案
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-oss-spring-boot-starterartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-oss-spring-boot-starterartifactId>
<version>1.0.0version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
启动报错:
Description:
Field ossClient in com.rymall.file.service.OssSysFileServiceImpl required a bean of type ‘com.aliyun.oss.OSSClient’ that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type ‘com.aliyun.oss.OSSClient’ in your configuration.
Process finished with exit code 1
参考网址
github OSS中的ISSUE
解决方案:
@Autowired
private OSSClient ossClient;
应该改为
@Autowired
private OSS ossClient;
Oss endpoint can’t be empty.
启动配置正确写法
alibaba:
cloud:
access-key: 8YmeeBJBg
secret-key: IFJn2P8GylI
oss:
endpoint: oss-cn-shenzhen.aliyuncs.com
注意图片配置alibaba是父节点
文件过服务器,再转阿里OSS
package com.rymall.file.service;
import com.aliyun.oss.*;
import com.aliyun.oss.model.Callback;
import com.rymall.file.config.MinioConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import static com.rymall.file.utils.FileUploadUtils.extractFilename;
@Primary
@Service
public class OssSysFileServiceImpl implements ISysFileService {
@Autowired
private OSS ossClient;
/**
* 本地文件上传接口
*
* @param file 上传的文件
* @return 访问地址
* @throws Exception
*/
public String uploadFile(MultipartFile file) throws Exception {
try {
// 将MultipartFile转为文件流
InputStream inputStream = file.getInputStream();
// 创建PutObject请求。
//extractFilename 若依框架生成文件名字
ossClient.putObject("rymall-product", extractFilename(file), inputStream);
} 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();
}
}
return null;
}
}
alibaba:
cloud:
access-key: LTAI5tLbJvNXyWs4gEMxw5XE
secret-key: g5D1D0FJT6zqAECy9nRSZnxNGTk48k
oss:
endpoint: oss-cn-shenzhen.aliyuncs.com
bucket: rymall-product
host: https://rymall-product.oss-cn-shenzhen.aliyuncs.com
@Autowired
private OSS ossClient;
@Value("${alibaba.cloud.oss.bucket}")
private String bucket;
@Value("${alibaba.cloud.oss.endpoint}")
private String endpoint;
@Value("${alibaba.cloud.oss.host}")
private String host;
@Value("${alibaba.cloud.access-key}")
private String accessId;
@Value("${alibaba.cloud.secret-key}")
private String accessKey;
@Override
public Map<String, String> policy(){
// 设置上传到OSS文件的前缀,可置空此项。置空后,文件将上传至Bucket的根目录下。
String dir = "test/";
OSSClient client = new OSSClient(endpoint, accessId, accessKey);
Map<String, String> respMap = new LinkedHashMap<String, String>();
try {
// 过期时间秒
long expireTime = 3000;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = client.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
respMap.put("accessid", accessId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
return respMap;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return respMap;
}