本文主要描写在Spring Boot项目里集成AWS SDK连接到AWS S3,实现上传下载功能的具体代码和注意事项。如有不足和错误之处,欢迎指正。
AWS S3(官网): https://www.amazonaws.cn/s3/
AWS SDK for Java(官网):https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/signup-create-iam-user.html
aws-doc-sdk-examples(官方github):https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/s3
aws-s3-java-sample(个人github):https://github.com/maolikui/aws-s3-java-sample
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javasample.s3</groupId>
<artifactId>SpringS3Amazon</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>SpringS3Amazon</name>
<description>SpringS3Amazon</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.0.0-preview-12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.0.0-preview-12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<fork>true</fork>
<executable>
C:\Program Files\Java\jdk1.8.0_171\bin\javac.exe
</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意事项:
pom.xml文件中的关于software.amazon.awssdk的version要保持一致,即以下两处:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.0.0-preview-12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.0.0-preview-12</version>
</dependency>
server.port=9001
server.servlet.path=/rest/services
spring.servlet.multipart.max-file-size=2500MB
spring.servlet.multipart.max-request-size=2500MB
aws.access_key_id=your access key
aws.secret_access_key=your secret access key
aws.s3.bucket=your bucket name
aws.s3.region=cn-northwest-1
aws.s3.filename=/666666/test123/photo.zip
注意事项:
1.如果上传的文件过大,需要设置spring.servlet.multipart.max-file-size和spring.servlet.multipart.max-request-size的值。
2.access_key_id和secret_access_key是通过IAM账户创建生成的。
3.bucket和region要对应。
package com.javasample.s3.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
@Configuration
public class S3Config {
@Value("${aws.access_key_id}")
private String awsId;
@Value("${aws.secret_access_key}")
private String awsKey;
@Value("${aws.s3.region}")
private String region;
@Bean
public S3Client s3client() {
AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
awsId,
awsKey);
S3Client s3Client = S3Client.builder().region(Region.of(region)).credentialsProvider(StaticCredentialsProvider.create(awsCreds)).build();
return s3Client;
}
}
package com.javasample.s3.services.impl;
import com.javasample.s3.services.S3Services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
@Service
public class S3ServicesImpl implements S3Services {
private Logger logger = LoggerFactory.getLogger(S3ServicesImpl.class);
@Value("${aws.s3.bucket}")
private String bucketName;
@Value("${aws.s3.filename}")
private String fileName;
@Autowired
private S3Client s3Client;
@Override
public ResponseInputStream<GetObjectResponse> downloadFile(String keyName) {
ResponseInputStream<GetObjectResponse> responseInputStream = null;
try {
logger.info("---------------- START DOWNLOAD FILE ----------------");
logger.info("Downloading to bucket '" + bucketName);
responseInputStream = s3Client.getObject(GetObjectRequest.builder().bucket(bucketName).key(keyName).build());
logger.info("===================== Download File - Done! =====================");
} catch (Exception e) {
logger.info("Exception e:" + e.toString());
}
return responseInputStream;
}
@Override
public PutObjectResponse uploadFile(MultipartFile file) {
PutObjectResponse putObjectResponse = null;
try {
logger.info("---------------- START UPLOAD FILE ----------------");
logger.info("Uploading to bucket '" + bucketName);
putObjectResponse = s3Client.putObject(PutObjectRequest.builder().bucket(bucketName).key(fileName).build(), RequestBody.fromBytes(file.getBytes()));
logger.info("===================== Upload File - Done! =====================");
} catch (Exception e) {
logger.info("Exception e:" + e.toString());
}
return putObjectResponse;
}
}
注意事项:
在上传和下载的方法里,方法key的参数指的是存储在AWS S3上的文件名。如果是路径字符串格式,S3服务器上会自动创建对应路径的文件夹。
例如:该例application.properties中的aws.s3.filename=/666666/test123/photo.zip,如果key的参数为/666666/test123/photo.zip,那么S3存储桶下就会有路径为/666666/test123/photo.zip的文件,该文件就是上传的文件。