title: How to parse the AWS S3 Path (s3://<bucket name>/<key>) using the AWSSDK.S3 in C# in order to get the bucket name & key
body: <p>I have a s3 path => s3://[bucket name]/[key]</p>
  
  <pre><code>s3://bn-complete-dev-test/1234567890/renders/Irradiance_A.png 
  </code></pre>
  
  <p>and I need get the bucket_name and the key separately: </p>
  
  <pre><code>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"
  </code></pre>
  
  <p><strong>how to I could parse in the correct way <em>using the AWS SDK</em>?</strong></p>
  
  <p>1) I am parsing manually (using a regular expression) and <strong>work fine but i am not comfortable</strong>:</p>
  
  <pre><code>public class S3Path : IS3Path
  {
      private const string _s3PathRegex = @"[s|S]3:\/\/(?&lt;bucket&gt;[^\/]*)\/(?&lt;key&gt;.*)";
  
      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; }
  }
  </code></pre>
  
  <p>2) I used the AmazonS3Uri from AWWSDK.S3:</p>
  
  <pre><code>string GetBucketNameFromS3Uri(string s3Uri)
  {
      return new AmazonS3Uri(s3Uri).Bucket;            
  }
  </code></pre>
  
  <p>I called the method:</p>
  
  <pre><code>GetBucketNameFromS3Uri("s3://sunsite-complete-dev-test/1234567890/renders/Irradiance_A.png");
  </code></pre>
  
  <p>and i have the following error:</p>
  
  <p>System.ArgumentException: 'Invalid S3 URI - hostname does not appear to be a valid S3 endpoint'</p>
  
  <p>3) Also I try </p>
  
  <pre><code>string GetBucketNameFromS3Uri(string s3Uri)
  {
      return new AmazonS3Uri(new Uri(s3Uri)).Bucket;            
  }
  </code></pre>
  
  <p>with the same error.    </p>
  
  <p>I created a new thread in AWS Forum with this issue: <a href="https://forums.aws.amazon.com/thread.jspa?threadID=304401" rel="noreferrer">https://forums.aws.amazon.com/thread.jspa?threadID=304401</a></p>