Browse Source

Pull request #203: JSY-29167 && JSY-28635

Merge in PG/plugin-repository-s3 from ~SETH.TIAN/plugin-repository-s3:persist/jsy-11.0 to persist/jsy-11.0

* commit '710182a7be2d8fc52c2d719fbb530dc40f734f77':
  JSY-29167 update:误提了一行
  JSY-29167 fix:文件删除
  JSY-28635 fix:导出超过2G的文件写s3会异常,临时处理一下
persist/jsy-11.0
Seth.Tian-田新兴 2 years ago committed by Icey.Zhang-张洁
parent
commit
6555c26f52
  1. 43
      src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java

43
src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java

@ -13,18 +13,26 @@ import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import com.fr.config.utils.RandomUtils;
import com.fr.io.repository.FineFileEntry;
import com.fr.io.repository.base.BaseResourceRepository;
import com.fr.log.FineLoggerFactory;
import com.fr.stable.Filter;
import com.fr.workspace.resource.ResourceIOException;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
@ -36,6 +44,11 @@ import java.util.List;
public class S3ResourceRepository extends BaseResourceRepository {
private static final String DELIMITER = "/";
/**
* 1.5G
*/
private static final int LENGTH_LIMIT = 1610612736;
private static final String LINK_SYMBOL = "_";
private final AmazonS3 readClient;
private final AmazonS3 writeClient;
@ -114,7 +127,7 @@ public class S3ResourceRepository extends BaseResourceRepository {
try {
return readClient.getObject(request).getObjectContent();
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e, "[S3] read path {} is error!", filePath);
FineLoggerFactory.getLogger().warn(e, "[S3] read path {} is error!", filePath);
return new ByteArrayInputStream(new byte[0]);
}
}
@ -145,8 +158,32 @@ public class S3ResourceRepository extends BaseResourceRepository {
} else {
ObjectMetadata metadata = new ObjectMetadata();
try {
metadata.setContentLength(inputStream.available());
this.writeClient.putObject(this.bucket, path, inputStream, metadata);
// 大于1.5G的,就自己算一下长度
if (inputStream.available() > LENGTH_LIMIT) {
final long l1 = System.currentTimeMillis();
final File file = new File(l1 + LINK_SYMBOL + RandomUtils.randomId(5));
Files.deleteIfExists(file.toPath());
file.createNewFile();
try (final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));) {
final long l = inputStream.transferTo(bufferedOutputStream);
bufferedOutputStream.flush();
metadata.setContentLength(l);
TransferManager tm = TransferManagerBuilder.standard()
.withS3Client(this.writeClient)
.build();
// TransferManager processes all transfers asynchronously,
// so this call returns immediately.
Upload upload = tm.upload(this.bucket, path, file);
// Optionally, wait for the upload to finish before continuing.
upload.waitForCompletion();
} finally {
Files.deleteIfExists(file.toPath());
}
} else {
metadata.setContentLength(inputStream.available());
this.writeClient.putObject(this.bucket, path, inputStream, metadata);
}
} catch (Exception e) {
FineLoggerFactory.getLogger().info("S3 inputStream.putObject error,path is {}, error message {}", path, e.getMessage());
} finally {

Loading…
Cancel
Save