Inheritance: Amazon.Runtime.AmazonServiceException
Ejemplo n.º 1
0
        public void SetUp()
        {
            var file1 = new Mock<IFileInfoWrap>();
            file1.Setup(x => x.Name).Returns("File1.jpg");
            file1.Setup(x => x.FullName).Returns("c:\\files\\File1.jpg");

            var file2 = new Mock<IFileInfoWrap>();
            file2.Setup(x => x.Name).Returns("File2.jpg");
            file2.Setup(x => x.FullName).Returns("c:\\files\\File2.jpg");

            var file3 = new Mock<IFileInfoWrap>();
            file3.Setup(x => x.Name).Returns("File3.jpg");
            file3.Setup(x => x.FullName).Returns("c:\\files\\morefiles\\File3.jpg");

            var file4 = new Mock<IFileInfoWrap>();
            file4.Setup(x => x.Name).Returns("File4.jpg");
            file4.Setup(x => x.FullName).Returns("c:\\files\\morefiles\\File4.jpg");

            var file5 = new Mock<IFileInfoWrap>();
            file5.Setup(x => x.Name).Returns("File5.jpg");
            file5.Setup(x => x.FullName).Returns("c:\\files\\otherfiles\\File5.jpg");

            var moreFilesDirectory = new Mock<IDirectoryInfoWrap>();
            moreFilesDirectory.Setup(x => x.GetFiles()).Returns(() => new IFileInfoWrap[] { file3.Object, file4.Object });
            moreFilesDirectory.Setup(x => x.GetDirectories()).Returns(() => new IDirectoryInfoWrap[] { });
            moreFilesDirectory.Setup(x => x.Exists).Returns(() => true);
            moreFilesDirectory.Setup(x => x.Name).Returns(() => "morefiles");
            moreFilesDirectory.Setup(x => x.FullName).Returns(() => "c:\\files\\morefiles");

            var otherfilesDirectory = new Mock<IDirectoryInfoWrap>();
            otherfilesDirectory.Setup(x => x.GetFiles()).Returns(() => new IFileInfoWrap[] { file5.Object });
            otherfilesDirectory.Setup(x => x.GetDirectories()).Returns(() => new IDirectoryInfoWrap[] { });
            otherfilesDirectory.Setup(x => x.Exists).Returns(() => true);
            otherfilesDirectory.Setup(x => x.Name).Returns(() => "otherfiles");
            otherfilesDirectory.Setup(x => x.FullName).Returns(() => "c:\\files\\otherfiles");

            DirectoryInfo = new Mock<IDirectoryInfoWrap>();
            DirectoryInfo.Setup(x => x.GetFiles()).Returns(() => new IFileInfoWrap[] {file1.Object, file2.Object});
            DirectoryInfo.Setup(x => x.GetDirectories()).Returns(() => new IDirectoryInfoWrap[] { moreFilesDirectory.Object, otherfilesDirectory.Object });
            DirectoryInfo.Setup(x => x.Exists).Returns(() => true);
            DirectoryInfo.Setup(x => x.Name).Returns(() => "files");
            DirectoryInfo.Setup(x => x.FullName).Returns(() => "c:\\files");

            var notFoundException = new AmazonS3Exception("Key not found", HttpStatusCode.NotFound);

            AmazonS3 = new Mock<AmazonS3>();
            AmazonS3.Setup(x => x.PutObject(It.IsAny<PutObjectRequest>())).Returns(new PutObjectResponse()).Verifiable();
            AmazonS3.Setup(x => x.GetObjectMetadata(It.IsAny<GetObjectMetadataRequest>())).Throws(notFoundException);

            S3FileSystem = new S3FileSystem(AmazonS3.Object, GetFileStreamFromFile);
        }
Ejemplo n.º 2
0
        public AmazonS3Exception(string message, Exception innerException) : base(message, innerException)
        {
            this.message = message;
            AmazonS3Exception exception = innerException as AmazonS3Exception;

            if (exception != null)
            {
                this.statusCode = exception.StatusCode;
                this.errorCode  = exception.ErrorCode;
                this.requestId  = exception.RequestId;
                this.message    = exception.Message;
                this.hostId     = exception.hostId;
                this.xml        = exception.XML;
            }
        }
Ejemplo n.º 3
0
 /// <summary>Format and display an exception</summary>
 /// <param name="ex">Exception to display</param>
 private void ShowError(AmazonS3Exception ex)
 {
     if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity"))) {
         Project.Log(Level.Error, "Please check the provided AWS Credentials.");
         Project.Log(Level.Error, "If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
     } else {
         Project.Log(Level.Error, "An Error, number {0}, occurred with the message '{1}'",
             ex.ErrorCode, ex.Message);
     }
 }
Ejemplo n.º 4
0
 /// <summary>Format and display an exception</summary>
 /// <param name="ex">Exception to display</param>
 public void ShowError(AmazonS3Exception ex)
 {
     if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity")))
     {
         Project.Log(Level.Error, "Please check the provided AWS Credentials.");
     }
     else
     {
         Project.Log(Level.Error, "An Error, number {0}, occurred with the message '{1}'",
             ex.ErrorCode, ex.Message);
     }
 }
Ejemplo n.º 5
0
        private static Exception WrapException(AmazonS3Exception ex)
        {
            if (IsSecurityException(ex.ErrorCode)) {
            return new BlobSecurityException(ex);
              }

              return ex;
        }
        public bool Exists(string key)
        {
            try
            {
                //lock (_lockconnections)
                //{
                var res = client.GetObjectMetadataAsync(this.Bucket, fixKey(key)).Result;
                Amazon.S3.Model.MetadataCollection meta = res?.Metadata;
                //.StatObjectAsync(this.Bucket, fixKey(key)).Result;
                //}
                if (meta.Keys?.Contains(_expiresAtMetaTag) == true)  // compatibility with minio client
                {
                    var expiration = Convert.ToInt64(meta[_expiresAtMetaTag]);
                    if (expiration == 0)
                    {
                        return(true);
                    }
                    DateTime expires = new DateTime(expiration);
                    if (expires < DateTime.UtcNow)
                    {
                        Remove(key);
                        return(false);
                    }
                    return(true);
                }
                if (meta.Keys?.Contains(_xAmzPrefix + _expiresAtMetaTag.ToLower()) == true) // compatibility with s3 client
                {
                    var expiration = Convert.ToInt64(meta[_xAmzPrefix + _expiresAtMetaTag.ToLower()]);
                    if (expiration == 0)
                    {
                        return(true);
                    }
                    DateTime expires = new DateTime(expiration);
                    if (expires < DateTime.UtcNow)
                    {
                        Remove(key);
                        return(false);
                    }
                    return(true);
                }

                Remove(key);
                return(false);
            }
            catch (Amazon.S3.AmazonS3Exception)
            {
                return(false);
            }
            catch (Amazon.Runtime.AmazonClientException me)
            {
                ESTraceLogger.Error($"AWS_S3CacheProvider: exists record {fixKey(key)} error {me.Message}", me);
                return(false);
            }
            catch (AggregateException agge)
            {
                //if (agge.InnerExceptions?.Any(m => m.GetType() == typeof(Minio.Exceptions.ObjectNotFoundException)) == true)
                //    return false;
                if (agge != null)
                {
                    if (agge.InnerExceptions?.Count > 0)
                    {
                        foreach (var ex in agge.InnerExceptions)
                        {
                            if (ex.GetType() == typeof(Amazon.S3.AmazonS3Exception))
                            {
                                Amazon.S3.AmazonS3Exception aex = (Amazon.S3.AmazonS3Exception)ex;
                                if (aex.StatusCode == System.Net.HttpStatusCode.NotFound)
                                {
                                    return(false);
                                }
                            }
                            ESTraceLogger.Error($"AWS_S3CacheProvider: exists record {fixKey(key)} error {ex.Message}", ex);
                        }
                    }
                    else if (agge.InnerException != null)
                    {
                        ESTraceLogger.Error($"AWS_S3CacheProvider: exists record {fixKey(key)} error {agge.InnerException.Message}", agge.InnerException);
                    }
                    else
                    {
                        ESTraceLogger.Error($"AWS_S3CacheProvider: exists record {fixKey(key)} error {agge.Message}", agge);
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                ESTraceLogger.Error($"AWS_S3CacheProvider: exists record {fixKey(key)} error {e.Message}", e);
                return(false);
            }
        }
 private bool IsMissingObjectException(AmazonS3Exception s3x)
 {
     return s3x.ErrorCode.Equals("NoSuchBucket", StringComparison.InvariantCultureIgnoreCase) ||
            s3x.ErrorCode.Equals("NoSuchKey", StringComparison.InvariantCultureIgnoreCase);
 }
Ejemplo n.º 8
0
        private string GetMessageFromException(AmazonS3Exception e)
        {
            if (!string.IsNullOrEmpty(e.ErrorCode) &&
                (e.ErrorCode == InvalidAccessKeyErrorCode ||
                 e.ErrorCode == InvalidSecurityErrorCode))
                return "S3 error: check your access key and secret access key";

            return string.Format("S3 error: code [{0}], message [{1}]", e.ErrorCode, e.Message);
        }
Ejemplo n.º 9
0
        private StorageException AnAwsRelatedException(AmazonS3Exception ex)
        {
            if ((ex.ErrorCode != null) &&
                ((ex.ErrorCode.Equals("InvalidAccessKeyId")) || (ex.ErrorCode.Equals("InvalidSecurity"))))
                return new StorageException("The provided AWS credentials are invalid.", ex);

            return new StorageException("An AWS service related error occurred while processing your request.", ex);
        }
Ejemplo n.º 10
0
        void S3ErrorResponse(AmazonS3Exception error)
        {
            StringBuilder errorBuilder = new StringBuilder();
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "ERROR CODE: {0}", error.ErrorCode));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "HOST ID: {0}", error.HostId));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "INNER EXCEPTION: {0}", error.InnerException));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "MESSAGE: {0}", error.Message));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "REQUEST ID: {0}", error.RequestId));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "STATUS CODE: {0}", error.StatusCode));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "REQUEST ADDRESS: {0}", error.S3RequestAddress));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "\nERROR RESPONSE:\n {0}", error.XML));

            this.Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(errorBuilder.ToString(), "Error Occured", MessageBoxButton.OK);
            });
        }