|
@@ -27,6 +27,10 @@ public class OssTemplate implements InitializingBean {
|
|
|
private final OssProperties ossProperties;
|
|
|
private AmazonS3 amazonS3;
|
|
|
|
|
|
+ public OssTemplate(OssProperties ossProperties) {
|
|
|
+ this.ossProperties = ossProperties;
|
|
|
+ }
|
|
|
+
|
|
|
public void createBucket(String bucketName) {
|
|
|
try {
|
|
|
if (!this.amazonS3.doesBucketExistV2(bucketName)) {
|
|
@@ -94,8 +98,16 @@ public class OssTemplate implements InitializingBean {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取文件大小
|
|
|
+ */
|
|
|
+ public long getSize(String bucketName, String objectName) {
|
|
|
+ List<S3ObjectSummary> apks = amazonS3.listObjects(bucketName, objectName).getObjectSummaries();
|
|
|
+ return apks.isEmpty() ? 0 : apks.get(0).getSize();
|
|
|
+ }
|
|
|
+
|
|
|
public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
|
|
|
- this.putObject(bucketName, objectName, stream, (long)stream.available(), "application/octet-stream");
|
|
|
+ this.putObject(bucketName, objectName, stream, (long) stream.available(), "application/octet-stream");
|
|
|
}
|
|
|
|
|
|
public PutObjectResult putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
|
|
@@ -137,10 +149,6 @@ public class OssTemplate implements InitializingBean {
|
|
|
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(this.ossProperties.getEndpoint(), this.ossProperties.getRegion());
|
|
|
AWSCredentials awsCredentials = new BasicAWSCredentials(this.ossProperties.getAccessKey(), this.ossProperties.getSecretKey());
|
|
|
AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
|
|
|
- this.amazonS3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder) AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)).withClientConfiguration(clientConfiguration)).withCredentials(awsCredentialsProvider)).disableChunkedEncoding()).withPathStyleAccessEnabled(this.ossProperties.getPathStyleAccess())).build();
|
|
|
- }
|
|
|
-
|
|
|
- public OssTemplate(OssProperties ossProperties) {
|
|
|
- this.ossProperties = ossProperties;
|
|
|
+ this.amazonS3 = (AmazonS3) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)).withClientConfiguration(clientConfiguration)).withCredentials(awsCredentialsProvider)).disableChunkedEncoding()).withPathStyleAccessEnabled(this.ossProperties.getPathStyleAccess())).build();
|
|
|
}
|
|
|
}
|