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

HicServices / RDMP / 16414523978

21 Jul 2025 10:27AM UTC coverage: 57.423% (+0.1%) from 57.301%
16414523978

Pull #2205

github

web-flow
Update DataExportChildProvider.cs
Pull Request #2205: Multi-Project Catalogues

11427 of 21435 branches covered (53.31%)

Branch coverage included in aggregate %.

101 of 145 new or added lines in 16 files covered. (69.66%)

32 existing lines in 4 files now uncovered.

32393 of 54876 relevant lines covered (59.03%)

17545.03 hits per line

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

69.23
/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 System;
13
using System.Collections.Generic;
14
using System.IO;
15
using System.Linq;
16
using System.Net;
17
using System.Threading.Tasks;
18

19
namespace Rdmp.Core.ReusableLibraryCode.AWS;
20

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

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

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

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

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

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

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

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

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

108
        var key = objectName;
16✔
109
        if (bucketSubdirectory != null)
16✔
110
            key = KeyGenerator(bucketSubdirectory, key);
16✔
111
        var request = new PutObjectRequest
16✔
112
        {
16✔
113
            BucketName = bucketName,
16✔
114
            Key = key,
16✔
115
            FilePath = localFilePath,
16✔
116
        };
16✔
117
        var response = await _client.PutObjectAsync(request);
16✔
118
        return response.HttpStatusCode;
16✔
119
    }
16✔
120
}
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

© 2025 Coveralls, Inc