阿里云OSS
的。
一、准备环境 1、在搜索框搜索 对象存储OSS 2、创建Bucket 3、找到开发相关信息
获取
endpoint
, 点击指定的 Bucket ,地域不同
的 Bucket 的 endpoint 是不一样的二、具体代码 1、Maven依赖
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>3.14.0version>
dependency>
2、application.yml配置
#阿里云存储文件系统配置
file:
store:
ali:
oss:
endpoint: xxxxx
bucket: xxxxx
accessKey: xxxxx
secretKey: xxxxx
3、AliOssProperties配置类
package com.central.filesystem.common.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "file.store.ali.oss")
@RefreshScope
public class AliOssProperties {
private String endpoint;
private String bucket;
private String accessKey;
private String secretKey;
}
4、AliOssUtil类
package com.central.filesystem.common.utils;
import com.aliyun.oss.*;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;
import com.central.filesystem.common.config.AliOssProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class AliOssUtil {
@Autowired
private AliOssProperties aliOssProperties;
private static final Logger logger = LoggerFactory.getLogger(AliOssUtil.class);
private static OSS ossClient;
public AliOssUtil(AliOssProperties aliOssProperties) {
this.aliOssProperties = aliOssProperties;
}
@PostConstruct
public void init() {
try {
ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKey(), aliOssProperties.getSecretKey());
} catch (Exception e) {
logger.error("初始化阿里云配置异常", e.fillInStackTrace());
}
}
public String upload(String uploadFile, String objectFile, Boolean shutdown) {
//OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKey(), aliOssProperties.getSecretKey());
String location = "";
try {
UploadFileRequest uploadFileRequest = new UploadFileRequest(aliOssProperties.getBucket(), objectFile);
// The local file to upload---it must exist.
uploadFileRequest.setUploadFile(uploadFile);//文件本地物理路径
// Sets the concurrent upload task number to 5.
uploadFileRequest.setTaskNum(5);
// Sets the part size to 1MB.
uploadFileRequest.setPartSize(1024 * 1024 * 1);
// Enables the checkpoint file. By default it's off.
uploadFileRequest.setEnableCheckpoint(true);
UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
CompleteMultipartUploadResult multipartUploadResult =
uploadResult.getMultipartUploadResult();
location = multipartUploadResult.getLocation();
logger.info("aliyun-oss文件[{}]上传成功,新文件名:{}", uploadFile, objectFile);
// logger.info("======" + multipartUploadResult.getETag());
} catch (OSSException oe) {
logger.info("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
logger.info("Error Message: " + oe.getErrorMessage());
logger.info("Error Code: " + oe.getErrorCode());
logger.info("Request ID: " + oe.getRequestId());
logger.info("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
logger.info("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.");
logger.info("Error Message: " + ce.getMessage());
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (shutdown) {
ossClient.shutdown();
}
return location;
}
}
public void shutdown() {
try {
ossClient.shutdown();
} catch (Exception e) {
logger.error("shutdown fail", e.fillInStackTrace());
}
}
}
三、测试
测试方法
@Autowired(required = false)
private AliOssUtil aliOssUtil;
@Test
public void ossTest(){
aliOssUtil.upload("C:\Users\kk\Desktop\2222.webp","test/image/2222.webp",false);
}
查看阿里云oss,图片已上传成功!总结
参考
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)