Use S3 file as QuickSight DataSet in C#

44 Views Asked by At

I am trying to connect my CSV file in S3 to be a Dataset in QuickSight.

This is how I upload my data and create my manifest file in S3.

using System;
using Amazon.S3;
using Amazon.S3.Model;
using System.IO;
using Amazon.QuickSight.Model;
using Amazon.QuickSight;

namespace QuickSight
{
    public class AmazonS3Uploader
    {

        public string BuildURI(string bucket, string key)
        {
            return @"s3://" + bucket + "/" + key;
        }

        public void CreateManifestFileFor(string filename, string bucket, string key)
        {
     
            string uri = BuildURI(bucket, key);
             var file = @"{
                        'fileLocations': [
                                            {
                                            'URIs': [
                                                        '" + uri + @"'
                                                    ]
                                            }
                                        ],
                        'globalUploadSettings': {
                                        'format': 'CSV',
                            'delimiter': ',',
                            'containsHeader': 'true'
                            }
                        }";
            File.WriteAllText(filename, file);
        }

        public async void UploadFile(string localFilePath, string bucketName, string keyName)
        {
            
            var awsCredentials = new Amazon.Runtime.BasicAWSCredentials(access_key, secret_key);
         
            var client = new AmazonS3Client(awsCredentials, Amazon.RegionEndpoint.USEast1);

            try
            {
                PutObjectRequest putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName,
                    FilePath = localFilePath,
                    ContentType = "text/plain"
                };

                PutObjectResponse response = await client.PutObjectAsync(putRequest);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                    ||
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    throw new Exception("Check the provided AWS Credentials.");
                }
                else
                {
                    throw new Exception("Error occurred: " + amazonS3Exception.Message);
                }
            }
        }
    }
}

I am able to use the manifest to upload the data via the console in AWS. However, I want to automate it in C#. I see in the AWS API that I can create a DataSource and a DataSet.

First, in the console I just click on S3 as a DataSource so do I need to create an S3 Datasource, especially since the DataSet call is able to specify S3 as a source?

Second, is there a way to do this in C# and if yes what would that look like?

0

There are 0 best solutions below