Spring BootでAWS S3に接続してファイルのダウンロード・アップロードを行う方法をメモしておきます。AWS S3の公式ページがとても参考になりました。
環境
- macOS Mojave 10.14.4 | |
- Java 8 | |
- Gradle 5.2.1 |
設定ファイル
build.gradle
plugins { | |
id 'org.springframework.boot' version '2.1.4.RELEASE' | |
id 'java' | |
} | |
apply plugin: 'io.spring.dependency-management' | |
group = 'com.example' | |
version = '0.0.1-SNAPSHOT' | |
sourceCompatibility = '1.8' | |
repositories { | |
mavenCentral() | |
} | |
ext { | |
set('springCloudVersion', 'Greenwich.SR1') | |
} | |
dependencies { | |
implementation 'org.springframework.boot:spring-boot-starter' | |
implementation 'org.springframework.cloud:spring-cloud-starter-aws' | |
testImplementation 'org.springframework.boot:spring-boot-starter-test' | |
} | |
dependencyManagement { | |
imports { | |
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" | |
} | |
} |
build.gradle
については、Spring Initializrで依存性にAWS Core
を追加しただけです。これだけでAWS SDK for Javaを利用できるようになります。それぞれの項目の詳しい説明は以下のAWS公式ページをご覧ください。
application.yml
cloud: | |
aws: | |
credentials: | |
# アクセスキー ID | |
accessKey: ${AWS_ACCESS_KEY_ID} | |
# シークレットアクセスキー | |
secretKey: ${AWS_SECRET_ACCESS_KEY} | |
stack: | |
# CloudFormationのスタック名を自動取得しない | |
auto: false | |
region: | |
# リージョン名を自動取得しない | |
auto: false | |
# リージョン名(例. ap-northeast-1) | |
static: ${AWS_REGION} |
Spring BootでAWSを利用するための設定です。詳細は以下記事をご覧ください。
Spring Bootでローカル環境からAWSを利用する際の設定 - Reasonable Code
アプリケーションクラス
@SpringBootApplication | |
public class DemoApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
} |
おなじみのSpring Bootのアプリケーションクラスです。
S3からダウンロードする
@Component | |
public class DemoDownload implements CommandLineRunner { | |
@Override | |
public void run(String... args) { | |
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); | |
try { | |
// S3のオブジェクトを取得する | |
S3Object o = s3.getObject("バケット名", "オブジェクト名"); | |
S3ObjectInputStream s3is = o.getObjectContent(); | |
// ダウンロード先のファイルパスを指定する | |
FileOutputStream fos = new FileOutputStream(new File("ファイルパス")); | |
// S3のオブジェクトを1024byteずつ読み込み、ダウンロード先のファイルに書き込んでいく | |
byte[] read_buf = new byte[1024]; | |
int read_len = 0; | |
while ((read_len = s3is.read(read_buf)) > 0) { | |
fos.write(read_buf, 0, read_len); | |
} | |
s3is.close(); | |
fos.close(); | |
} catch (AmazonServiceException e) { | |
System.err.println(e.getErrorMessage()); | |
System.exit(1); | |
} catch (FileNotFoundException e) { | |
System.err.println(e.getMessage()); | |
System.exit(1); | |
} catch (IOException e) { | |
System.err.println(e.getMessage()); | |
System.exit(1); | |
} | |
System.out.println("Done!"); | |
} | |
} |
「S3のオブジェクトを取得する」→「ダウンロード先のファイルパスを指定する」→「S3のオブジェクトを読み込み、ダウンロード先のファイルに書き込む」という流れで処理しています。簡単ですね。
S3にアップロードする
@Component | |
public class DemoUpload implements CommandLineRunner { | |
@Override | |
public void run(String... args) { | |
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); | |
try { | |
// ファイルをS3にアップロードする | |
s3.putObject("バケット名", "オブジェクト名", new File("ファイルパス")); | |
} catch (AmazonServiceException e) { | |
System.err.println(e.getErrorMessage()); | |
System.exit(1); | |
} | |
System.out.println("Done!"); | |
} | |
} |
こちらはもっと簡単ですね。たった数行でローカルのファイルをS3にアップロードできました。
まとめ
Spring BootでAWS S3に接続してファイルのダウンロード・アップロードを行う方法でした。ソースはGitHubにも載せています。
donchan922/spring-boot-s3-sample