11const assert = require ( 'assert' ) ;
2- const AWS = require ( 'aws-sdk' ) ;
2+ const {
3+ PutBucketPolicyCommand,
4+ ListObjectsCommand,
5+ GetObjectCommand,
6+ PutObjectCommand } = require ( '@aws-sdk/client-s3' ) ;
37const { errorInstances } = require ( 'arsenal' ) ;
48
59const withV4 = require ( '../support/withV4' ) ;
610const BucketUtility = require ( '../../lib/utility/bucket-util' ) ;
7- const { VALIDATE_CREDENTIALS , SIGN } = AWS . EventListeners . Core ;
811
912withV4 ( sigCfg => {
1013 const ownerAccountBucketUtil = new BucketUtility ( 'default' , sigCfg ) ;
@@ -13,13 +16,33 @@ withV4(sigCfg => {
1316
1417 function awsRequest ( auth , operation , params , callback ) {
1518 if ( auth ) {
16- ownerAccountBucketUtil . s3 [ operation ] ( params , callback ) ;
19+ // Use authenticated client
20+ const commandMap = {
21+ 'listObjects' : ListObjectsCommand ,
22+ 'getObject' : GetObjectCommand ,
23+ 'putObject' : PutObjectCommand ,
24+ } ;
25+ const CommandCtor = commandMap [ operation ] ;
26+ ownerAccountBucketUtil . s3 . send ( new CommandCtor ( params ) )
27+ . then ( data => callback ( null , data ) )
28+ . catch ( err => callback ( err ) ) ;
1729 } else {
18- const bucketUtil = new BucketUtility ( 'default' , sigCfg ) ;
19- const request = bucketUtil . s3 [ operation ] ( params ) ;
20- request . removeListener ( 'validate' , VALIDATE_CREDENTIALS ) ;
21- request . removeListener ( 'sign' , SIGN ) ;
22- request . send ( callback ) ;
30+ // Create unauthenticated client
31+ const unauthClient = new BucketUtility ( 'default' , {
32+ ...sigCfg ,
33+ credentials : { accessKeyId : '' , secretAccessKey : '' } ,
34+ forcePathStyle : true ,
35+ signer : { sign : async request => request } ,
36+ } ) ;
37+ const commandMap = {
38+ 'listObjects' : ListObjectsCommand ,
39+ 'getObject' : GetObjectCommand ,
40+ 'putObject' : PutObjectCommand ,
41+ } ;
42+ const CommandCtor = commandMap [ operation ] ;
43+ unauthClient . s3 . send ( new CommandCtor ( params ) )
44+ . then ( data => callback ( null , data ) )
45+ . catch ( err => callback ( err ) ) ;
2346 }
2447 }
2548
@@ -32,7 +55,7 @@ withV4(sigCfg => {
3255
3356 function cbWithError ( done ) {
3457 return err => {
35- assert . strictEqual ( err . statusCode , errorInstances . AccessDenied . code ) ;
58+ assert . strictEqual ( err . $metadata . httpStatusCode , errorInstances . AccessDenied . code ) ;
3659 done ( ) ;
3760 } ;
3861 }
@@ -54,11 +77,11 @@ withV4(sigCfg => {
5477 Version : '2012-10-17' ,
5578 Statement : [ statement ] ,
5679 } ;
57- s3 . putBucketPolicy ( {
80+ s3 . send ( new PutBucketPolicyCommand ( {
5881 Bucket : testBuckets [ 0 ] ,
5982 Policy : JSON . stringify ( bucketPolicy ) ,
60- } , err => {
61- assert . ifError ( err ) ;
83+ } ) )
84+ . then ( ( ) => {
6285 const param = { Bucket : testBuckets [ 0 ] } ;
6386 awsRequest ( true , 'listObjects' , param , cbNoError ( done ) ) ;
6487 } ) ;
@@ -76,11 +99,11 @@ withV4(sigCfg => {
7699 Version : '2012-10-17' ,
77100 Statement : [ statement ] ,
78101 } ;
79- s3 . putBucketPolicy ( {
102+ s3 . send ( new PutBucketPolicyCommand ( {
80103 Bucket : testBuckets [ 0 ] ,
81104 Policy : JSON . stringify ( bucketPolicy ) ,
82- } , err => {
83- assert . ifError ( err ) ;
105+ } ) )
106+ . then ( ( ) => {
84107 const param = { Bucket : testBuckets [ 1 ] } ;
85108 awsRequest ( false , 'listObjects' , param , cbWithError ( done ) ) ;
86109 } ) ;
@@ -98,11 +121,11 @@ withV4(sigCfg => {
98121 Version : '2012-10-17' ,
99122 Statement : [ statement ] ,
100123 } ;
101- s3 . putBucketPolicy ( {
124+ s3 . send ( new PutBucketPolicyCommand ( {
102125 Bucket : testBuckets [ 0 ] ,
103126 Policy : JSON . stringify ( bucketPolicy ) ,
104- } , err => {
105- assert . ifError ( err ) ;
127+ } ) )
128+ . then ( ( ) => {
106129 const param = { Bucket : testBuckets [ 0 ] } ;
107130 awsRequest ( false , 'listObjects' , param , cbWithError ( done ) ) ;
108131 } ) ;
@@ -122,23 +145,21 @@ withV4(sigCfg => {
122145 Version : '2012-10-17' ,
123146 Statement : [ statement ] ,
124147 } ;
125- s3 . putBucketPolicy ( {
148+ s3 . send ( new PutBucketPolicyCommand ( {
126149 Bucket : testBuckets [ 0 ] ,
127150 Policy : JSON . stringify ( bucketPolicy ) ,
128- } , err => {
129- assert . ifError ( err ) ;
130- s3 . putObject ( {
151+ } ) )
152+ . then ( ( ) => s3 . send ( new PutObjectCommand ( {
131153 Bucket : testBuckets [ 0 ] ,
132154 Body : testBody ,
133155 Key : testKey ,
134- } , er => {
135- assert . ifError ( er ) ;
136- const param = {
137- Bucket : testBuckets [ 0 ] ,
138- Key : testKey ,
139- } ;
140- awsRequest ( false , 'getObject' , param , cbNoError ( done ) ) ;
141- } ) ;
156+ } ) ) )
157+ . then ( ( ) => {
158+ const param = {
159+ Bucket : testBuckets [ 0 ] ,
160+ Key : testKey ,
161+ } ;
162+ awsRequest ( false , 'getObject' , param , cbNoError ( done ) ) ;
142163 } ) ;
143164 } ) ;
144165
@@ -156,23 +177,21 @@ withV4(sigCfg => {
156177 Version : '2012-10-17' ,
157178 Statement : [ statement ] ,
158179 } ;
159- s3 . putBucketPolicy ( {
180+ s3 . send ( new PutBucketPolicyCommand ( {
160181 Bucket : testBuckets [ 0 ] ,
161182 Policy : JSON . stringify ( bucketPolicy ) ,
162- } , err => {
163- assert . ifError ( err ) ;
164- s3 . putObject ( {
183+ } ) )
184+ . then ( ( ) => s3 . send ( new PutObjectCommand ( {
165185 Bucket : testBuckets [ 0 ] ,
166186 Body : testBody ,
167187 Key : testKey ,
168- } , er => {
169- assert . ifError ( er ) ;
170- const param = {
171- Bucket : testBuckets [ 0 ] ,
172- Key : testKey ,
173- } ;
174- awsRequest ( false , 'getObject' , param , cbNoError ( done ) ) ;
175- } ) ;
188+ } ) ) )
189+ . then ( ( ) => {
190+ const param = {
191+ Bucket : testBuckets [ 0 ] ,
192+ Key : testKey ,
193+ } ;
194+ awsRequest ( false , 'getObject' , param , cbNoError ( done ) ) ;
176195 } ) ;
177196 } ) ;
178197
@@ -190,23 +209,21 @@ withV4(sigCfg => {
190209 Version : '2012-10-17' ,
191210 Statement : [ statement ] ,
192211 } ;
193- s3 . putBucketPolicy ( {
212+ s3 . send ( new PutBucketPolicyCommand ( {
194213 Bucket : testBuckets [ 0 ] ,
195214 Policy : JSON . stringify ( bucketPolicy ) ,
196- } , err => {
197- assert . ifError ( err ) ;
198- s3 . putObject ( {
215+ } ) )
216+ . then ( ( ) => s3 . send ( new PutObjectCommand ( {
199217 Bucket : testBuckets [ 0 ] ,
200218 Body : testBody ,
201219 Key : testKey ,
202- } , er => {
203- assert . ifError ( er ) ;
204- const param = {
205- Bucket : testBuckets [ 0 ] ,
206- Key : testKey ,
207- } ;
208- awsRequest ( false , 'getObject' , param , cbWithError ( done ) ) ;
209- } ) ;
220+ } ) ) )
221+ . then ( ( ) => {
222+ const param = {
223+ Bucket : testBuckets [ 0 ] ,
224+ Key : testKey ,
225+ } ;
226+ awsRequest ( false , 'getObject' , param , cbWithError ( done ) ) ;
210227 } ) ;
211228 } ) ;
212229
@@ -223,11 +240,11 @@ withV4(sigCfg => {
223240 Version : '2012-10-17' ,
224241 Statement : [ statement ] ,
225242 } ;
226- s3 . putBucketPolicy ( {
243+ s3 . send ( new PutBucketPolicyCommand ( {
227244 Bucket : testBuckets [ 0 ] ,
228245 Policy : JSON . stringify ( bucketPolicy ) ,
229- } , err => {
230- assert . ifError ( err ) ;
246+ } ) )
247+ . then ( ( ) => {
231248 const param = {
232249 Bucket : testBuckets [ 0 ] ,
233250 Key : 'invalidkey' ,
@@ -249,11 +266,11 @@ withV4(sigCfg => {
249266 Version : '2012-10-17' ,
250267 Statement : [ statement ] ,
251268 } ;
252- s3 . putBucketPolicy ( {
269+ s3 . send ( new PutBucketPolicyCommand ( {
253270 Bucket : testBuckets [ 0 ] ,
254271 Policy : JSON . stringify ( bucketPolicy ) ,
255- } , err => {
256- assert . ifError ( err ) ;
272+ } ) )
273+ . then ( ( ) => {
257274 const param = {
258275 Bucket : testBuckets [ 1 ] ,
259276 Key : 'invalidkey' ,
0 commit comments