Table of Contents

Class AWSSDKUtils

Namespace
Amazon.Util
Assembly
AWSSDK.Core.dll

This class defines utilities and constants that can be used by all the client libraries of the SDK.

public static class AWSSDKUtils
Inheritance
AWSSDKUtils
Inherited Members

Fields

DefaultBufferSize

public const int DefaultBufferSize = 8192

Field Value

int

DefaultProgressUpdateInterval

public const long DefaultProgressUpdateInterval = 102400

Field Value

long

EPOCH_START

public static readonly DateTime EPOCH_START

Field Value

DateTime

GMTDateFormat

The GMT Date Format string. Used when parsing date objects

public const string GMTDateFormat = "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T"

Field Value

string

ISO8601BasicDateFormat

The ISO8601 basic date format. Used during AWS4 signature computation.

public const string ISO8601BasicDateFormat = "yyyyMMdd"

Field Value

string

ISO8601BasicDateTimeFormat

The ISO8601 Basic date/time format string. Used when parsing date objects

public const string ISO8601BasicDateTimeFormat = "yyyyMMddTHHmmssZ"

Field Value

string

ISO8601DateFormat

The ISO8601Date Format string. Used when parsing date objects

public const string ISO8601DateFormat = "yyyy-MM-dd\\THH:mm:ss.fff\\Z"

Field Value

string

ISO8601DateFormatNoMS

The ISO8601Date Format string. Used when parsing date objects

public const string ISO8601DateFormatNoMS = "yyyy-MM-dd\\THH:mm:ss\\Z"

Field Value

string

RFC822DateFormat

The RFC822Date Format string. Used when parsing date objects

public const string RFC822DateFormat = "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T"

Field Value

string

UrlEncodedContent

The string representing Url Encoded Content in HTTP requests

public const string UrlEncodedContent = "application/x-www-form-urlencoded; charset=utf-8"

Field Value

string

UserAgentHeader

The user agent string header

public const string UserAgentHeader = "User-Agent"

Field Value

string

ValidTraceIdHeaderValueCharacters

The set of characters which are not to be encoded as part of the X-Amzn-Trace-Id header values

public const string ValidTraceIdHeaderValueCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=;:+&[]{}\"',"

Field Value

string

ValidUrlCharacters

The Set of accepted and valid Url characters per RFC3986. Characters outside of this set will be encoded.

public const string ValidUrlCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"

Field Value

string

ValidUrlCharactersRFC1738

The Set of accepted and valid Url characters per RFC1738. Characters outside of this set will be encoded.

public const string ValidUrlCharactersRFC1738 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_."

Field Value

string

Properties

CorrectedUtcNow

Returns DateTime.UtcNow + ManualClockCorrection when ManualClockCorrection is set. This value should be used instead of DateTime.UtcNow to factor in manual clock correction

[Obsolete("This property does not account for endpoint specific clock skew.  Use CorrectClockSkew.GetCorrectedUtcNowForEndpoint() instead.")]
public static DateTime CorrectedUtcNow { get; }

Property Value

DateTime

FormattedCurrentTimestampGMT

Formats the current date as a GMT timestamp

public static string FormattedCurrentTimestampGMT { get; }

Property Value

string

A GMT formatted string representation of the current date and time

FormattedCurrentTimestampISO8601

Formats the current date as ISO 8601 timestamp

public static string FormattedCurrentTimestampISO8601 { get; }

Property Value

string

An ISO 8601 formatted string representation of the current date and time

FormattedCurrentTimestampRFC822

Formats the current date as ISO 8601 timestamp

public static string FormattedCurrentTimestampRFC822 { get; }

Property Value

string

An ISO 8601 formatted string representation of the current date and time

Methods

BytesToHexString(byte[])

Convert bytes to a hex string

public static string BytesToHexString(byte[] value)

Parameters

value byte[]

Bytes to convert.

Returns

string

Hexadecimal string representing the byte array.

CanonicalizeResourcePath(Uri, string)

Returns the canonicalized resource path for the service endpoint with single URL encoded path segments.

[Obsolete("Use CanonicalizeResourcePathV2 instead")]
public static string CanonicalizeResourcePath(Uri endpoint, string resourcePath)

Parameters

endpoint Uri

Endpoint URL for the request

resourcePath string

Resource path for the request

Returns

string

Canonicalized resource path for the endpoint

Remarks

If resourcePath begins or ends with slash, the resulting canonicalized path will follow suit.

CanonicalizeResourcePath(Uri, string, bool)

Returns the canonicalized resource path for the service endpoint

[Obsolete("Use CanonicalizeResourcePathV2 instead")]
public static string CanonicalizeResourcePath(Uri endpoint, string resourcePath, bool detectPreEncode)

Parameters

endpoint Uri

Endpoint URL for the request

resourcePath string

Resource path for the request

detectPreEncode bool

If true pre URL encode path segments if necessary. S3 is currently the only service that does not expect pre URL encoded segments.

Returns

string

Canonicalized resource path for the endpoint

Remarks

If resourcePath begins or ends with slash, the resulting canonicalized path will follow suit.

CanonicalizeResourcePath(Uri, string, bool, IDictionary<string, string>, int)

Returns the canonicalized resource path for the service endpoint

[Obsolete("Use CanonicalizeResourcePathV2 instead")]
public static string CanonicalizeResourcePath(Uri endpoint, string resourcePath, bool detectPreEncode, IDictionary<string, string> pathResources, int marshallerVersion)

Parameters

endpoint Uri

Endpoint URL for the request

resourcePath string

Resource path for the request

detectPreEncode bool

If true pre URL encode path segments if necessary. S3 is currently the only service that does not expect pre URL encoded segments.

pathResources IDictionary<string, string>

Dictionary of key/value parameters containing the values for the ResourcePath key replacements

marshallerVersion int

The version of the marshaller that constructed the request object.

Returns

string

Canonicalized resource path for the endpoint

Remarks

If resourcePath begins or ends with slash, the resulting canonicalized path will follow suit.

CanonicalizeResourcePathV2(Uri, string, bool, IDictionary<string, string>)

Returns the canonicalized resource path for the service endpoint.

public static string CanonicalizeResourcePathV2(Uri endpoint, string resourcePath, bool encode, IDictionary<string, string> pathResources)

Parameters

endpoint Uri

Endpoint URL for the request.

resourcePath string

Resource path for the request.

encode bool

If true will URL-encode path segments including "/". "S3" is currently the only service that does not expect pre URL-encoded segments.

pathResources IDictionary<string, string>

Dictionary of key/value parameters containing the values for the ResourcePath key replacements.

Returns

string

Canonicalized resource path for the endpoint.

Remarks

If resourcePath begins or ends with slash, the resulting canonicalized path will follow suit.

CompressSpaces(string)

Utility method that accepts a string and replaces white spaces with a space.

public static string CompressSpaces(string data)

Parameters

data string

Returns

string

ConvertDateTimetoMilliseconds(DateTime)

public static long ConvertDateTimetoMilliseconds(DateTime dateTime)

Parameters

dateTime DateTime

Returns

long

ConvertFromUnixEpochMilliseconds(long)

Utility method for converting Unix epoch milliseconds to DateTime structure.

public static DateTime ConvertFromUnixEpochMilliseconds(long milliseconds)

Parameters

milliseconds long

The number of milliseconds since January 1, 1970.

Returns

DateTime

Converted DateTime structure

ConvertFromUnixEpochSeconds(int)

Utility method for converting Unix epoch seconds to DateTime structure.

public static DateTime ConvertFromUnixEpochSeconds(int seconds)

Parameters

seconds int

The number of seconds since January 1, 1970.

Returns

DateTime

Converted DateTime structure

ConvertTimeSpanToMilliseconds(TimeSpan)

public static long ConvertTimeSpanToMilliseconds(TimeSpan timeSpan)

Parameters

timeSpan TimeSpan

Returns

long

ConvertToUnixEpochMilliSeconds(DateTime)

[Obsolete("This method isn't named correctly: it returns seconds instead of milliseconds. Use ConvertToUnixEpochSecondsDouble instead.", false)]
public static double ConvertToUnixEpochMilliSeconds(DateTime dateTime)

Parameters

dateTime DateTime

Returns

double

ConvertToUnixEpochSeconds(DateTime)

public static int ConvertToUnixEpochSeconds(DateTime dateTime)

Parameters

dateTime DateTime

Returns

int

ConvertToUnixEpochSecondsDouble(DateTime)

public static double ConvertToUnixEpochSecondsDouble(DateTime dateTime)

Parameters

dateTime DateTime

Returns

double

ConvertToUnixEpochSecondsString(DateTime)

public static string ConvertToUnixEpochSecondsString(DateTime dateTime)

Parameters

dateTime DateTime

Returns

string

CopyStream(Stream, Stream)

Utility method for copy the contents of the source stream to the destination stream.

public static void CopyStream(Stream source, Stream destination)

Parameters

source Stream
destination Stream

CopyStream(Stream, Stream, int)

Utility method for copy the contents of the source stream to the destination stream.

public static void CopyStream(Stream source, Stream destination, int bufferSize)

Parameters

source Stream
destination Stream
bufferSize int

DetermineRegion(string)

Attempt to infer the region for a service request based on the endpoint

public static string DetermineRegion(string url)

Parameters

url string

Endpoint to the service to be called

Returns

string

Region parsed from the endpoint; DefaultRegion (or DefaultGovRegion) if it cannot be determined/is not explicit

DetermineService(string)

Attempt to infer the service name for a request (in short form, eg 'iam') from the service endpoint.

public static string DetermineService(string url)

Parameters

url string

Endpoint to the service to be called

Returns

string

Short-form name of the service parsed from the endpoint; empty string if it cannot be determined

DownloadStringContent(Uri)

public static string DownloadStringContent(Uri uri)

Parameters

uri Uri

Returns

string

DownloadStringContent(Uri, IWebProxy)

public static string DownloadStringContent(Uri uri, IWebProxy proxy)

Parameters

uri Uri
proxy IWebProxy

Returns

string

DownloadStringContent(Uri, TimeSpan)

public static string DownloadStringContent(Uri uri, TimeSpan timeout)

Parameters

uri Uri
timeout TimeSpan

Returns

string

DownloadStringContent(Uri, TimeSpan, IWebProxy)

public static string DownloadStringContent(Uri uri, TimeSpan timeout, IWebProxy proxy)

Parameters

uri Uri
timeout TimeSpan
proxy IWebProxy

Returns

string

ExecuteHttpRequest(Uri, string, string, TimeSpan, IWebProxy, IDictionary<string, string>)

Executes an HTTP request and returns the response as a string. This method throws WebException and HttpRequestException. In the event HttpRequestException is thrown the StatusCode is sent as user defined data on the exception under the key "StatusCode".

public static string ExecuteHttpRequest(Uri uri, string requestType, string content, TimeSpan timeout, IWebProxy proxy, IDictionary<string, string> headers)

Parameters

uri Uri

The URI to make the request to

requestType string

The request type: GET, PUT, POST

content string

null or the content to send with the request

timeout TimeSpan

Timeout for the request

proxy IWebProxy

Proxy for the request

headers IDictionary<string, string>

null or any headers to send with the request

Returns

string

The response as a string.

ForceCanonicalPathAndQuery(Uri)

public static void ForceCanonicalPathAndQuery(Uri uri)

Parameters

uri Uri

GenerateChecksumForBytes(byte[], bool)

Generates an MD5 Digest for the given byte array

public static string GenerateChecksumForBytes(byte[] content, bool fBase64Encode)

Parameters

content byte[]

The content for which the MD5 Digest needs to be computed.

fBase64Encode bool

Whether the returned checksum should be base64 encoded.

Returns

string

A string representation of the hash with or w/o base64 encoding

GenerateChecksumForContent(string, bool)

Generates an MD5 Digest for the string-based content

public static string GenerateChecksumForContent(string content, bool fBase64Encode)

Parameters

content string

The content for which the MD5 Digest needs to be computed.

fBase64Encode bool

Whether the returned checksum should be base64 encoded.

Returns

string

A string representation of the hash with or w/o base64 encoding

GenerateMD5ChecksumForStream(Stream)

Generates an MD5 Digest for the stream specified

public static string GenerateMD5ChecksumForStream(Stream input)

Parameters

input Stream

The Stream for which the MD5 Digest needs to be computed.

Returns

string

A string representation of the hash with base64 encoding

GenerateMemoryStreamFromString(string)

Utility method for converting a string to a MemoryStream.

public static MemoryStream GenerateMemoryStreamFromString(string s)

Parameters

s string

Returns

MemoryStream

GetExtension(string)

Returns an extension of a path. This has the same behavior as System.IO.Path.GetExtension, but does not check the path for invalid characters.

public static string GetExtension(string path)

Parameters

path string

Returns

string

GetFormattedTimestampISO8601(int)

Gets the ISO8601 formatted timestamp that is minutesFromNow in the future.

public static string GetFormattedTimestampISO8601(int minutesFromNow)

Parameters

minutesFromNow int

The number of minutes from the current instant for which the timestamp is needed.

Returns

string

The ISO8601 formatted future timestamp.

GetFormattedTimestampRFC822(int)

Gets the RFC822 formatted timestamp that is minutesFromNow in the future.

public static string GetFormattedTimestampRFC822(int minutesFromNow)

Parameters

minutesFromNow int

The number of minutes from the current instant for which the timestamp is needed.

Returns

string

The ISO8601 formatted future timestamp.

GetTimeSpanInTicks(DateTime)

public static TimeSpan GetTimeSpanInTicks(DateTime dateTime)

Parameters

dateTime DateTime

Returns

TimeSpan

HasBidiControlCharacters(string)

Returns true if the string has any bidirectional control characters.

public static bool HasBidiControlCharacters(string input)

Parameters

input string

Returns

bool

HexStringToBytes(string)

Convert a hex string to bytes

public static byte[] HexStringToBytes(string hex)

Parameters

hex string

Hexadecimal string

Returns

byte[]

Byte array corresponding to the hex string.

InvokeInBackground<T>(EventHandler<T>, T, object)

Calls a specific EventHandler in a background thread

public static void InvokeInBackground<T>(EventHandler<T> handler, T args, object sender) where T : EventArgs

Parameters

handler EventHandler<T>
args T
sender object

Type Parameters

T

IsAbsolutePath(string)

Determines whether the given string is an absolute path to a root.

public static bool IsAbsolutePath(string path)

Parameters

path string

The hypothetical absolute path.

Returns

bool

True if path is an absolute path.

IsPropertySet(object, string)

This method allows to check whether a property of an object returned by a service call is set. This method is needed to discriminate whether a field is not set (not present in the service response) or if it is set to the default value for its type. Using this method is not required for nullable properties (non-ValueType and Nullable) because they will be simply set to null when not included in the service response. This method can also be used on objects used as part of service requests. This method doesn't support objects that are part of the S3 service model.

public static bool IsPropertySet(object awsServiceObject, string propertyName)

Parameters

awsServiceObject object

An object that is used in an AWS service request or is returned as part of an AWS service response.

propertyName string

The name of the property of awsServiceObject to check.

Returns

bool

True if the property is set, otherwise false.

Join(List<string>)

Returns a new string created by joining each of the strings in the specified list together, with a comma between them.

public static string Join(List<string> strings)

Parameters

strings List<string>

Returns

string

A new string created by joining each of the strings in the specified list together, with a comma between strings.

JoinResourcePathSegments(IEnumerable<string>, bool)

Joins all path segments with the / character and encodes each segment before joining.

public static string JoinResourcePathSegments(IEnumerable<string> pathSegments, bool path)

Parameters

pathSegments IEnumerable<string>

The segments of a URL path split at each / character

path bool

If the path property is specified, the accepted path characters {/+:} are not encoded.

Returns

string

A joined URL with encoded segments

OpenStream(Uri)

public static Stream OpenStream(Uri uri)

Parameters

uri Uri

Returns

Stream

OpenStream(Uri, IWebProxy)

public static Stream OpenStream(Uri uri, IWebProxy proxy)

Parameters

uri Uri
proxy IWebProxy

Returns

Stream

ParseQueryParameters(string)

Parses a query string of a URL and returns the parameters as a string-to-string dictionary.

public static Dictionary<string, string> ParseQueryParameters(string url)

Parameters

url string

Returns

Dictionary<string, string>

PreserveStackTrace(Exception)

This method is used preserve the stacktrace used from clients that support async calls. This make sure that exceptions thrown during EndXXX methods has the orignal stacktrace that happen in the background thread.

public static void PreserveStackTrace(Exception exception)

Parameters

exception Exception

ProtectEncodedSlashUrlEncode(string, bool)

URL encodes a string per the specified RFC with the exception of preserving the encoding of previously encoded slashes. If the path property is specified, the accepted path characters {/+:} are not encoded.

[Obsolete("This method is not supported in AWSSDK 3.5")]
public static string ProtectEncodedSlashUrlEncode(string data, bool path)

Parameters

data string

The string to encode

path bool

Whether the string is a URL path or not

Returns

string

The encoded string with any previously encoded %2F preserved

ResolveResourcePath(string, IDictionary<string, string>)

Takes a patterned resource path and resolves it using the key/value path resources into a segmented encoded URL.

public static string ResolveResourcePath(string resourcePath, IDictionary<string, string> pathResources)

Parameters

resourcePath string

The patterned resourcePath

pathResources IDictionary<string, string>

The key/value lookup for the patterned resourcePath

Returns

string

A segmented encoded URL

ResolveResourcePath(string, IDictionary<string, string>, bool)

Takes a patterned resource path and resolves it using the key/value path resources into a segmented encoded URL.

public static string ResolveResourcePath(string resourcePath, IDictionary<string, string> pathResources, bool skipEncodingValidPathChars)

Parameters

resourcePath string

The patterned resourcePath

pathResources IDictionary<string, string>

The key/value lookup for the patterned resourcePath

skipEncodingValidPathChars bool

If true valid path characters {/+:} are not encoded

Returns

string

A segmented encoded URL

RunProcess(ProcessStartInfo)

Runs a process with the below input parameters.

public static ProcessExecutionResult RunProcess(ProcessStartInfo processStartInfo)

Parameters

processStartInfo ProcessStartInfo

Returns

ProcessExecutionResult

RunProcessAsync(ProcessStartInfo)

public static Task<ProcessExecutionResult> RunProcessAsync(ProcessStartInfo processStartInfo)

Parameters

processStartInfo ProcessStartInfo

Returns

Task<ProcessExecutionResult>

Sleep(int)

public static void Sleep(int ms)

Parameters

ms int

Sleep(TimeSpan)

public static void Sleep(TimeSpan ts)

Parameters

ts TimeSpan

SplitResourcePathIntoSegments(string, IDictionary<string, string>)

Splits the resourcePath at / into segments then resolves any keys with the path resource values. Greedy key values will be split into multiple segments at each /.

public static IEnumerable<string> SplitResourcePathIntoSegments(string resourcePath, IDictionary<string, string> pathResources)

Parameters

resourcePath string

The patterned resourcePath

pathResources IDictionary<string, string>

The key/value lookup for the patterned resourcePath

Returns

IEnumerable<string>

A list of path segments where all keys in the resourcePath have been resolved to one or more path segment values

ToHex(byte[], bool)

Helper function to format a byte array into string

public static string ToHex(byte[] data, bool lowercase)

Parameters

data byte[]

The data blob to process

lowercase bool

If true, returns hex digits in lower case form

Returns

string

String version of the data

UrlEncode(int, string, bool)

URL encodes a string per the specified RFC. If the path property is specified, the accepted path characters {/+:} are not encoded.

public static string UrlEncode(int rfcNumber, string data, bool path)

Parameters

rfcNumber int

RFC number determing safe characters

data string

The string to encode

path bool

Whether the string is a URL path or not

Returns

string

The encoded string

Remarks

Currently recognised RFC versions are 1738 (Dec '94) and 3986 (Jan '05). If the specified RFC is not recognised, 3986 is used by default.

UrlEncode(string, bool)

URL encodes a string per RFC3986. If the path property is specified, the accepted path characters {/+:} are not encoded.

public static string UrlEncode(string data, bool path)

Parameters

data string

The string to encode

path bool

Whether the string is a URL path or not

Returns

string

The encoded string