I have a s3 path => s3://[bucket name]/[key]
s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png
and I need get the bucket_name and the key separately:
var s3PathParsed = parseS3Path("s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png");
s3PathParsed.BucketName == "bn-complete-dev-test"
s3PathParsed.Key == "1234567890/renders/Irradiance_A.png"
how to I could parse in the correct way using the AWS SDK?
1) I am parsing manually (using a regular expression) and work fine but i am not comfortable:
public class S3Path : IS3Path
{
private const string _s3PathRegex = @"[s|S]3:\/\/(?<bucket>[^\/]*)\/(?<key>.*)";
public S3Path(string s3Path)
{
Path = s3Path;
var rx = new Regex(_s3PathRegex).Match(s3Path);
if (!rx.Success || rx.Groups.Count != 3)
throw new Exception($"the S3 Path '{s3Path}' is wrong.");
BucketName = rx.Groups[1].Value;
Key = rx.Groups[2].Value;
}
public string Path { get; }
public string BucketName { get; }
public string Key { get; }
}
2) I used the AmazonS3Uri from AWWSDK.S3:
string GetBucketNameFromS3Uri(string s3Uri)
{
return new AmazonS3Uri(s3Uri).Bucket;
}
I called the method:
GetBucketNameFromS3Uri("s3://sunsite-complete-dev-test/1234567890/renders/Irradiance_A.png");
and i have the following error:
System.ArgumentException: 'Invalid S3 URI - hostname does not appear to be a valid S3 endpoint'
3) Also I try
string GetBucketNameFromS3Uri(string s3Uri)
{
return new AmazonS3Uri(new Uri(s3Uri)).Bucket;
}
with the same error.
I created a new thread in AWS Forum with this issue: https://forums.aws.amazon.com/thread.jspa?threadID=304401
In Java, We can do something like
AmazonS3URI s3URI = new AmazonS3URI("s3://bucket/folder/object.csv");
S3Object s3Object = s3Client.getObject(s3URI.getBucket(), s3URI.getKey());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With