public static string GetSqsQueueName(string destination, TransportConfiguration transportConfiguration)
        {
            if (string.IsNullOrWhiteSpace(destination))
            {
                throw new ArgumentNullException(nameof(destination));
            }

            var s = transportConfiguration.QueueNamePrefix + destination;

            if (transportConfiguration.PreTruncateQueueNames && s.Length > 80)
            {
                var charsToTake = 80 - transportConfiguration.QueueNamePrefix.Length;
                s = transportConfiguration.QueueNamePrefix +
                    new string(s.Reverse().Take(charsToTake).Reverse().ToArray());
            }

            if (s.Length > 80)
            {
                throw new Exception($"Address {destination} with configured prefix {transportConfiguration.QueueNamePrefix} is longer than 80 characters and therefore cannot be used to create an SQS queue. Use a shorter queue name.");
            }

            var queueNameBuilder = new StringBuilder(s);

            return(GetSanitizedQueueName(queueNameBuilder, s));
        }
        public static async Task <byte[]> RetrieveBody(this TransportMessage transportMessage,
                                                       IAmazonS3 s3Client,
                                                       TransportConfiguration transportConfiguration,
                                                       CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(transportMessage.S3BodyKey))
            {
                return(Convert.FromBase64String(transportMessage.Body));
            }

            var s3GetResponse = await s3Client.GetObjectAsync(transportConfiguration.S3BucketForLargeMessages,
                                                              transportMessage.S3BodyKey,
                                                              cancellationToken).ConfigureAwait(false);

            using (var memoryStream = new MemoryStream())
            {
                await s3GetResponse.ResponseStream.CopyToAsync(memoryStream).ConfigureAwait(false);

                return(memoryStream.ToArray());
            }
        }
Exemple #3
0
        public static string GetSqsQueueName(string destination, TransportConfiguration transportConfiguration)
        {
            if (string.IsNullOrWhiteSpace(destination))
            {
                throw new ArgumentNullException(nameof(destination));
            }

            var s = transportConfiguration.QueueNamePrefix + destination;

            if (transportConfiguration.PreTruncateQueueNames && s.Length > 80)
            {
                var charsToTake = 80 - transportConfiguration.QueueNamePrefix.Length;
                s = transportConfiguration.QueueNamePrefix +
                    new string(s.Reverse().Take(charsToTake).Reverse().ToArray());
            }

            if (s.Length > 80)
            {
                throw new Exception($"Address {destination} with configured prefix {transportConfiguration.QueueNamePrefix} is longer than 80 characters and therefore cannot be used to create an SQS queue. Use a shorter queue name.");
            }

            var skipCharacters   = s.EndsWith(".fifo") ? 5 : 0;
            var queueNameBuilder = new StringBuilder(s);

            // SQS queue names can only have alphanumeric characters, hyphens and underscores.
            // Any other characters will be replaced with a hyphen.
            for (var i = 0; i < queueNameBuilder.Length - skipCharacters; ++i)
            {
                var c = queueNameBuilder[i];
                if (!char.IsLetterOrDigit(c) &&
                    c != '-' &&
                    c != '_')
                {
                    queueNameBuilder[i] = '-';
                }
            }

            return(queueNameBuilder.ToString());
        }
        public static async Task <byte[]> RetrieveBody(this TransportMessage transportMessage,
                                                       IAmazonS3 s3Client,
                                                       TransportConfiguration transportConfiguration,
                                                       CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(transportMessage.S3BodyKey))
            {
                return(Convert.FromBase64String(transportMessage.Body));
            }

            var getObjectRequest = new GetObjectRequest
            {
                BucketName = transportConfiguration.S3BucketForLargeMessages,
                Key        = transportMessage.S3BodyKey
            };

            if (transportConfiguration.ServerSideEncryptionCustomerMethod != null)
            {
                getObjectRequest.ServerSideEncryptionCustomerMethod      = transportConfiguration.ServerSideEncryptionCustomerMethod;
                getObjectRequest.ServerSideEncryptionCustomerProvidedKey = transportConfiguration.ServerSideEncryptionCustomerProvidedKey;

                if (!string.IsNullOrEmpty(transportConfiguration.ServerSideEncryptionCustomerProvidedKeyMD5))
                {
                    getObjectRequest.ServerSideEncryptionCustomerProvidedKeyMD5 = transportConfiguration.ServerSideEncryptionCustomerProvidedKeyMD5;
                }
            }

            var s3GetResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken)
                                .ConfigureAwait(false);

            using (var memoryStream = new MemoryStream())
            {
                await s3GetResponse.ResponseStream.CopyToAsync(memoryStream).ConfigureAwait(false);

                return(memoryStream.ToArray());
            }
        }
        public static async Task <IncomingMessage> ToIncomingMessage(this TransportMessage transportMessage,
                                                                     IAmazonS3 amazonS3,
                                                                     TransportConfiguration transportConfiguration,
                                                                     CancellationToken cancellationToken)
        {
            var messageId = transportMessage.Headers[Headers.MessageId];

            byte[] body;

            if (string.IsNullOrEmpty(transportMessage.S3BodyKey))
            {
                body = Convert.FromBase64String(transportMessage.Body);
            }
            else
            {
                var s3GetResponse = await amazonS3.GetObjectAsync(transportConfiguration.S3BucketForLargeMessages,
                                                                  transportMessage.S3BodyKey,
                                                                  cancellationToken).ConfigureAwait(false);

                body = new byte[s3GetResponse.ResponseStream.Length];
                using (var bufferedStream = new BufferedStream(s3GetResponse.ResponseStream))
                {
                    int       count;
                    var       transferred  = 0;
                    const int maxChunkSize = 8 * 1024;
                    var       bytesToRead  = Math.Min(maxChunkSize, body.Length - transferred);
                    while ((count = await bufferedStream.ReadAsync(body, transferred, bytesToRead, cancellationToken).ConfigureAwait(false)) > 0)
                    {
                        transferred += count;
                        bytesToRead  = Math.Min(maxChunkSize, body.Length - transferred);
                    }
                }
            }

            return(new IncomingMessage(messageId, transportMessage.Headers, body));
        }