Reputation: 1533
I am just stuck with aws s3 on my .net core mvc application. I just simply need to input bucket name of s3 then return all of directory name list in this bucket but this simple task i didn't found anywhere on internet. I already tried few solution provided by AWS forum but problem is this absolutely not works at all. Bellow i have provided my controller code also forum link. Actually the issue they told is Amazon.S3.IO
and S3DirectoryInfo
namespace was removed from .net core so i am failed to follow them as they advised there. Any one can fix my code bellow which will give a list of bucket directory in .net core application?
I am using two nuget package-
AWSSDK.Core
and AWSSDK.S3
Forum Link - Amazon.S3.IO not supported in .Net Core anymore?
Controller:
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
public IActionResult Media()
{
string bucketName = "domain33.com";
AmazonS3Client s3Client = new AmazonS3Client("Access_Key_ID", "Secret_Access_Key", RegionEndpoint.USEast1);
var getResponse = s3Client.ListBucketsAsync(new GetObjectRequest
{
BucketName = bucketName
});
var x = getResponse;
return View();
}
Upvotes: 2
Views: 10676
Reputation: 131
You could try using the ListObjectsV2Async
method on IAmazonS3
to retrieve a list of all of the existing objects in the bucket based on the AWS's example. Their code is below in case the link dies:
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
namespace Amazon.DocSamples.S3
{
class ListObjectsTest
{
private const string bucketName = "*** bucket name ***";
// Specify your bucket region (an example region is shown).
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
private static IAmazonS3 client;
public static void Main()
{
client = new AmazonS3Client(bucketRegion);
ListingObjectsAsync().Wait();
}
static async Task ListingObjectsAsync()
{
try
{
ListObjectsV2Request request = new ListObjectsV2Request
{
BucketName = bucketName,
MaxKeys = 10
};
ListObjectsV2Response response;
do
{
response = await client.ListObjectsV2Async(request);
// Process the response.
foreach (S3Object entry in response.S3Objects)
{
Console.WriteLine("key = {0} size = {1}",
entry.Key, entry.Size);
}
Console.WriteLine("Next Continuation Token: {0}", response.NextContinuationToken);
request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString());
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
Console.ReadKey();
}
}
}
}
Based on that sample, you could do further processing or add the keys to a list of strings for subsequent processing, instead of just writing it the console as their example code does. For instance, you could add each key to a list, and then process that list to calculate the distinct "directories".
Upvotes: 2