{"id":443996,"date":"2025-01-02T21:01:50","date_gmt":"2025-01-02T21:01:50","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=443996"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2025-01-02T21:01:50","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=443996","title":{"rendered":"<span>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 Yandex Object Storage \u0432 Spring Boot<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><a href=\"https:\/\/yandex.cloud\/ru\/docs\/storage\/\" rel=\"noopener noreferrer nofollow\">Yandex Cloud Object Storage<\/a> &#8212; \u044d\u0442\u043e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0435 \u0441 AWS S3 \u043e\u0431\u043b\u0430\u0447\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u0443\u0435\u043c \u0435\u0433\u043e \u0432 Spring Boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f SDK \u0410\u043c\u0430\u0437\u043e\u043d\u0430. <\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0431\u0430\u043a\u0435\u0442\u0430 \u0432 Yandex Cloud<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0437\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/db5\/941\/306\/db5941306c9fa4d296741a37b96d7b7f.png\" width=\"1920\" height=\"1013\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/db5\/941\/306\/db5941306c9fa4d296741a37b96d7b7f.png\"\/><\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c s3<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/64d\/46c\/454\/64d46c4549f83d329011bd49550a87e1.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/64d\/46c\/454\/64d46c4549f83d329011bd49550a87e1.png\"\/><\/figure>\n<p>\u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/adf\/369\/7a3\/adf3697a384bf5d9915a658db0a21a48.png\" width=\"900\" height=\"763\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/adf\/369\/7a3\/adf3697a384bf5d9915a658db0a21a48.png\"\/><\/figure>\n<p>\u0418 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043d\u0430\u0448\u0435 \u0432\u0435\u0434\u0435\u0440\u043a\u043e<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/339\/ce2\/8dd\/339ce28dd43757e8b171a38ecb43c130.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/339\/ce2\/8dd\/339ce28dd43757e8b171a38ecb43c130.png\"\/><\/figure>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0431\u0430\u043a\u0435\u0442\u0443<\/h2>\n<p>\u041a\u0430\u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u0432 <a href=\"https:\/\/yandex.cloud\/en\/docs\/storage\/s3\/\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>, \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f s3 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u043d\u044b\u0439 \u0430\u043a\u043a\u0430\u0443\u043d\u0442.<br \/>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0430\u043f\u043a\u0443, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 Service accounts \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u0430\u043a\u043a\u0430\u0443\u043d\u0442:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f11\/d05\/260\/f11d052608550599b44ee02f369fca49.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f11\/d05\/260\/f11d052608550599b44ee02f369fca49.png\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bd7\/f3a\/068\/bd7f3a06824c15c35de3b9c90931dbe6.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bd7\/f3a\/068\/bd7f3a06824c15c35de3b9c90931dbe6.png\"\/><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0432 \u043d\u0430\u0448 \u0431\u0430\u043a\u0435\u0442 \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0443 security, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u043e\u043b\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0431\u0430\u043a\u0435\u0442\u0430<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/69e\/048\/5e8\/69e0485e8f760e2f5216e96cf9f4d3a0.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/69e\/048\/5e8\/69e0485e8f760e2f5216e96cf9f4d3a0.png\"\/><\/figure>\n<p>\u041d\u0430\u0436\u0438\u043c\u0430\u0435\u043c \u043d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0443 Assign bindings \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u043e\u043b\u044c editor \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f7f\/899\/4d0\/f7f8994d04f64d2c6b07654e0a21e08a.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f7f\/899\/4d0\/f7f8994d04f64d2c6b07654e0a21e08a.png\"\/><\/figure>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0430\u043a\u043a\u0430\u0443\u043d\u0442 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c static access key, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c key id \u0438 secret key<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3fd\/3a1\/df0\/3fd3a1df0d2c692e39effe5360ef7191.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3fd\/3a1\/df0\/3fd3a1df0d2c692e39effe5360ef7191.png\"\/><\/figure>\n<h2>\u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 S3<\/h2>\n<p>Yandex Object Storage \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c \u0441 AWS S3, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u0438\u0445 sdk \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0445 \u044d\u0442\u0443 sdk.<\/p>\n<ol>\n<li>\n<p>com.amazonaws:aws-java-sdk:1.x.x &#8212; \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0439 sdk, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u044d\u0442\u043e\u043c \u0433\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>software.amazon.awssdk:s3:2.x.x &#8212; \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 sdk<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/docs.awspring.io\/spring-cloud-aws\/docs\/3.2.0\/reference\/html\/index.html#spring-cloud-aws-s3\" rel=\"noopener noreferrer nofollow\">io.awspring.cloud:spring-cloud-aws-starter-s3<\/a> &#8212; \u043d\u0430\u0434\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043d\u0430\u0434 sdk, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u0430\u044f \u043a\u043e\u043c\u044c\u044e\u0438\u043d\u0438\u0442\u0438 \u0438 \u0443\u043b\u0443\u0447\u0448\u0430\u044e\u0449\u0430\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e \u0441\u043e \u0441\u043f\u0440\u0438\u043d\u0433\u043e\u043c<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/spring.io\/projects\/spring-cloud-stream\" rel=\"noopener noreferrer nofollow\">org.springframework.cloud.stream.app:aws-s3-app-starters-common<\/a> &#8212; \u0441\u0442\u0430\u0440\u0442\u0435\u0440 \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 spring cloud stream, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439\u0441\u044f \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 S3<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e sdk (\u0432\u0430\u0440\u0438\u0430\u043d\u0442 2), \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0439.<\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 spring boot \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"kotlin\">implementation(\"software.amazon.awssdk:aws-sdk-java:2.29.33\") implementation(\"software.amazon.awssdk:apache-client:2.29.33\")  implementation(\"org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0\") implementation(\"org.springframework.boot:spring-boot-starter-web\")<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 sdk \u043d\u0430\u043c \u0442\u0430\u043a \u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c  http-\u043a\u043b\u0438\u0435\u043d\u0442 \u0430\u043c\u0430\u0437\u043e\u043d\u0430. \u0422\u0430\u043a\u0436\u0435, \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 springdoc-openapi \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 Swagger UI, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c <code>s3Client<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 S3. \u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441\u0442\u043e\u0438\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0431\u0438\u043d\u043e\u043c \u0438 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432\u0441\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432 <code>application.properties<\/code>, \u043d\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044e \u0432\u0441\u0435 \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/photos\") @Tag(name = \"Photos\") @ApiResponses(@ApiResponse(responseCode = \"200\", useReturnTypeSchema = true)) public class PhotoController {      private static final String KEY_ID = \"YOUR_KEY_ID\";     private static final String SECRET_KEY = \"YOUR_SECRET_KEY\";     private static final String REGION = \"ru-central1\";     private static final String S3_ENDPOINT = \"https:\/\/storage.yandexcloud.net\";      private static final String BUCKET = \"spring-boot-s3-exmaple\";      private final S3Client s3Client;      public PhotoController() {         AwsCredentials credentials = AwsBasicCredentials.create(KEY_ID, SECRET_KEY);          s3Client = S3Client.builder()                 .httpClient(ApacheHttpClient.create())                 .region(Region.of(REGION))                 .endpointOverride(URI.create(S3_ENDPOINT))                 .credentialsProvider(StaticCredentialsProvider.create(credentials))                 .build();     } }<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439 \u0432 \u0431\u0430\u043a\u0435\u0442:<\/p>\n<pre><code class=\"java\">@PutMapping(consumes = MULTIPART_FORM_DATA_VALUE) public String uploadFile(@RequestParam MultipartFile photo) throws IOException {      String key = \"photos\/\" + photo.getOriginalFilename();     PutObjectRequest putObjectRequest = PutObjectRequest.builder()             .bucket(BUCKET)             .key(key)             .contentType(photo.getContentType())             .build();      s3Client.putObject(putObjectRequest, RequestBody.fromBytes(photo.getBytes()));      return key; }<\/code><\/pre>\n<p>\u0418 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u0445 \u0438\u0437 \u043d\u0435\u0433\u043e:<\/p>\n<pre><code class=\"java\">@GetMapping public ResponseEntity&lt;byte[]&gt; downloadFile(@RequestParam String key) throws IOException {      GetObjectRequest objectRequest = GetObjectRequest.builder()             .bucket(BUCKET)             .key(key)             .build();      var inputStream = s3Client.getObject(objectRequest);     byte[] data = inputStream.readAllBytes();      var headers = new HttpHeaders();     headers.add(HttpHeaders.CONTENT_DISPOSITION, \"inline\");     headers.add(HttpHeaders.CONTENT_TYPE, inputStream.response().contentType());      return ResponseEntity.ok()             .headers(headers)             .body(data); }<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e:<\/p>\n<pre><code class=\"java\">import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.AwsCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.http.apache.ApacheHttpClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.PutObjectRequest;  import java.io.IOException; import java.net.URI;  import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;  @RestController @RequestMapping(\"\/photos\") @Tag(name = \"Photos\") @ApiResponses(@ApiResponse(responseCode = \"200\", useReturnTypeSchema = true)) public class PhotoController {      private static final String KEY_ID = \"YOR_KEY_ID\";     private static final String SECRET_KEY = \"YOUR_SECRET_KEY\";     private static final String REGION = \"ru-central1\";     private static final String S3_ENDPOINT = \"https:\/\/storage.yandexcloud.net\";      private static final String BUCKET = \"spring-boot-s3-exmaple\";      private final S3Client s3Client;      public PhotoController() {         AwsCredentials credentials = AwsBasicCredentials.create(KEY_ID, SECRET_KEY);          s3Client = S3Client.builder()                 .httpClient(ApacheHttpClient.create())                 .region(Region.of(REGION))                 .endpointOverride(URI.create(S3_ENDPOINT))                 .credentialsProvider(StaticCredentialsProvider.create(credentials))                 .build();     }       @PutMapping(consumes = MULTIPART_FORM_DATA_VALUE)     public String uploadFile(@RequestParam MultipartFile photo) throws IOException {          String key = \"photos\/\" + photo.getOriginalFilename();         PutObjectRequest putObjectRequest = PutObjectRequest.builder()                 .bucket(BUCKET)                 .key(key)                 .contentType(photo.getContentType())                 .build();          s3Client.putObject(putObjectRequest, RequestBody.fromBytes(photo.getBytes()));          return key;     }      @GetMapping     public ResponseEntity&lt;byte[]&gt; downloadFile(@RequestParam String key) throws IOException {          GetObjectRequest objectRequest = GetObjectRequest.builder()                 .bucket(BUCKET)                 .key(key)                 .build();          var inputStream = s3Client.getObject(objectRequest);         byte[] data = inputStream.readAllBytes();          var headers = new HttpHeaders();         headers.add(HttpHeaders.CONTENT_DISPOSITION, \"inline\");         headers.add(HttpHeaders.CONTENT_TYPE, inputStream.response().contentType());          return ResponseEntity.ok()                 .headers(headers)                 .body(data);     } }<\/code><\/pre>\n<h2>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043d\u0430 <a href=\"http:\/\/localhost:8080\/swagger-ui\/index.html\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8080\/swagger-ui\/index.html<\/a> \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0444\u043e\u0442\u043e<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/265\/525\/7ea\/2655257ead5725d5ba1f2e9ed19a16a9.png\" width=\"1420\" height=\"1148\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/265\/525\/7ea\/2655257ead5725d5ba1f2e9ed19a16a9.png\"\/><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u043e\u043d\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u0432 \u0431\u0430\u043a\u0435\u0442\u0435. \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, S3 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u0443\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 <code>\/<\/code> \u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0430\u043f\u043a\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0448\u0435 \u0444\u043e\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u0432 \u043f\u0430\u043f\u043a\u0443 <code>photos<\/code><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b3d\/f65\/4f6\/b3df654f645f76cd91c64bb3337b25aa.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b3d\/f65\/4f6\/b3df654f645f76cd91c64bb3337b25aa.png\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/59c\/947\/e3c\/59c947e3c2b6ec0ad9d7bc64ef71199b.png\" width=\"1920\" height=\"1012\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/59c\/947\/e3c\/59c947e3c2b6ec0ad9d7bc64ef71199b.png\"\/><\/figure>\n<p>\u041f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0444\u043e\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u043d\u0430\u0448 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/aff\/ec1\/1e5\/affec11e51d429b4d51b44fbd63061a7.png\" width=\"1420\" height=\"1230\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/aff\/ec1\/1e5\/affec11e51d429b4d51b44fbd63061a7.png\"\/><\/figure>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e S3 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0440\u0435\u0434\u044b<\/h2>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u0430\u043c \u043d\u0443\u0436\u0435\u043d S3 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043f\u0440\u043e\u0434\u0435, \u0430 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0432\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u0414\u0432\u0430 \u0442\u0430\u043a\u0438\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 &#8212; MinIO \u0438 LocalStack. \u042f \u043d\u0435 \u0431\u0443\u0434\u0443 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u043d\u043e \u043a\u043e\u0440\u043e\u0442\u043a\u043e \u0441\u0440\u0430\u0432\u043d\u044e:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/docs.localstack.cloud\/user-guide\/aws\/s3\/\" rel=\"noopener noreferrer nofollow\">LocalStack<\/a> \u2014 \u044d\u0442\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u043c\u0443\u043b\u0438\u0440\u0443\u0435\u0442 \u043e\u0431\u043b\u0430\u0447\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b AWS. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e S3, \u0442\u043e LocalStack \u0432\u0430\u043c \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/min.io\/product\/overview\" rel=\"noopener noreferrer nofollow\">MinIO<\/a> \u2014 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0435 \u0441 API Amazon S3. \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\ud83d\udc68\u200d\ud83d\udcbb <a href=\"https:\/\/t.me\/life_of_junior_dev\" rel=\"noopener noreferrer nofollow\">\u0414\u0436\u0443\u043d\u0438\u043e\u0440<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/871280\/\"> https:\/\/habr.com\/ru\/articles\/871280\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><a href=\"https:\/\/yandex.cloud\/ru\/docs\/storage\/\" rel=\"noopener noreferrer nofollow\">Yandex Cloud Object Storage<\/a> &#8212; \u044d\u0442\u043e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0435 \u0441 AWS S3 \u043e\u0431\u043b\u0430\u0447\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u0443\u0435\u043c \u0435\u0433\u043e \u0432 Spring Boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f SDK \u0410\u043c\u0430\u0437\u043e\u043d\u0430. <\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0431\u0430\u043a\u0435\u0442\u0430 \u0432 Yandex Cloud<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0437\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c s3<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0418 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043d\u0430\u0448\u0435 \u0432\u0435\u0434\u0435\u0440\u043a\u043e<\/p>\n<figure class=\"full-width\"><\/figure>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0431\u0430\u043a\u0435\u0442\u0443<\/h2>\n<p>\u041a\u0430\u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u0432 <a href=\"https:\/\/yandex.cloud\/en\/docs\/storage\/s3\/\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>, \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f s3 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u043d\u044b\u0439 \u0430\u043a\u043a\u0430\u0443\u043d\u0442.<br \/>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0430\u043f\u043a\u0443, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 Service accounts \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u0430\u043a\u043a\u0430\u0443\u043d\u0442:<\/p>\n<figure class=\"full-width\"><\/figure>\n<figure class=\"full-width\"><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0432 \u043d\u0430\u0448 \u0431\u0430\u043a\u0435\u0442 \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0443 security, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u043e\u043b\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0431\u0430\u043a\u0435\u0442\u0430<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041d\u0430\u0436\u0438\u043c\u0430\u0435\u043c \u043d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0443 Assign bindings \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u043e\u043b\u044c editor \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0430\u043a\u043a\u0430\u0443\u043d\u0442 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c static access key, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c key id \u0438 secret key<\/p>\n<figure class=\"full-width\"><\/figure>\n<h2>\u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 S3<\/h2>\n<p>Yandex Object Storage \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c \u0441 AWS S3, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u0438\u0445 sdk \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0445 \u044d\u0442\u0443 sdk.<\/p>\n<ol>\n<li>\n<p>com.amazonaws:aws-java-sdk:1.x.x &#8212; \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0439 sdk, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u044d\u0442\u043e\u043c \u0433\u043e\u0434\u0443<\/p>\n<\/li>\n<li>\n<p>software.amazon.awssdk:s3:2.x.x &#8212; \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 sdk<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/docs.awspring.io\/spring-cloud-aws\/docs\/3.2.0\/reference\/html\/index.html#spring-cloud-aws-s3\" rel=\"noopener noreferrer nofollow\">io.awspring.cloud:spring-cloud-aws-starter-s3<\/a> &#8212; \u043d\u0430\u0434\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043d\u0430\u0434 sdk, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u0430\u044f \u043a\u043e\u043c\u044c\u044e\u0438\u043d\u0438\u0442\u0438 \u0438 \u0443\u043b\u0443\u0447\u0448\u0430\u044e\u0449\u0430\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e \u0441\u043e \u0441\u043f\u0440\u0438\u043d\u0433\u043e\u043c<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/spring.io\/projects\/spring-cloud-stream\" rel=\"noopener noreferrer nofollow\">org.springframework.cloud.stream.app:aws-s3-app-starters-common<\/a> &#8212; \u0441\u0442\u0430\u0440\u0442\u0435\u0440 \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 spring cloud stream, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439\u0441\u044f \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 S3<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e sdk (\u0432\u0430\u0440\u0438\u0430\u043d\u0442 2), \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0439.<\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 spring boot \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"kotlin\">implementation(\"software.amazon.awssdk:aws-sdk-java:2.29.33\") implementation(\"software.amazon.awssdk:apache-client:2.29.33\")  implementation(\"org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0\") implementation(\"org.springframework.boot:spring-boot-starter-web\")<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 sdk \u043d\u0430\u043c \u0442\u0430\u043a \u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c  http-\u043a\u043b\u0438\u0435\u043d\u0442 \u0430\u043c\u0430\u0437\u043e\u043d\u0430. \u0422\u0430\u043a\u0436\u0435, \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 springdoc-openapi \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 Swagger UI, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c <code>s3Client<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 S3. \u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441\u0442\u043e\u0438\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0431\u0438\u043d\u043e\u043c \u0438 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432\u0441\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432 <code>application.properties<\/code>, \u043d\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044e \u0432\u0441\u0435 \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/photos\") @Tag(name = \"Photos\") @ApiResponses(@ApiResponse(responseCode = \"200\", useReturnTypeSchema = true)) public class PhotoController {      private static final String KEY_ID = \"YOUR_KEY_ID\";     private static final String SECRET_KEY = \"YOUR_SECRET_KEY\";     private static final String REGION = \"ru-central1\";     private static final String S3_ENDPOINT = \"https:\/\/storage.yandexcloud.net\";      private static final String BUCKET = \"spring-boot-s3-exmaple\";      private final S3Client s3Client;      public PhotoController() {         AwsCredentials credentials = AwsBasicCredentials.create(KEY_ID, SECRET_KEY);          s3Client = S3Client.builder()                 .httpClient(ApacheHttpClient.create())                 .region(Region.of(REGION))                 .endpointOverride(URI.create(S3_ENDPOINT))                 .credentialsProvider(StaticCredentialsProvider.create(credentials))                 .build();     } }<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439 \u0432 \u0431\u0430\u043a\u0435\u0442:<\/p>\n<pre><code class=\"java\">@PutMapping(consumes = MULTIPART_FORM_DATA_VALUE) public String uploadFile(@RequestParam MultipartFile photo) throws IOException {      String key = \"photos\/\" + photo.getOriginalFilename();     PutObjectRequest putObjectRequest = PutObjectRequest.builder()             .bucket(BUCKET)             .key(key)             .contentType(photo.getContentType())             .build();      s3Client.putObject(putObjectRequest, RequestBody.fromBytes(photo.getBytes()));      return key; }<\/code><\/pre>\n<p>\u0418 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u0445 \u0438\u0437 \u043d\u0435\u0433\u043e:<\/p>\n<pre><code class=\"java\">@GetMapping public ResponseEntity&lt;byte[]&gt; downloadFile(@RequestParam String key) throws IOException {      GetObjectRequest objectRequest = GetObjectRequest.builder()             .bucket(BUCKET)             .key(key)             .build();      var inputStream = s3Client.getObject(objectRequest);     byte[] data = inputStream.readAllBytes();      var headers = new HttpHeaders();     headers.add(HttpHeaders.CONTENT_DISPOSITION, \"inline\");     headers.add(HttpHeaders.CONTENT_TYPE, inputStream.response().contentType());      return ResponseEntity.ok()             .headers(headers)             .body(data); }<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e:<\/p>\n<pre><code class=\"java\">import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.AwsCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.http.apache.ApacheHttpClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.PutObjectRequest;  import java.io.IOException; import java.net.URI;  import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;  @RestController @RequestMapping(\"\/photos\") @Tag(name = \"Photos\") @ApiResponses(@ApiResponse(responseCode = \"200\", useReturnTypeSchema = true)) public class PhotoController {      private static final String KEY_ID = \"YOR_KEY_ID\";     private static final String SECRET_KEY = \"YOUR_SECRET_KEY\";     private static final String REGION = \"ru-central1\";     private static final String S3_ENDPOINT = \"https:\/\/storage.yandexcloud.net\";      private static final String BUCKET = \"spring-boot-s3-exmaple\";      private final S3Client s3Client;      public PhotoController() {         AwsCredentials credentials = AwsBasicCredentials.create(KEY_ID, SECRET_KEY);          s3Client = S3Client.builder()                 .httpClient(ApacheHttpClient.create())                 .region(Region.of(REGION))                 .endpointOverride(URI.create(S3_ENDPOINT))                 .credentialsProvider(StaticCredentialsProvider.create(credentials))                 .build();     }       @PutMapping(consumes = MULTIPART_FORM_DATA_VALUE)     public String uploadFile(@RequestParam MultipartFile photo) throws IOException {          String key = \"photos\/\" + photo.getOriginalFilename();         PutObjectRequest putObjectRequest = PutObjectRequest.builder()                 .bucket(BUCKET)                 .key(key)                 .contentType(photo.getContentType())                 .build();          s3Client.putObject(putObjectRequest, RequestBody.fromBytes(photo.getBytes()));          return key;     }      @GetMapping     public ResponseEntity&lt;byte[]&gt; downloadFile(@RequestParam String key) throws IOException {          GetObjectRequest objectRequest = GetObjectRequest.builder()                 .bucket(BUCKET)                 .key(key)                 .build();          var inputStream = s3Client.getObject(objectRequest);         byte[] data = inputStream.readAllBytes();          var headers = new HttpHeaders();         headers.add(HttpHeaders.CONTENT_DISPOSITION, \"inline\");         headers.add(HttpHeaders.CONTENT_TYPE, inputStream.response().contentType());          return ResponseEntity.ok()                 .headers(headers)                 .body(data);     } }<\/code><\/pre>\n<h2>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043d\u0430 <a href=\"http:\/\/localhost:8080\/swagger-ui\/index.html\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8080\/swagger-ui\/index.html<\/a> \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0444\u043e\u0442\u043e<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u043e\u043d\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u0432 \u0431\u0430\u043a\u0435\u0442\u0435. \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, S3 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u0443\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 <code>\/<\/code> \u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0430\u043f\u043a\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0448\u0435 \u0444\u043e\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u0432 \u043f\u0430\u043f\u043a\u0443 <code>photos<\/code><\/p>\n<figure class=\"full-width\"><\/figure>\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0444\u043e\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u043d\u0430\u0448 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442:<\/p>\n<figure class=\"full-width\"><\/figure>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e S3 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0440\u0435\u0434\u044b<\/h2>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u0430\u043c \u043d\u0443\u0436\u0435\u043d S3 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043f\u0440\u043e\u0434\u0435, \u0430 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0432\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u0414\u0432\u0430 \u0442\u0430\u043a\u0438\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 &#8212; MinIO \u0438 LocalStack. \u042f \u043d\u0435 \u0431\u0443\u0434\u0443 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u043d\u043e \u043a\u043e\u0440\u043e\u0442\u043a\u043e \u0441\u0440\u0430\u0432\u043d\u044e:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/docs.localstack.cloud\/user-guide\/aws\/s3\/\" rel=\"noopener noreferrer nofollow\">LocalStack<\/a> \u2014 \u044d\u0442\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u043c\u0443\u043b\u0438\u0440\u0443\u0435\u0442 \u043e\u0431\u043b\u0430\u0447\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b AWS. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e S3, \u0442\u043e LocalStack \u0432\u0430\u043c \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/min.io\/product\/overview\" rel=\"noopener noreferrer nofollow\">MinIO<\/a> \u2014 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0435 \u0441 API Amazon S3. \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\ud83d\udc68\u200d\ud83d\udcbb <a href=\"https:\/\/t.me\/life_of_junior_dev\" rel=\"noopener noreferrer nofollow\">\u0414\u0436\u0443\u043d\u0438\u043e\u0440<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/871280\/\"> https:\/\/habr.com\/ru\/articles\/871280\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-443996","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/443996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=443996"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/443996\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=443996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=443996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=443996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}