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 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); } }