dengsixing 3 years ago
parent
commit
2f282e8f75

+ 42 - 13
4dkankan-utils-fyun/src/main/java/com/fdkankan/fyun/oss/UploadToOssUtil.java

@@ -11,6 +11,8 @@ import com.aliyun.oss.model.*;
 import com.amazonaws.HttpMethod;
 import com.amazonaws.auth.AWSStaticCredentialsProvider;
 import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.client.builder.AwsClientBuilder;
+import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
 import com.amazonaws.regions.Regions;
 import com.amazonaws.services.s3.AmazonS3;
 import com.amazonaws.services.s3.AmazonS3ClientBuilder;
@@ -59,7 +61,7 @@ public class UploadToOssUtil {
 	Configuration config = new Configuration(zone);
 	UploadManager uploadManager = new UploadManager(config);
 
-	@Value("${oss.point:http://oss-cn-shenzhen-internal.aliyuncs.com}")
+	@Value("${oss.point}")
 	private String point;
 
 	@Value("${oss.key:LTAIUrvuHqj8pvry}")
@@ -262,7 +264,9 @@ public class UploadToOssUtil {
 		BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 		AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 			.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-			.withRegion(Regions.EU_WEST_2)
+			.withPathStyleAccessEnabled(true)
+			.withEndpointConfiguration(
+				new EndpointConfiguration(this.point, null))
 			.build();
 		try {
 			com.amazonaws.services.s3.model.ListObjectsRequest listObjectsRequest = new com.amazonaws.services.s3.model.ListObjectsRequest();
@@ -308,10 +312,15 @@ public class UploadToOssUtil {
 		}
 	}
 	public void uploadAws(byte[] data,String objectName){
+		log.info("s3key=" + s3key);
+		log.info("s3secrey=" + s3secrey);
 		BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 		AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 			.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-			.withRegion(Regions.EU_WEST_2)
+			.withPathStyleAccessEnabled(true)
+			.withEndpointConfiguration(
+				new EndpointConfiguration(this.point, null))
+//			.withRegion(Regions.EU_WEST_2)
 			.build();
 		try {
 			com.amazonaws.services.s3.model.ObjectMetadata metadata = new com.amazonaws.services.s3.model.ObjectMetadata();
@@ -460,7 +469,9 @@ public class UploadToOssUtil {
 
 		AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
 				.withCredentials(new AWSStaticCredentialsProvider(awsCred))
-				.withRegion(Regions.EU_WEST_2)
+				.withPathStyleAccessEnabled(true)
+				.withEndpointConfiguration(
+					new EndpointConfiguration(this.point, null))
 				.build();
 
 		// Set the pre-signed URL to expire after one hour.
@@ -539,7 +550,9 @@ public class UploadToOssUtil {
 		BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 		AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 				.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-				.withRegion(Regions.EU_WEST_2)
+				.withPathStyleAccessEnabled(true)
+				.withEndpointConfiguration(
+					new EndpointConfiguration(this.point, null))
 				.build();
 
 		try {
@@ -581,7 +594,9 @@ public class UploadToOssUtil {
 		BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 		AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 			.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-			.withRegion(Regions.EU_WEST_2)
+			.withPathStyleAccessEnabled(true)
+			.withEndpointConfiguration(
+				new EndpointConfiguration(this.point, null))
 			.build();
 		try{
 			File file = new File(filePath);
@@ -629,7 +644,9 @@ public class UploadToOssUtil {
 		BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 		AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 				.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-				.withRegion(Regions.EU_WEST_2)
+				.withPathStyleAccessEnabled(true)
+				.withEndpointConfiguration(
+					new EndpointConfiguration(this.point, null))
 				.build();
 
 		if (objectName.startsWith("/")) {
@@ -776,7 +793,9 @@ public class UploadToOssUtil {
 		BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 		AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 			.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-			.withRegion(Regions.EU_WEST_2)
+			.withPathStyleAccessEnabled(true)
+			.withEndpointConfiguration(
+				new EndpointConfiguration(this.point, null))
 			.build();
 
 		try {
@@ -932,7 +951,9 @@ public class UploadToOssUtil {
 			BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 			AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 				.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-				.withRegion(Regions.EU_WEST_2)//s3 地区位置
+				.withPathStyleAccessEnabled(true)
+				.withEndpointConfiguration(
+					new EndpointConfiguration(this.point, null))
 				.build();
 
 			// 复制文件
@@ -960,7 +981,9 @@ public class UploadToOssUtil {
 			BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 			AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 				.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-				.withRegion(Regions.EU_WEST_2)//s3 地区位置
+				.withPathStyleAccessEnabled(true)
+				.withEndpointConfiguration(
+					new EndpointConfiguration(this.point, null))
 				.build();
 			// 复制文件
 			log.info("开始复制:" + sourceKey);
@@ -1074,7 +1097,9 @@ public class UploadToOssUtil {
 			BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 			AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 				.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-				.withRegion(Regions.EU_WEST_2)
+				.withPathStyleAccessEnabled(true)
+				.withEndpointConfiguration(
+					new EndpointConfiguration(this.point, null))
 				.build();
 
 			GetObjectRequest request  = new GetObjectRequest(bucketName,objectName);
@@ -1108,7 +1133,9 @@ public class UploadToOssUtil {
 		BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 		AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 			.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-			.withRegion(Regions.EU_WEST_2)
+			.withPathStyleAccessEnabled(true)
+			.withEndpointConfiguration(
+				new EndpointConfiguration(this.point, null))
 			.build();
 		try {
 			boolean exist = s3.doesObjectExist(bucket, objectName);
@@ -1198,7 +1225,9 @@ public class UploadToOssUtil {
 		BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
 		AmazonS3 s3 = AmazonS3ClientBuilder.standard()
 			.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
-			.withRegion(Regions.EU_WEST_2)
+			.withPathStyleAccessEnabled(true)
+			.withEndpointConfiguration(
+				new EndpointConfiguration(this.point, null))
 			.build();
 		try {
 			GetObjectRequest request  = new GetObjectRequest(this.bucket,objectName);