|
|
|
@ -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 { |
|
|
|
|