Skip to content

Commit

Permalink
document class
Browse files Browse the repository at this point in the history
  • Loading branch information
JFriel committed Aug 9, 2024
1 parent 3790371 commit c4be248
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public class AWSS3BucketReleaseDestination : IPluginDataFlowComponent<ReleaseAud
[DemandsInitialization("The folder in the S3 bucket you wish to release to", defaultValue: "")]
public string BucketFolder { get; set; }

[DemandsInitialization("If selected, AWS configuration will be asked for at runtime", defaultValue: false)]
public bool ConfigureInteractivelyOnRelease { get; set; }

private ReleaseData _releaseData;
private Project _project;
private AWSReleaseEngine _engine;
Expand Down
169 changes: 83 additions & 86 deletions Rdmp.Core/ReusableLibraryCode/AWS/AWSS3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,123 +9,120 @@
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Azure;
using MathNet.Numerics.Statistics;
using Renci.SshNet;
using SharpCompress.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.AccessControl;
using System.Threading.Tasks;

namespace Rdmp.Core.ReusableLibraryCode.AWS
namespace Rdmp.Core.ReusableLibraryCode.AWS;

/// <summary>
/// Helper Class to interact with AWS and S3 Buckets
/// </summary>
public class AWSS3
{
public class AWSS3

public readonly string Profile;
public readonly RegionEndpoint Region;
private readonly AWSCredentials _credentials;
private readonly AmazonS3Client _client;

public AWSS3(string profile, RegionEndpoint region)
{
Profile = profile ?? "default";
Region = region;
_credentials = AWSCredentialsHelper.LoadSsoCredentials(Profile);
_client = new AmazonS3Client(_credentials, Region);
}

public readonly string Profile;
public readonly RegionEndpoint Region;
private readonly AWSCredentials _credentials;
private readonly AmazonS3Client _client;
public async Task<List<S3Bucket>> ListAvailableBuckets()
{
var foundBuckets = await _client.ListBucketsAsync();
return foundBuckets.Buckets;
}

public AWSS3(string profile, RegionEndpoint region)
public async Task<S3Bucket> GetBucket(string bucketName)
{
var foundBuckets = await _client.ListBucketsAsync();
var bucket = foundBuckets.Buckets.Single(bucket => bucket.BucketName == bucketName);
if (bucket == null)
{
Profile = profile ?? "default";
Region = region;
_credentials = AWSCredentialsHelper.LoadSsoCredentials(Profile);
_client = new AmazonS3Client(_credentials, Region);
throw new Exception("Bucket not found...");
}
return bucket;
}

public async Task<List<S3Bucket>> ListAvailableBuckets()
{
var foundBuckets = await _client.ListBucketsAsync();
return foundBuckets.Buckets;
}
public static string KeyGenerator(string path, string file)
{
return Path.Join(path, file).Replace("\\", "/");//todo there is probably a better way to do this
}

public async Task<S3Bucket> GetBucket(string bucketName)
public async Task<bool> DoesObjectExists(string Key, string bucketName)
{
ListObjectsResponse response = null;
try
{
var foundBuckets = await _client.ListBucketsAsync();
var bucket = foundBuckets.Buckets.Single(bucket => bucket.BucketName == bucketName);
if (bucket == null)

ListObjectsRequest request = new ListObjectsRequest
{
throw new Exception("Bucket not found...");
}
return bucket;
}
BucketName = bucketName,
Prefix = Key
};
response = await _client.ListObjectsAsync(request);

public static string KeyGenerator(string path, string file)
{
return Path.Join(path, file).Replace("\\", "/");//todo there is probably a better way to do this
}

public async Task<bool> DoesObjectExists(string Key, string bucketName)
catch (Exception ex)
{
ListObjectsResponse response = null;
try
{

ListObjectsRequest request = new ListObjectsRequest
{
BucketName = bucketName,
Prefix = Key
};
response = await _client.ListObjectsAsync(request);

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return (response != null && response.S3Objects != null && response.S3Objects.Count > 0 && response.S3Objects.Any(o => o.Key == Key));
Console.WriteLine(ex.Message);
}
return (response != null && response.S3Objects != null && response.S3Objects.Count > 0 && response.S3Objects.Any(o => o.Key == Key));
}


public bool ObjectExists(string fileKey, string bucketName)
public bool ObjectExists(string fileKey, string bucketName)
{
try
{
try
var response = _client.GetObjectMetadataAsync(new GetObjectMetadataRequest()
{
var response = _client.GetObjectMetadataAsync(new GetObjectMetadataRequest()
{
BucketName = bucketName,
Key = fileKey
});
var result = response.Result;

return true;
}
BucketName = bucketName,
Key = fileKey
});
var result = response.Result;

catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
return true;
}

public void DeleteObject(string fileKey, string bucketName)
catch (Exception ex)
{
_client.DeleteObjectAsync(new DeleteObjectRequest()
{
BucketName = bucketName,
Key = fileKey,
});
Console.WriteLine(ex.Message);
return false;
}
public async Task<HttpStatusCode> PutObject(string bucketName, string objectName, string localFilePath, string bucketSubdirectory = null)
}

public void DeleteObject(string fileKey, string bucketName)
{
_client.DeleteObjectAsync(new DeleteObjectRequest()
{
BucketName = bucketName,
Key = fileKey,
});
}
public async Task<HttpStatusCode> PutObject(string bucketName, string objectName, string localFilePath, string bucketSubdirectory = null)
{

var key = objectName;
if (bucketSubdirectory != null)
key = KeyGenerator(bucketSubdirectory, key);
var request = new PutObjectRequest
{
BucketName = bucketName,
Key = key,
FilePath = localFilePath,
};
var response = await _client.PutObjectAsync(request);
return response.HttpStatusCode;
}
var key = objectName;
if (bucketSubdirectory != null)
key = KeyGenerator(bucketSubdirectory, key);
var request = new PutObjectRequest
{
BucketName = bucketName,
Key = key,
FilePath = localFilePath,
};
var response = await _client.PutObjectAsync(request);
return response.HttpStatusCode;
}
}

0 comments on commit c4be248

Please sign in to comment.