List AWS S3 Buckets using C-Sharp on Visual Studio
In this tutorial, AWS developers can find C-Sharp codes to list all buckets created with Amazon S3 Simple Storage Service.
Using Visual Studio 2017 I created sample Microsoft .NET Windows Forms application with C#
I want to share step by step how to create a Visual Studio project to read AWS S3 buckets list.
Launch Visual Studio 2017.
Then start creating a new VS project using menu File > New > Project
I selected Windows Forms App (.NET Framework) Visual C# project template under installed Visual C# Windows Desktop templates.
I named my project ListS3BucketsKodyazAWS
First of all on Solution Explorer window right click on the C# solution and from context menu choose Manage NuGet packages option.
Add the AWSSDK.S3 NuGet package into the solution.
AWSSDK.S3 package is developed by Amazon Web Services.
AWSSDK.S3 package enables programmers to manage Amazon Simple Storage Service (Amazon S3) programmatically which is used as a secure, durable and highly-scalable object storage.
Click Install to get this package and accept if you are requested to give approval for changes being made by Visual Studio.
Following step will be modifying the project's App.config file and including below mentioned XML nodes in application configuration file.
My Windows Forms project app.config file is created with below initial content
I changed the App.config by adding two modifications to prevent possible problems that might occur during coding.
C-Sharp programmers, we are ready to start coding.
First of all, import following namespaces
Additionally, I define following form variables that will enable authentication to the AWS services
For form design, I added a Button control and a ComboBox onto the layout of the Form1 object as in below screenshot. This will be the simple design I preferred for this AWS tutorial for C# programmers working on S3 Simple Storage Service.
Double click on the button to start coding from button's click event.
If you look at construction line for AmazonS3Client object, you will see I used an arbitrary RegionEndpoint.
We need a region to connect to our AWS services.
On the otherhand, since S3 service is not region based, developers can use any region as RegionEndpoint.
But for querying contents of an AWS S3 Bucket, the developers have to use the corresponding endpoint in which Region the Bucket is created.
For AWS Regions and Region Codes for programmatic access please refer to referenced document.
Is possible to overcome of the use of a default RegionEndpoint, application programmers can add following configuration in App.config file.
If the required configurations are missing, either in C# code or in App.config file, following exception could occur:
Amazon.Runtime.AmazonClientException: 'No RegionEndpoint or ServiceURL configured'
Amazon.Runtime.AmazonClientException
HResult=0x80131500
Message=No RegionEndpoint or ServiceURL configured
Source=AWSSDK.Core
StackTrace:
at Amazon.Runtime.ClientConfig.Validate()
at Amazon.S3.AmazonS3Config.Validate()
at Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config)
at Amazon.S3.AmazonS3Client..ctor()
If you are plan to run your AWS application behind a firewall, you might require authorization for your HTTP requests.
For this proxy authentication, the App.config configuration for defaultProxy can be used.
If you don't include this configuration in the App.config file, developers can experience following error:
Amazon.S3.AmazonS3Exception: 'Error making request with Error Code ProxyAuthenticationRequired and Http Status Code ProxyAuthenticationRequired. No further error information was returned by the service.'
If the .NET programmer runs the final Visual Studio C# project, the AWS S3 buckets list will be displayed as items of the combobox control as seen in below screenshot.