• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HicServices / RDMP / 13318089130

13 Feb 2025 10:13PM UTC coverage: 57.398% (+0.004%) from 57.394%
13318089130

Pull #2134

github

jas88
Update ChildProviderTests.cs

Fix up TestUpTo method
Pull Request #2134: CodeQL fixups

11346 of 21308 branches covered (53.25%)

Branch coverage included in aggregate %.

104 of 175 new or added lines in 45 files covered. (59.43%)

362 existing lines in 23 files now uncovered.

32218 of 54590 relevant lines covered (59.02%)

17091.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.85
/Rdmp.Core/ReusableLibraryCode/AWS/AWSS3.cs
1
// Copyright (c) The University of Dundee 2024-2024
2
// This file is part of the Research Data Management Platform (RDMP).
3
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
6

7

8
using Amazon;
9
using Amazon.Runtime;
10
using Amazon.S3;
11
using Amazon.S3.Model;
12
using Org.BouncyCastle.Security.Certificates;
13
using System;
14
using System.Collections.Generic;
15
using System.IO;
16
using System.Linq;
17
using System.Net;
18
using System.Threading.Tasks;
19

20
namespace Rdmp.Core.ReusableLibraryCode.AWS;
21

22
/// <summary>
23
/// Helper Class to interact with AWS and S3 Buckets
24
/// </summary>
25
public class AWSS3
26
{
27

28
    public readonly string Profile;
29
    public readonly RegionEndpoint Region;
30
    private readonly AWSCredentials _credentials;
31
    private readonly AmazonS3Client _client;
32

33
    public AWSS3(string profile, RegionEndpoint region)
14✔
34
    {
35
        Profile = profile ?? "default";
14!
36
        Region = region;
14✔
37
        _credentials = AWSCredentialsHelper.LoadSsoCredentials(Profile);
14✔
38
        var awsEndpoint = Environment.GetEnvironmentVariable("AWS_ENDPOINT_URL");
12✔
39
        if (awsEndpoint != null)
12!
40
        {
41
            AmazonS3Config config = new AmazonS3Config()
12✔
42
            {
12✔
43
                ServiceURL = awsEndpoint,
12✔
44
                UseHttp = true,
12✔
45
                ForcePathStyle = true,
12✔
46
            };
12✔
47
            _client = new AmazonS3Client(_credentials, config);
12✔
48
        }
49
        else
50
        {
UNCOV
51
            _client = new AmazonS3Client(_credentials, Region);
×
52
        }
UNCOV
53
    }
×
54

55
    public async Task<List<S3Bucket>> ListAvailableBuckets()
56
    {
57
        var foundBuckets = await _client.ListBucketsAsync();
2✔
58
        return foundBuckets.Buckets;
2✔
59
    }
2✔
60

61
    public async Task<S3Bucket> GetBucket(string bucketName)
62
    {
63
        var foundBuckets = await _client.ListBucketsAsync();
10✔
64
        var bucket = foundBuckets.Buckets.Single(bucket => bucket.BucketName == bucketName);
18✔
65
        if (bucket == null)
6!
66
        {
UNCOV
67
            throw new Exception("Bucket not found...");
×
68
        }
69
        return bucket;
6✔
70
    }
6✔
71

72
    public static string KeyGenerator(string path, string file)
73
    {
74
        return Path.Join(path, file).Replace("\\", "/");
16✔
75
    }
76

77
    public bool ObjectExists(string fileKey, string bucketName)
78
    {
79
        try
80
        {
81
            var response = _client.GetObjectMetadataAsync(new GetObjectMetadataRequest()
6✔
82
            {
6✔
83
                BucketName = bucketName,
6✔
84
                Key = fileKey
6✔
85
            });
6✔
86
            if (response.Result is not null)
6!
87
                return true;
6✔
UNCOV
88
            return false;
×
89
        }
90

UNCOV
91
        catch (Exception ex)
×
92
        {
93
            Console.WriteLine(ex.Message);
×
UNCOV
94
            return false;
×
95
        }
96
    }
6✔
97

98
    public void DeleteObject(string fileKey, string bucketName)
99
    {
100
        _client.DeleteObjectAsync(new DeleteObjectRequest()
×
101
        {
×
102
            BucketName = bucketName,
×
103
            Key = fileKey,
×
104
        });
×
UNCOV
105
    }
×
106
    public async Task<HttpStatusCode> PutObject(string bucketName, string objectName, string localFilePath, string bucketSubdirectory = null)
107
    {
108

109
        var key = objectName;
16✔
110
        if (bucketSubdirectory != null)
16✔
111
            key = KeyGenerator(bucketSubdirectory, key);
16✔
112
        var request = new PutObjectRequest
16✔
113
        {
16✔
114
            BucketName = bucketName,
16✔
115
            Key = key,
16✔
116
            FilePath = localFilePath,
16✔
117
        };
16✔
118
        var response = await _client.PutObjectAsync(request);
16✔
119
        return response.HttpStatusCode;
16✔
120
    }
16✔
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc