Browse Source

JSY-28635 fix:导出超过2G的文件写s3会异常,临时处理一下

persist/jsy-11.0
seth.tian 2 years ago
parent
commit
50e2a0cdf7
  1. 42
      src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java

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

@ -13,15 +13,22 @@ 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;
@ -36,6 +43,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 +126,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 +157,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));
file.deleteOnExit();
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 {
file.deleteOnExit();
}
} 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