8
8
import java .io .ByteArrayOutputStream ;
9
9
import java .io .IOException ;
10
10
import java .io .InputStream ;
11
+ import java .util .HashMap ;
12
+ import java .util .Map ;
11
13
import java .util .regex .Matcher ;
12
14
import java .util .regex .Pattern ;
13
15
14
16
import javax .imageio .ImageIO ;
15
17
16
- import com .amazonaws .AmazonServiceException ;
18
+ import software .amazon .awssdk .awscore .exception .AwsServiceException ;
19
+ import software .amazon .awssdk .core .sync .RequestBody ;
20
+ import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
21
+ import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
22
+ import software .amazon .awssdk .services .s3 .model .S3Object ;
23
+ import software .amazon .awssdk .services .s3 .S3Client ;
24
+
17
25
import com .amazonaws .services .lambda .runtime .Context ;
18
26
import com .amazonaws .services .lambda .runtime .RequestHandler ;
19
27
import com .amazonaws .services .lambda .runtime .events .S3Event ;
20
- import com .amazonaws .services .s3 .AmazonS3 ;
21
- import com .amazonaws .services .s3 .event .S3EventNotification .S3EventNotificationRecord ;
22
- import com .amazonaws .services .s3 .model .GetObjectRequest ;
23
- import com .amazonaws .services .s3 .model .ObjectMetadata ;
24
- import com .amazonaws .services .s3 .model .S3Object ;
25
- import com .amazonaws .services .s3 .AmazonS3ClientBuilder ;
28
+ import com .amazonaws .services .lambda .runtime .events .models .s3 .S3EventNotification .S3EventNotificationRecord ;
26
29
27
30
import com .google .gson .Gson ;
28
31
import com .google .gson .GsonBuilder ;
@@ -67,19 +70,20 @@ public String handleRequest(S3Event s3event, Context context) {
67
70
}
68
71
69
72
// Download the image from S3 into a stream
70
- AmazonS3 s3Client = AmazonS3ClientBuilder .defaultClient ();
71
- S3Object s3Object = s3Client .getObject (new GetObjectRequest (
72
- srcBucket , srcKey ));
73
- InputStream objectData = s3Object .getObjectContent ();
73
+ S3Client s3Client = S3Client .builder ().build ();
74
+ GetObjectRequest getObjectRequest = GetObjectRequest .builder ()
75
+ .bucket (srcBucket )
76
+ .key (srcKey )
77
+ .build ();
78
+ InputStream s3Object = s3Client .getObject (getObjectRequest );
74
79
75
80
// Read the source image
76
- BufferedImage srcImage = ImageIO .read (objectData );
81
+ BufferedImage srcImage = ImageIO .read (s3Object );
77
82
int srcHeight = srcImage .getHeight ();
78
83
int srcWidth = srcImage .getWidth ();
79
- // Infer the scaling factor to avoid stretching the image
80
- // unnaturally
81
- float scalingFactor = Math .min (MAX_WIDTH / srcWidth , MAX_HEIGHT
82
- / srcHeight );
84
+ // Infer scaling factor to avoid stretching image unnaturally
85
+ float scalingFactor = Math .min (
86
+ MAX_WIDTH / srcWidth , MAX_HEIGHT / srcHeight );
83
87
int width = (int ) (scalingFactor * srcWidth );
84
88
int height = (int ) (scalingFactor * srcHeight );
85
89
@@ -98,25 +102,29 @@ public String handleRequest(S3Event s3event, Context context) {
98
102
// Re-encode image to target format
99
103
ByteArrayOutputStream os = new ByteArrayOutputStream ();
100
104
ImageIO .write (resizedImage , imageType , os );
101
- InputStream is = new ByteArrayInputStream (os .toByteArray ());
102
- // Set Content-Length and Content-Type
103
- ObjectMetadata meta = new ObjectMetadata ();
104
- meta . setContentLength ( os .size ());
105
+ // InputStream is = new ByteArrayInputStream(os.toByteArray());
106
+
107
+ Map < String , String > metadata = new HashMap <> ();
108
+ metadata . put ( "Content-Length" , Integer . toString ( os .size () ));
105
109
if (JPG_TYPE .equals (imageType )) {
106
- meta .setContentType (JPG_MIME );
107
- }
108
- if (PNG_TYPE .equals (imageType )) {
109
- meta .setContentType (PNG_MIME );
110
+ metadata .put ("Content-Type" , JPG_MIME );
111
+ } else if (PNG_TYPE .equals (imageType )) {
112
+ metadata .put ("Content-Type" , PNG_MIME );
110
113
}
114
+ PutObjectRequest putObjectRequest = PutObjectRequest .builder ()
115
+ .bucket (dstBucket )
116
+ .key (dstKey )
117
+ .metadata (metadata )
118
+ .build ();
111
119
112
120
// Uploading to S3 destination bucket
113
121
logger .info ("Writing to: " + dstBucket + "/" + dstKey );
114
122
try {
115
- s3Client .putObject (dstBucket , dstKey , is , meta );
123
+ s3Client .putObject (putObjectRequest , RequestBody . fromBytes ( os . toByteArray ()) );
116
124
}
117
- catch (AmazonServiceException e )
125
+ catch (AwsServiceException e )
118
126
{
119
- logger .error (e .getErrorMessage ());
127
+ logger .error (e .awsErrorDetails (). errorMessage ());
120
128
System .exit (1 );
121
129
}
122
130
logger .info ("Successfully resized " + srcBucket + "/"
0 commit comments