22
33import com .aliyun .oss .OSSClient ;
44import com .aliyun .oss .model .*;
5- import lombok .AllArgsConstructor ;
5+ import lombok .Setter ;
6+ import org .minbox .framework .api .boot .plugin .oss .progress .ApiBootObjectStorageProgress ;
7+ import org .minbox .framework .api .boot .plugin .oss .progress .OssProgressListener ;
68import org .minbox .framework .api .boot .plugin .storage .ApiBootObjectStorageService ;
79import org .minbox .framework .api .boot .plugin .storage .exception .ApiBootObjectStorageException ;
810import org .minbox .framework .api .boot .plugin .storage .response .ApiBootObjectStorageResponse ;
2729 * Gitee:https://gitee.com/hengboy
2830 * GitHub:https://github.com/hengboy
2931 */
30- @ AllArgsConstructor
3132public class ApiBootOssService implements ApiBootObjectStorageService {
3233 /**
3334 * 地域性的endpoint
@@ -49,13 +50,26 @@ public class ApiBootOssService implements ApiBootObjectStorageService {
4950 * 自定义域名
5051 */
5152 protected String domain ;
53+ /**
54+ * ApiBoot Oss Progress
55+ */
56+ @ Setter
57+ private ApiBootObjectStorageProgress apiBootObjectStorageProgress ;
58+
59+ public ApiBootOssService (String endpoint , String bucketName , String accessKeyId , String accessKeySecret , String domain ) {
60+ this .endpoint = endpoint ;
61+ this .bucketName = bucketName ;
62+ this .accessKeyId = accessKeyId ;
63+ this .accessKeySecret = accessKeySecret ;
64+ this .domain = domain ;
65+ }
5266
5367 @ Override
5468 public ApiBootObjectStorageResponse upload (String objectName , byte [] bytes ) throws ApiBootObjectStorageException {
5569 try {
5670 OSSClient ossClient = getOssClient ();
5771 // put byte inputStream
58- ossClient .putObject (bucketName , objectName , new ByteArrayInputStream (bytes ));
72+ ossClient .putObject (new PutObjectRequest ( bucketName , objectName , new ByteArrayInputStream (bytes )). withProgressListener ( new OssProgressListener ( objectName , apiBootObjectStorageProgress ) ));
5973 closeOssClient (ossClient );
6074 } catch (Exception e ) {
6175 throw new ApiBootObjectStorageException (e .getMessage (), e );
@@ -68,7 +82,7 @@ public ApiBootObjectStorageResponse upload(String objectName, InputStream inputS
6882 try {
6983 OSSClient ossClient = getOssClient ();
7084 // put byte inputStream
71- ossClient .putObject (bucketName , objectName , inputStream );
85+ ossClient .putObject (new PutObjectRequest ( bucketName , objectName , inputStream ). withProgressListener ( new OssProgressListener ( objectName , apiBootObjectStorageProgress )) );
7286 closeOssClient (ossClient );
7387 } catch (Exception e ) {
7488 throw new ApiBootObjectStorageException (e .getMessage (), e );
@@ -81,7 +95,7 @@ public ApiBootObjectStorageResponse upload(String objectName, String localFile)
8195 try {
8296 OSSClient ossClient = getOssClient ();
8397 // put byte inputStream
84- ossClient .putObject (bucketName , objectName , new File (localFile ));
98+ ossClient .putObject (new PutObjectRequest ( bucketName , objectName , new File (localFile )). withProgressListener ( new OssProgressListener ( objectName , apiBootObjectStorageProgress ) ));
8599 closeOssClient (ossClient );
86100 } catch (Exception e ) {
87101 throw new ApiBootObjectStorageException (e .getMessage (), e );
@@ -93,7 +107,7 @@ public ApiBootObjectStorageResponse upload(String objectName, String localFile)
93107 public void download (String objectName , String localFile ) throws ApiBootObjectStorageException {
94108 try {
95109 OSSClient ossClient = getOssClient ();
96- ossClient .getObject (new GetObjectRequest (bucketName , objectName ), new File (localFile ));
110+ ossClient .getObject (new GetObjectRequest (bucketName , objectName ). withProgressListener ( new OssProgressListener ( objectName , apiBootObjectStorageProgress )) , new File (localFile ));
97111 closeOssClient (ossClient );
98112 } catch (Exception e ) {
99113 throw new ApiBootObjectStorageException (e .getMessage (), e );
@@ -242,4 +256,6 @@ protected String getObjectUrl(String objectName) {
242256 }
243257 return getDefaultObjectUrl (objectName );
244258 }
259+
260+
245261}
0 commit comments