None
None

Reputation: 5670

Get files from amazon s3 buckets sub folder .Net

I am downloading some files from amazon s3 like this

 private static readonly IAmazonS3 _amazonS3Client = AWSClientFactory.CreateAmazonS3Client(
        AccessKey,
        SecretKey,
        Config
        );
  var request = new ListObjectsRequest {BucketName = BucketName};

        ListObjectsResponse response = _amazonS3Client.ListObjects(request);
        foreach (S3Object o in response.S3Objects)
        {
            var objRequest = new GetObjectRequest
            {
                BucketName = BucketName,
                Key = o.Key

            };
            GetObjectResponse objResponse = _amazonS3Client.GetObject(objRequest);
            objResponse.WriteResponseStreamToFile(ConfigurationManager.AppSettings["TargetLocation"] + o.Key);
        }

This works well, Now I have created a folder inside Bucket, and I want to download only the files inside that bucket. Can any one point out the correct approach to achieve this?

Upvotes: 2

Views: 5657

Answers (1)

E.J. Brennan
E.J. Brennan

Reputation: 46879

Just set your bucketname as so: bucketname/foldername.

So If you have a bucket called 'MyBucket' and a folder within that bucket called MyFolder, than you would do:

var objRequest = new GetObjectRequest
            {
                BucketName = "MyBucket/MyFolder",
                Key = o.Key

            };

Upvotes: 5

Related Questions