不多说,全是干货,上菜。
1. Maven引入
我这里因为有其他的模块,所以依赖都引入了,你们可以根据自身需要,酌情删减依赖。
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3-transfer-manager</artifactId>
<version>2.17.103-PREVIEW</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3control</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sts</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.16.60</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.837</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. java 代码配置
需要注意的是,我是使用的配置的方式输入秘钥和key ,别忘了填写桶名称和配置桶的区域,且一定要对应。
public static void uploadDirectory(MultipartFile[] files) {
List<CORSRule.AllowedMethods> rule1AM = new ArrayList<CORSRule.AllowedMethods>();
rule1AM.add(CORSRule.AllowedMethods.PUT);
rule1AM.add(CORSRule.AllowedMethods.POST);
rule1AM.add(CORSRule.AllowedMethods.DELETE);
List<CORSRule.AllowedMethods> rule2AM = new ArrayList<CORSRule.AllowedMethods>();
rule2AM.add(CORSRule.AllowedMethods.GET);
rule2AM.add(CORSRule.AllowedMethods.POST);
rule2AM.add(CORSRule.AllowedMethods.PUT);
CORSRule rule2 = new CORSRule().withId("CORSRule2")
.withMaxAgeSeconds(3000)
.withAllowedMethods(rule2AM)
.withAllowedOrigins(Arrays.asList("*"))
.withAllowedHeaders(Arrays.asList("*"));
List<CORSRule> rules = new ArrayList<CORSRule>();
//rules.add(new CORSRule().withId("CORSRule1").withAllowedMethods(rule1AM).withAllowedOrigins(Arrays.asList("https://*.bragaming.com")));
rules.add(rule2);
// Add the rules to a new CORS configuration.
BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();
configuration.setRules(rules);
AWSCredentials awsCredentials = new BasicAWSCredentials("accessKey", "secretKet");
AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
String bucketName = "桶名称";
builder.setRegion("区域");
AmazonS3 s3Client = builder.build();
//设置跨域
s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);
for (MultipartFile file : files) {
try {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
objectMetadata.setContentLength(file.getSize());
String key = file.getName();
InputStream inputStream = file.getInputStream();
PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
s3Client.putObject(request.withCannedAcl(CannedAccessControlList.PublicRead));
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
以上就是完整配置了,Over。
资料参考 MB博客 - 每日分享精彩资源