1、在yml文件中,配置相关信息
aliyun:
oss:
endpoint: #Endpoint以华东1(杭州)为例,其它Region请按实际情况填写
accessKeyId: #自己阿里云的信息
accessKeySecret: #自己阿里云的信息
bucketName: #填写Bucket名称,例如examplebucket
2、创建阿里云对象实体类
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliossProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
3、编写一个配置类,注入阿里云对象实体类,将对象交给IOC容器管理
@Configuration
public class OssConfiguration {
@Autowired
private AliossProperties aliossProperties;
@Bean
@ConditionalOnMissingBean
public AliOssUtil aliOssUtil(){
return new AliOssUtil(aliossProperties.getEndpoint(),
aliossProperties.getAccessKeyId(),
aliossProperties.getAccessKeySecret(),
aliossProperties.getBucketName());
}
}
4、使用阿里云OSS的工具类上传图片
这个工具类是根据阿里云OSS官方工具类改写,可以直接导入使用
@Data
@AllArgsConstructor
public class AliOssUtil {
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
private String endpoint;
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
//EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
private String accessKeyId;
private String accessKeySecret;
// 填写Bucket名称,例如examplebucket。
private String bucketName;
public String uploadFile(String objectName, InputStream inputStream) throws Exception {
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
//String objectName = "001.png";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String url="";
try {
// 填写字符串。
String content = "Hello OSS,你好世界";
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);
// 上传字符串。
PutObjectResult result = ossClient.putObject(putObjectRequest);
//url组成: https://bucket名称,区域节点/objectName
url="https://"+bucketName+"."+endpoint.substring(endpoint.lastIndexOf("/")+1)+"/"+objectName;
} 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 url;
}
5、在service层中导入bean即可使用