Newbie in C#.
Trying to use the below code to check if a certain bucket exists in S3.
using System;
using Amazon.S3;
using Amazon.S3.Util;
namespace CRO
{
internal class Program
{
static void Main(string[] args)
{
Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", "123");
Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", "456");
Environment.SetEnvironmentVariable("AWS_REGION", "ap-southeast-2");
Console.WriteLine(DoesBucketExist("randombucket").GetType());
Console.WriteLine("Done");
}
public static async Task<bool> DoesBucketExist(string bucketName)
{
using (var s3Client = new AmazonS3Client(Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID"), Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY")))
{
DateTime currentDateTime = DateTime.Now;
Console.WriteLine("Current date and time: " + currentDateTime);
return await AmazonS3Util.DoesS3BucketExistV2Async(s3Client, bucketName);
}
}
}
}
I’m expecting a boolean value (true or false) to be returned.
But when I check the data type below is what I get instead of a "boolean".
System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1+AsyncStateMachineBox
1[System.Boolean,CRO.Program+d__1]
Very new to the C# world. Just wondering what I’m doing wrong.
Trying with .net core if it helps.
>Solution :
To get the value from the async method, you should use await
. Also, you should change the Main
method to an asynchronous method.
static async Task Main(string[] args)
{
...
Console.WriteLine(await DoesBucketExist("randombucket"));
}
Object.GetType()
returns the type name of the instance.