@@ -108,14 +108,12 @@ public async Task<bool> DownloadObjectFromBucketAsync(
108108 string objectName ,
109109 string filePath )
110110 {
111- // Create a GetObject request
112111 var request = new GetObjectRequest
113112 {
114113 BucketName = bucketName ,
115114 Key = objectName ,
116115 } ;
117116
118- // Issue request and remember to dispose of the response
119117 using GetObjectResponse response = await _amazonS3 . GetObjectAsync ( request ) ;
120118
121119 try
@@ -177,11 +175,10 @@ public async Task<bool> CopyObjectInBucketAsync(
177175 /// <summary>
178176 /// Shows how to list the objects in an Amazon S3 bucket.
179177 /// </summary>
180- /// <param name="bucketName">The name of the bucket for which to list
178+ /// <param name="bucketName">The name of the bucket for which to list.
181179 /// the contents.</param>
182- /// <returns>A boolean value indicating the success or failure of the
183- /// copy operation.</returns>
184- public async Task < bool > ListBucketContentsAsync ( string bucketName )
180+ /// <returns>The collection of objects.</returns>
181+ public async Task < List < S3Object > > ListBucketContentsAsync ( string bucketName )
185182 {
186183 try
187184 {
@@ -199,23 +196,27 @@ public async Task<bool> ListBucketContentsAsync(string bucketName)
199196 {
200197 BucketName = bucketName ,
201198 } ) ;
202-
199+ var s3Objects = new List < S3Object > ( ) ;
203200 await foreach ( var response in listObjectsV2Paginator . Responses )
204201 {
205- Console . WriteLine ( $ "HttpStatusCode: { response . HttpStatusCode } ") ;
206- Console . WriteLine ( $ "Number of Keys: { response . KeyCount } ") ;
207- foreach ( var entry in response . S3Objects )
202+ if ( response . S3Objects != null )
208203 {
209- Console . WriteLine ( $ "Key = { entry . Key } Size = { entry . Size } " ) ;
204+ s3Objects . AddRange ( response . S3Objects ) ;
210205 }
211206 }
212207
213- return true ;
208+ Console . WriteLine ( $ "Number of Objects: { s3Objects . Count } ") ;
209+ foreach ( var entry in s3Objects )
210+ {
211+ Console . WriteLine ( $ "Key = { entry . Key } Size = { entry . Size } ") ;
212+ }
213+
214+ return s3Objects ;
214215 }
215216 catch ( AmazonS3Exception ex )
216217 {
217218 Console . WriteLine ( $ "Error encountered on server. Message:'{ ex . Message } ' getting list of objects.") ;
218- return false ;
219+ return null ;
219220 }
220221 }
221222
@@ -236,16 +237,15 @@ public async Task<bool> DeleteBucketContentsAsync(string bucketName)
236237 try
237238 {
238239 // Delete all objects in the bucket.
239- var deleteList = await _amazonS3 . ListObjectsV2Async ( new ListObjectsV2Request ( )
240+ var deleteList = await ListBucketContentsAsync ( bucketName ) ;
241+ if ( deleteList != null && deleteList . Any ( ) )
240242 {
241- BucketName = bucketName ,
242- } ) ;
243- await _amazonS3 . DeleteObjectsAsync ( new DeleteObjectsRequest ( )
244- {
245- BucketName = bucketName ,
246- Objects = deleteList . S3Objects
247- . Select ( o => new KeyVersion { Key = o . Key } ) . ToList ( ) ,
248- } ) ;
243+ await _amazonS3 . DeleteObjectsAsync ( new DeleteObjectsRequest ( )
244+ {
245+ BucketName = bucketName ,
246+ Objects = deleteList . Select ( o => new KeyVersion { Key = o . Key } ) . ToList ( ) ,
247+ } ) ;
248+ }
249249
250250 return true ;
251251 }
0 commit comments