SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Amazon Web Services AWS Tutorials and Guides
Development resources, articles, tutorials, code samples and tools and downloads for ASP.Net, SQL Server, R Script, Windows, Windows Phone, AWS, SAP HANA and ABAP, like SAP UI5, Screen Personas, etc.




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.

manage NuGet packges for Amazon AWS S3 service

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.

install AWSSDK.S3 NuGet package in Visual Studio AWS project

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

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
 </startup>
</configuration>

I changed the App.config by adding two modifications to prevent possible problems that might occur during coding.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
 </startup>
 <appSettings>
  <add key="AWSRegion" value="eu-central-1" />
 </appSettings>
 <system.net>
  <defaultProxy useDefaultCredentials="true"></defaultProxy>
 </system.net>
</configuration>

C-Sharp programmers, we are ready to start coding.

First of all, import following namespaces

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

Additionally, I define following form variables that will enable authentication to the AWS services

private string profileName = "default";
private string accessKeyId = "SOMEACCESSKEYID";
private string secretKey = "YOURSECRETKEYHERE";

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.

Visual Studio Windows Forms application for AWS S3 service

Double click on the button to start coding from button's click event.

comboBox1.Items.Clear();

Amazon.Util.ProfileManager.RegisterProfile(profileName, accessKeyId, secretKey);

// Create an S3 object to query AWS Simple Storage Service
IAmazonS3 objS3Client = new AmazonS3Client(RegionEndpoint.EUCentral1);

// Get the list of AWS S3 buckets
ListBucketsResponse objListBucketsResponse = objS3Client.ListBuckets();

int bucketsCount = objListBucketsResponse.Buckets.Count;
label2.Text = bucketsCount.ToString();

if (bucketsCount > 0)
{
 foreach (S3Bucket bucket in objListBucketsResponse.Buckets)
 {
 comboBox1.Items.Add(bucket.BucketName);
 }
}

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.

<appSettings>
 <add key="AWSRegion" value="eu-central-1" />
</appSettings>

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()

No RegionEndpoint or ServiceURL configured

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.

<system.net>
 <defaultProxy useDefaultCredentials="true"></defaultProxy>
</system.net>

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.'

Error making request with Error Code ProxyAuthenticationRequired and Http Status Code ProxyAuthenticationRequired

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.

Amazon S3 Simple Storage Service call to list S3 buckets





AWS Tutorials

AWS Tutorials

AWS








Copyright © 2004 - 2019 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems