AwsAttachmentServiceImpl.java
3.36 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.hotent.file.attachmentService;
import cn.hutool.core.io.IoUtil;
import com.amazonaws.services.s3.model.*;
import com.hotent.base.attachment.Attachment;
import com.hotent.base.attachment.AttachmentService;
import com.hotent.base.attachment.MultipartFileParam;
import com.hotent.base.attachment.UploadShardResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.file.model.AwsConfig;
import com.hotent.file.util.AppFileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* 亚马逊S3存储服务实现类
*/
@Service
public class AwsAttachmentServiceImpl implements AttachmentService {
@Value("${aws.bucketName:null}")
private String bucketName;
@Resource
AwsConfig awsConfig;
@Override
public String getStoreType() {
return "aws";
}
@Override
public void remove(Attachment attachment, String propertiesId) throws Exception {
}
@Override
public void upload(Attachment attachment, InputStream inputStream, String propertiesId) throws Exception {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(attachment.getContentType());
objectMetadata.setContentLength(BeanUtils.isNotEmpty(attachment.getByteCount())?attachment.getByteCount():0);
String keyStr = attachment.getFilePath();//保存文件路径
awsConfig.getAmazonS3().putObject(new PutObjectRequest(bucketName, keyStr, inputStream, objectMetadata));
}
@Override
public void download(Attachment attachment, OutputStream outStream, String propertiesId) throws Exception {
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName,attachment.getFilePath());
S3Object s3Object = awsConfig.getAmazonS3().getObject(getObjectRequest);
S3ObjectInputStream objectContent = s3Object.getObjectContent();
IoUtil.copy(objectContent,outStream);
IoUtil.close(objectContent);
IoUtil.close(outStream);
IoUtil.close(s3Object);
}
@Override
public boolean chekckFile(Attachment attachment, String propertiesId) throws Exception {
return false;
}
@Override
public byte[] getFileBytes(Attachment sysFile) throws Exception {
return new byte[0];
}
/**
* 分片上传附件
*
* @param multipartFileParam
* @param attachment
* @param propertiesId
* @throws Exception
*/
@Override
public UploadShardResult uploadByShard(MultipartFileParam multipartFileParam, Attachment attachment, String propertiesId) throws Exception {
return null;
}
@Override
public Map<String, Object> generatePolicy(Attachment attachment, String propertiesId) {
return null;
}
@Override
public String getUrl(Attachment attachment, String propertiesId) throws UnsupportedEncodingException {
return null;
}
@Override
public InputStream getFileInputStream(Attachment attachment) throws FileNotFoundException {
return null;
}
@Override
public String getFilePath(String account, String fileName) {
return AppFileUtil.getFilePath(account,fileName);
}
}