Monday, February 21, 2011

Amazon Web Services Simple Queue Service - How to Get Queue Name from Queue Url

This is a snippet to retrieve the queue name from the AWS (Amazon Web Services) SQS (Simple Queue Service) from the queue url using a Regular Expression.

It's pretty simple, but this regular expression will retrieve the queue name, and provide it back as a group. This can use used to retrieve the information, and provide it back in a consistent manner.

string queueUrl = "http://queue.amazonaws.com/A1MU5FWLQSN7CU/SimpleQueue";
var expression = "^http://queue.amazonaws.com/[0-9a-zA-z]+/(?[0-9a-zA-z]+)$";

Regex regEx = new Regex(expression);
Match m = regEx.Match(queueUrl);

if (m.Success)
{
var group = m.Groups["name"];

var found = group.Success;
var value = group.Value;

Console.WriteLine("Found Queue? {0}", found);
Console.WriteLine("Queue Value: {0}", value);
}

No comments: