@@ -6,9 +6,10 @@ const config = require('../config')
66const { getImageMimeType } = require ( '../utils' )
77const logger = require ( '../logger' )
88
9- const AWS = require ( 'aws-sdk' )
10- const awsConfig = new AWS . Config ( config . s3 )
11- const s3 = new AWS . S3 ( awsConfig )
9+ const { S3Client } = require ( '@aws-sdk/client-s3-node/S3Client' )
10+ const { PutObjectCommand } = require ( '@aws-sdk/client-s3-node/commands/PutObjectCommand' )
11+
12+ const s3 = new S3Client ( config . s3 )
1213
1314exports . uploadImage = function ( imagePath , callback ) {
1415 if ( ! imagePath || typeof imagePath !== 'string' ) {
@@ -32,23 +33,23 @@ exports.uploadImage = function (imagePath, callback) {
3233 Body : buffer ,
3334 ACL : 'public-read'
3435 }
35-
3636 const mimeType = getImageMimeType ( imagePath )
3737 if ( mimeType ) { params . ContentType = mimeType }
3838
39- s3 . putObject ( params , function ( err , data ) {
40- if ( err ) {
41- callback ( new Error ( err ) , null )
42- return
43- }
39+ const command = new PutObjectCommand ( params )
4440
41+ s3 . send ( command ) . then ( data => {
4542 let s3Endpoint = 's3.amazonaws.com'
4643 if ( config . s3 . endpoint ) {
4744 s3Endpoint = config . s3 . endpoint
4845 } else if ( config . s3 . region && config . s3 . region !== 'us-east-1' ) {
4946 s3Endpoint = `s3-${ config . s3 . region } .amazonaws.com`
5047 }
5148 callback ( null , `https://${ s3Endpoint } /${ config . s3bucket } /${ params . Key } ` )
49+ } ) . catch ( err => {
50+ if ( err ) {
51+ callback ( new Error ( err ) , null )
52+ }
5253 } )
5354 } )
5455}
0 commit comments