55package com .jcabi .s3 ;
66
77import com .jcabi .aspects .Loggable ;
8- import com .jcabi .log .Logger ;
98import java .io .IOException ;
109import java .io .InputStream ;
1110import java .io .OutputStream ;
1211import lombok .EqualsAndHashCode ;
1312import org .apache .commons .io .IOUtils ;
1413import org .apache .commons .io .input .BoundedInputStream ;
15- import software .amazon .awssdk .core .ResponseInputStream ;
1614import software .amazon .awssdk .core .sync .RequestBody ;
17- import software .amazon .awssdk .services .s3 .S3Client ;
1815import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
19- import software .amazon .awssdk .services .s3 .model .GetObjectResponse ;
2016import software .amazon .awssdk .services .s3 .model .HeadObjectRequest ;
2117import software .amazon .awssdk .services .s3 .model .HeadObjectResponse ;
2218import software .amazon .awssdk .services .s3 .model .ListObjectsV2Request ;
23- import software .amazon .awssdk .services .s3 .model .ListObjectsV2Response ;
2419import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
25- import software .amazon .awssdk .services .s3 .model .PutObjectResponse ;
2620import software .amazon .awssdk .services .s3 .model .S3Exception ;
2721
2822/**
3226 */
3327@ EqualsAndHashCode (of = { "bkt" , "name" })
3428@ Loggable (Loggable .DEBUG )
35- @ SuppressWarnings ("PMD.GuardLogStatement" )
3629final class AwsOcket implements Ocket {
3730
3831 /**
@@ -73,23 +66,12 @@ public String key() {
7366 @ Override
7467 public HeadObjectResponse meta () throws IOException {
7568 try {
76- final S3Client aws = this .bkt .region ().aws ();
77- final long start = System .currentTimeMillis ();
78- final HeadObjectResponse meta = aws .headObject (
69+ return this .bkt .region ().aws ().headObject (
7970 HeadObjectRequest .builder ()
8071 .bucket (this .bkt .name ())
8172 .key (this .name )
8273 .build ()
8374 );
84- Logger .info (
85- this ,
86- // @checkstyle LineLength (1 line)
87- "metadata loaded for ocket '%s' in bucket '%s' in %[ms]s (etag=%s)" ,
88- this .name , this .bkt .name (),
89- System .currentTimeMillis () - start ,
90- meta .eTag ()
91- );
92- return meta ;
9375 } catch (final S3Exception ex ) {
9476 throw new OcketNotFoundException (
9577 String .format (
@@ -102,27 +84,15 @@ public HeadObjectResponse meta() throws IOException {
10284 }
10385
10486 @ Override
105- @ SuppressWarnings ("PMD.GuardLogStatement" )
10687 public boolean exists () throws IOException {
10788 try {
108- final S3Client aws = this .bkt .region ().aws ();
109- final long start = System .currentTimeMillis ();
110- final ListObjectsV2Response listing = aws .listObjectsV2 (
89+ return !this .bkt .region ().aws ().listObjectsV2 (
11190 ListObjectsV2Request .builder ()
11291 .bucket (this .bkt .name ())
11392 .prefix (this .name )
11493 .maxKeys (1 )
11594 .build ()
116- );
117- final boolean exists = !listing .contents ().isEmpty ();
118- Logger .info (
119- this ,
120- "ocket '%s' existence checked in bucket '%s' in %[ms]s (%b)" ,
121- this .name , this .bkt .name (),
122- System .currentTimeMillis () - start ,
123- exists
124- );
125- return exists ;
95+ ).contents ().isEmpty ();
12696 } catch (final S3Exception ex ) {
12797 throw new IOException (
12898 String .format (
@@ -135,28 +105,17 @@ public boolean exists() throws IOException {
135105 }
136106
137107 @ Override
138- @ SuppressWarnings ("PMD.GuardLogStatement" )
139108 public void read (final OutputStream output ) throws IOException {
140109 try {
141- final S3Client aws = this .bkt .region ().aws ();
142- final ResponseInputStream <GetObjectResponse > obj = aws .getObject (
143- GetObjectRequest .builder ()
144- .bucket (this .bkt .name ())
145- .key (this .name )
146- .build ()
110+ IOUtils .copy (
111+ this .bkt .region ().aws ().getObject (
112+ GetObjectRequest .builder ()
113+ .bucket (this .bkt .name ())
114+ .key (this .name )
115+ .build ()
116+ ),
117+ output
147118 );
148- try (InputStream input = obj ) {
149- final long start = System .currentTimeMillis ();
150- final int bytes = IOUtils .copy (input , output );
151- Logger .info (
152- this ,
153- // @checkstyle LineLength (1 line)
154- "loaded %d byte(s) from ocket '%s' in bucket '%s' in %[ms]s (etag=%s)" ,
155- bytes , this .name , this .bkt .name (),
156- System .currentTimeMillis () - start ,
157- obj .response ().eTag ()
158- );
159- }
160119 } catch (final S3Exception ex ) {
161120 throw new OcketNotFoundException (
162121 String .format (
@@ -169,13 +128,10 @@ public void read(final OutputStream output) throws IOException {
169128 }
170129
171130 @ Override
172- @ SuppressWarnings ("PMD.GuardLogStatement" )
173131 public void write (final InputStream input , final HeadObjectResponse meta )
174132 throws IOException {
175133 try (BoundedInputStream cnt = BoundedInputStream .builder ()
176134 .setInputStream (input ).get ()) {
177- final S3Client aws = this .bkt .region ().aws ();
178- final long start = System .currentTimeMillis ();
179135 final PutObjectRequest .Builder req = PutObjectRequest .builder ()
180136 .bucket (this .bkt .name ())
181137 .key (this .name );
@@ -185,27 +141,17 @@ public void write(final InputStream input, final HeadObjectResponse meta)
185141 if (meta .contentEncoding () != null ) {
186142 req .contentEncoding (meta .contentEncoding ());
187143 }
188- final PutObjectResponse result ;
189144 if (meta .contentLength () != null && meta .contentLength () > 0L ) {
190- result = aws .putObject (
145+ this . bkt . region (). aws () .putObject (
191146 req .contentLength (meta .contentLength ()).build (),
192147 RequestBody .fromInputStream (cnt , meta .contentLength ())
193148 );
194149 } else {
195- final byte [] bytes = IOUtils .toByteArray (cnt );
196- result = aws .putObject (
197- req .contentLength ((long ) bytes .length ).build (),
198- RequestBody .fromBytes (bytes )
150+ this .bkt .region ().aws ().putObject (
151+ req .build (),
152+ RequestBody .fromBytes (IOUtils .toByteArray (cnt ))
199153 );
200154 }
201- Logger .info (
202- this ,
203- // @checkstyle LineLength (1 line)
204- "saved %d byte(s) to ocket '%s' in bucket '%s' in %[ms]s (etag=%s)" ,
205- cnt .getCount (), this .name , this .bkt .name (),
206- System .currentTimeMillis () - start ,
207- result .eTag ()
208- );
209155 } catch (final S3Exception ex ) {
210156 throw new IOException (
211157 String .format (
0 commit comments