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

HicServices / RDMP / 10405576686

15 Aug 2024 02:38PM UTC coverage: 57.421% (+0.5%) from 56.907%
10405576686

push

github

JFriel
updated tests

11190 of 20986 branches covered (53.32%)

Branch coverage included in aggregate %.

31668 of 53652 relevant lines covered (59.02%)

4095.34 hits per line

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

74.7
/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)
4✔
33
    {
34
        Profile = profile ?? "default";
4!
35
        Region = region;
4✔
36
        _credentials = AWSCredentialsHelper.LoadSsoCredentials(Profile);
4✔
37
        var awsEndpoint = "http://127.0.0.1:9000";// Environment.GetEnvironmentVariable("AWS_ENDPOINT_URL");
4✔
38
        if (awsEndpoint != null)
4!
39
        {
40
            AmazonS3Config config = new AmazonS3Config()
4✔
41
            {
4✔
42
                ServiceURL = awsEndpoint,
4✔
43
                UseHttp = true,
4✔
44
                ForcePathStyle = true,
4✔
45
                //ProxyHost = "127.0.0.1",
4✔
46
                //ProxyPort = 9000
4✔
47
            };
4✔
48
            _client = new AmazonS3Client(_credentials, config);
4✔
49
        }
50
        else
51
        {
52
            _client = new AmazonS3Client(_credentials, Region);
×
53
        }
54
    }
×
55

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

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

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

78
    public async Task<bool> DoesObjectExists(string Key, string bucketName)
79
    {
80
        ListObjectsResponse response = null;
1✔
81
        try
82
        {
83

84
            ListObjectsRequest request = new ListObjectsRequest
1✔
85
            {
1✔
86
                BucketName = bucketName,
1✔
87
                Prefix = Key
1✔
88
            };
1✔
89
            response = await _client.ListObjectsAsync(request);
1✔
90

91
        }
1✔
92
        catch (Exception ex)
×
93
        {
94
            Console.WriteLine(ex.Message);
×
95
        }
×
96
        return (response != null && response.S3Objects != null && response.S3Objects.Count > 0 && response.S3Objects.Any(o => o.Key == Key));
2!
97
    }
1✔
98

99

100
    public bool ObjectExists(string fileKey, string bucketName)
101
    {
102
        try
103
        {
104
            var response = _client.GetObjectMetadataAsync(new GetObjectMetadataRequest()
2✔
105
            {
2✔
106
                BucketName = bucketName,
2✔
107
                Key = fileKey
2✔
108
            });
2✔
109
            var result = response.Result;
2✔
110

111
            return true;
2✔
112
        }
113

114
        catch (Exception ex)
×
115
        {
116
            Console.WriteLine(ex.Message);
×
117
            return false;
×
118
        }
119
    }
2✔
120

121
    public void DeleteObject(string fileKey, string bucketName)
122
    {
123
        _client.DeleteObjectAsync(new DeleteObjectRequest()
×
124
        {
×
125
            BucketName = bucketName,
×
126
            Key = fileKey,
×
127
        });
×
128
    }
×
129
    public async Task<HttpStatusCode> PutObject(string bucketName, string objectName, string localFilePath, string bucketSubdirectory = null)
130
    {
131

132
        var key = objectName;
8✔
133
        if (bucketSubdirectory != null)
8✔
134
            key = KeyGenerator(bucketSubdirectory, key);
8✔
135
        var request = new PutObjectRequest
8✔
136
        {
8✔
137
            BucketName = bucketName,
8✔
138
            Key = key,
8✔
139
            FilePath = localFilePath,
8✔
140
        };
8✔
141
        var response = await _client.PutObjectAsync(request);
8✔
142
        return response.HttpStatusCode;
8✔
143
    }
8✔
144
}
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