Beispiel #1
0
 public void Dispose()
 {
     if (m_options != null)
         m_options = null;
     if (m_wrapper != null)
     {
         m_wrapper.Dispose();
         m_wrapper = null;
     }
 }
Beispiel #2
0
        public S3(string url, Dictionary <string, string> options)
        {
            var uri = new Utility.Uri(url);

            uri.RequireHost();

            string host = uri.Host;

            m_prefix = uri.Path;

            string awsID  = null;
            string awsKey = null;

            if (options.ContainsKey("auth-username"))
            {
                awsID = options["auth-username"];
            }
            if (options.ContainsKey("auth-password"))
            {
                awsKey = options["auth-password"];
            }

            if (options.ContainsKey("aws_access_key_id"))
            {
                awsID = options["aws_access_key_id"];
            }
            if (options.ContainsKey("aws_secret_access_key"))
            {
                awsKey = options["aws_secret_access_key"];
            }
            if (!string.IsNullOrEmpty(uri.Username))
            {
                awsID = uri.Username;
            }
            if (!string.IsNullOrEmpty(uri.Password))
            {
                awsKey = uri.Password;
            }

            if (string.IsNullOrEmpty(awsID))
            {
                throw new UserInformationException(Strings.S3Backend.NoAMZUserIDError);
            }
            if (string.IsNullOrEmpty(awsKey))
            {
                throw new UserInformationException(Strings.S3Backend.NoAMZKeyError);
            }

            bool euBuckets = Utility.Utility.ParseBoolOption(options, EU_BUCKETS_OPTION);
            bool useRRS    = Utility.Utility.ParseBoolOption(options, RRS_OPTION);
            bool useSSL    = Utility.Utility.ParseBoolOption(options, SSL_OPTION);

            string locationConstraint;

            options.TryGetValue(LOCATION_OPTION, out locationConstraint);

            if (!string.IsNullOrEmpty(locationConstraint) && euBuckets)
            {
                throw new UserInformationException(Strings.S3Backend.OptionsAreMutuallyExclusiveError(LOCATION_OPTION, EU_BUCKETS_OPTION));
            }

            if (euBuckets)
            {
                locationConstraint = S3_EU_REGION_NAME;
            }

            string storageClass;

            options.TryGetValue(STORAGECLASS_OPTION, out storageClass);
            if (string.IsNullOrWhiteSpace(storageClass) && useRRS)
            {
                storageClass = S3_RRS_CLASS_NAME;
            }

            string s3host;

            options.TryGetValue(SERVER_NAME, out s3host);
            if (string.IsNullOrEmpty(s3host))
            {
                s3host = DEFAULT_S3_HOST;

                //Change in S3, now requires that you use location specific endpoint
                if (!string.IsNullOrEmpty(locationConstraint))
                {
                    foreach (KeyValuePair <string, string> kvp in DEFAULT_S3_LOCATION_BASED_HOSTS)
                    {
                        if (kvp.Key.Equals(locationConstraint, StringComparison.InvariantCultureIgnoreCase))
                        {
                            s3host = kvp.Value;
                            break;
                        }
                    }
                }
            }

            //Fallback to previous formats
            if (host.Contains(DEFAULT_S3_HOST))
            {
                Uri u = new Uri(url);
                host     = u.Host;
                m_prefix = "";

                if (host.ToLower() == s3host)
                {
                    m_bucket = Library.Utility.Uri.UrlDecode(u.PathAndQuery);

                    if (m_bucket.StartsWith("/"))
                    {
                        m_bucket = m_bucket.Substring(1);
                    }

                    if (m_bucket.Contains("/"))
                    {
                        m_prefix = m_bucket.Substring(m_bucket.IndexOf("/") + 1);
                        m_bucket = m_bucket.Substring(0, m_bucket.IndexOf("/"));
                    }
                }
                else
                {
                    //Subdomain type lookup
                    if (host.ToLower().EndsWith("." + s3host))
                    {
                        m_bucket = host.Substring(0, host.Length - ("." + s3host).Length);
                        host     = s3host;
                        m_prefix = Library.Utility.Uri.UrlDecode(u.PathAndQuery);

                        if (m_prefix.StartsWith("/"))
                        {
                            m_prefix = m_prefix.Substring(1);
                        }
                    }
                    else
                    {
                        throw new UserInformationException(Strings.S3Backend.UnableToDecodeBucketnameError(url));
                    }
                }

                try { Console.Error.WriteLine(Strings.S3Backend.DeprecatedUrlFormat("s3://" + m_bucket + "/" + m_prefix)); }
                catch { }
            }
            else
            {
                //The new simplified url style s3://bucket/prefix
                m_bucket = host;
                host     = s3host;
            }

            m_options = options;
            m_prefix  = m_prefix.Trim();
            if (m_prefix.Length != 0 && !m_prefix.EndsWith("/"))
            {
                m_prefix += "/";
            }

            m_wrapper = new S3Wrapper(awsID, awsKey, locationConstraint, host, storageClass, useSSL, options);
        }
Beispiel #3
0
        public S3(string url, Dictionary <string, string> options)
        {
            var uri = new Utility.Uri(url);

            uri.RequireHost();

            string host = uri.Host;

            m_prefix = uri.Path;

            string awsID  = null;
            string awsKey = null;

            if (options.ContainsKey("auth-username"))
            {
                awsID = options["auth-username"];
            }
            if (options.ContainsKey("auth-password"))
            {
                awsKey = options["auth-password"];
            }

            if (options.ContainsKey("aws_access_key_id"))
            {
                awsID = options["aws_access_key_id"];
            }
            if (options.ContainsKey("aws_secret_access_key"))
            {
                awsKey = options["aws_secret_access_key"];
            }
            if (!string.IsNullOrEmpty(uri.Username))
            {
                awsID = uri.Username;
            }
            if (!string.IsNullOrEmpty(uri.Password))
            {
                awsKey = uri.Password;
            }

            if (string.IsNullOrEmpty(awsID))
            {
                throw new UserInformationException(Strings.S3Backend.NoAMZUserIDError, "S3NoAmzUserID");
            }
            if (string.IsNullOrEmpty(awsKey))
            {
                throw new UserInformationException(Strings.S3Backend.NoAMZKeyError, "S3NoAmzKey");
            }

            bool euBuckets = Utility.Utility.ParseBoolOption(options, EU_BUCKETS_OPTION);
            bool useRRS    = Utility.Utility.ParseBoolOption(options, RRS_OPTION);
            bool useSSL    = Utility.Utility.ParseBoolOption(options, SSL_OPTION);

            string locationConstraint;

            options.TryGetValue(LOCATION_OPTION, out locationConstraint);

            if (!string.IsNullOrEmpty(locationConstraint) && euBuckets)
            {
                throw new UserInformationException(Strings.S3Backend.OptionsAreMutuallyExclusiveError(LOCATION_OPTION, EU_BUCKETS_OPTION), "S3CannotMixLocationAndEuOptions");
            }

            if (euBuckets)
            {
                locationConstraint = S3_EU_REGION_NAME;
            }

            string storageClass;

            options.TryGetValue(STORAGECLASS_OPTION, out storageClass);
            if (string.IsNullOrWhiteSpace(storageClass) && useRRS)
            {
                storageClass = S3_RRS_CLASS_NAME;
            }

            string s3host;

            options.TryGetValue(SERVER_NAME, out s3host);
            if (string.IsNullOrEmpty(s3host))
            {
                s3host = DEFAULT_S3_HOST;

                //Change in S3, now requires that you use location specific endpoint
                if (!string.IsNullOrEmpty(locationConstraint))
                {
                    foreach (KeyValuePair <string, string> kvp in DEFAULT_S3_LOCATION_BASED_HOSTS)
                    {
                        if (kvp.Key.Equals(locationConstraint, StringComparison.OrdinalIgnoreCase))
                        {
                            s3host = kvp.Value;
                            break;
                        }
                    }
                }
            }

            //Fallback to previous formats
            if (host.Contains(DEFAULT_S3_HOST))
            {
                Uri u = new Uri(url);
                host     = u.Host;
                m_prefix = "";

                if (String.Equals(host, s3host, StringComparison.OrdinalIgnoreCase))
                {
                    m_bucket = Utility.Uri.UrlDecode(u.PathAndQuery);

                    if (m_bucket.StartsWith("/", StringComparison.Ordinal))
                    {
                        m_bucket = m_bucket.Substring(1);
                    }

                    if (m_bucket.Contains("/"))
                    {
                        m_prefix = m_bucket.Substring(m_bucket.IndexOf("/", StringComparison.Ordinal) + 1);
                        m_bucket = m_bucket.Substring(0, m_bucket.IndexOf("/", StringComparison.Ordinal));
                    }
                }
                else
                {
                    //Subdomain type lookup
                    if (host.EndsWith("." + s3host, StringComparison.OrdinalIgnoreCase))
                    {
                        m_bucket = host.Substring(0, host.Length - ("." + s3host).Length);
                        host     = s3host;
                        m_prefix = Utility.Uri.UrlDecode(u.PathAndQuery);

                        if (m_prefix.StartsWith("/", StringComparison.Ordinal))
                        {
                            m_prefix = m_prefix.Substring(1);
                        }
                    }
                    else
                    {
                        throw new UserInformationException(Strings.S3Backend.UnableToDecodeBucketnameError(url), "S3CannotDecodeBucketName");
                    }
                }

                Logging.Log.WriteWarningMessage(LOGTAG, "DeprecatedS3Format", null, Strings.S3Backend.DeprecatedUrlFormat("s3://" + m_bucket + "/" + m_prefix));
            }
            else
            {
                //The new simplified url style s3://bucket/prefix
                m_bucket = host;
                host     = s3host;
            }

            m_options = options;
            m_prefix  = m_prefix.Trim();
            if (m_prefix.Length != 0)
            {
                m_prefix = Util.AppendDirSeparator(m_prefix, "/");
            }

            // Auto-disable dns lookup for non AWS configurations
            var hasForcePathStyle = options.ContainsKey("s3-ext-forcepathstyle");

            if (!hasForcePathStyle && !DEFAULT_S3_LOCATION_BASED_HOSTS.Any(x => string.Equals(x.Value, host, StringComparison.OrdinalIgnoreCase)) && !string.Equals(host, "s3.amazonaws.com", StringComparison.OrdinalIgnoreCase))
            {
                options["s3-ext-forcepathstyle"] = "true";
            }

            m_wrapper = new S3Wrapper(awsID, awsKey, locationConstraint, host, storageClass, useSSL, options);
        }
Beispiel #4
0
        public S3(string url, Dictionary<string, string> options)
        {
            var uri = new Utility.Uri(url);
            uri.RequireHost();

            string host = uri.Host;
            m_prefix = uri.Path;

            string awsID = null;
            string awsKey = null;

            if (options.ContainsKey("auth-username"))
                awsID = options["auth-username"];
            if (options.ContainsKey("auth-password"))
                awsKey = options["auth-password"];

            if (options.ContainsKey("aws_access_key_id"))
                awsID = options["aws_access_key_id"];
            if (options.ContainsKey("aws_secret_access_key"))
                awsKey = options["aws_secret_access_key"];
            if (!string.IsNullOrEmpty(uri.Username))
                awsID = uri.Username;
            if (!string.IsNullOrEmpty(uri.Password))
                awsKey = uri.Password;

            if (string.IsNullOrEmpty(awsID))
                throw new Exception(Strings.S3Backend.NoAMZUserIDError);
            if (string.IsNullOrEmpty(awsKey))
                throw new Exception(Strings.S3Backend.NoAMZKeyError);

            bool euBuckets = Utility.Utility.ParseBoolOption(options, EU_BUCKETS_OPTION);
            bool useRRS = Utility.Utility.ParseBoolOption(options, RRS_OPTION);
            bool useSSL = Utility.Utility.ParseBoolOption(options, SSL_OPTION);

            string locationConstraint;
            options.TryGetValue(LOCATION_OPTION, out locationConstraint);

            if (!string.IsNullOrEmpty(locationConstraint) && euBuckets)
                throw new Exception(string.Format(Strings.S3Backend.OptionsAreMutuallyExclusiveError, LOCATION_OPTION, EU_BUCKETS_OPTION));

            if (euBuckets)
                locationConstraint = S3_EU_REGION_NAME;

            string s3host;
            options.TryGetValue(SERVER_NAME, out s3host);
            if (string.IsNullOrEmpty(s3host))
            {
                s3host = DEFAULT_S3_HOST;

                //Change in S3, now requires that you use location specific endpoint
                if (!string.IsNullOrEmpty(locationConstraint))
                    foreach(KeyValuePair<string, string> kvp in DEFAULT_S3_LOCATION_BASED_HOSTS)
                        if (kvp.Key.Equals(locationConstraint, StringComparison.InvariantCultureIgnoreCase))
                        {
                            s3host = kvp.Value;
                            break;
                        }
            }

            //Fallback to previous formats
            if (host.Contains(DEFAULT_S3_HOST))
            {
                Uri u = new Uri(url);
                host = u.Host;
                m_prefix = "";

                if (host.ToLower() == s3host)
                {
                    m_bucket = System.Web.HttpUtility.UrlDecode(u.PathAndQuery);

                    if (m_bucket.StartsWith("/"))
                        m_bucket = m_bucket.Substring(1);

                    if (m_bucket.Contains("/"))
                    {
                        m_prefix = m_bucket.Substring(m_bucket.IndexOf("/") + 1);
                        m_bucket = m_bucket.Substring(0, m_bucket.IndexOf("/"));
                    }
                }
                else
                {
                    //Subdomain type lookup
                    if (host.ToLower().EndsWith("." + s3host))
                    {
                        m_bucket = host.Substring(0, host.Length - ("." + s3host).Length);
                        host = s3host;
                        m_prefix = System.Web.HttpUtility.UrlDecode(u.PathAndQuery);

                        if (m_prefix.StartsWith("/"))
                            m_prefix = m_prefix.Substring(1);
                    }
                    else
                        throw new Exception(string.Format(Strings.S3Backend.UnableToDecodeBucketnameError, url));
                }

                try { Console.Error.WriteLine(string.Format(Strings.S3Backend.DeprecatedUrlFormat, "s3://" + m_bucket + "/" + m_prefix)); }
                catch { }
            }
            else
            {
                //The new simplified url style s3://bucket/prefix
                m_bucket = host;
                host = s3host;
            }

            m_options = options;
            m_prefix = m_prefix.Trim();
            if (m_prefix.Length != 0 && !m_prefix.EndsWith("/"))
                m_prefix += "/";

            m_wrapper = new S3Wrapper(awsID, awsKey, locationConstraint, host, useRRS, useSSL);
        }
Beispiel #5
0
 public void Dispose()
 {
     if (m_options != null)
         m_options = null;
     if (m_wrapper != null)
     {
         m_wrapper.Dispose();
         m_wrapper = null;
     }
 }
Beispiel #6
0
        public S3(string url, Dictionary <string, string> options)
        {
            // make sure there is a host even though it's bogus,
            // so we can make sure the path is retrieved properly
            var uri = new Utility.Uri(url.Replace("s3://", "s3://c.c"));
            //uri.RequireHost();

            //string host = uri.Host;

            string awsID  = null;
            string awsKey = null;

            if (options.ContainsKey("auth-username"))
            {
                awsID = options["auth-username"];
            }
            if (options.ContainsKey("auth-password"))
            {
                awsKey = options["auth-password"];
            }

            if (options.ContainsKey("aws_access_key_id"))
            {
                awsID = options["aws_access_key_id"];
            }
            if (options.ContainsKey("aws_secret_access_key"))
            {
                awsKey = options["aws_secret_access_key"];
            }
            if (!string.IsNullOrEmpty(uri.Username))
            {
                awsID = uri.Username;
            }
            if (!string.IsNullOrEmpty(uri.Password))
            {
                awsKey = uri.Password;
            }

            if (string.IsNullOrEmpty(awsID))
            {
                throw new Exception(Strings.S3Backend.NoAMZUserIDError);
            }
            if (string.IsNullOrEmpty(awsKey))
            {
                throw new Exception(Strings.S3Backend.NoAMZKeyError);
            }

            string locationConstraint;

            options.TryGetValue(LOCATION_OPTION, out locationConstraint);
            string emailUsername;

            options.TryGetValue(EMAIL_OPTION, out emailUsername);

            // The new simplified url style is s3://bucket/prefix
            //m_bucket = host;
            //host = s3host;
            m_bucket  = "cbd-backup-" + locationConstraint; // use account id as part of bucket name
            m_prefix  = emailUsername + "/v1/";
            m_options = options;

            //m_prefix = m_prefix.Trim();
            //if (m_prefix.Length != 0 && !m_prefix.EndsWith("/"))
            //    m_prefix += "/";

            m_wrapper = new S3Wrapper(awsID, awsKey, locationConstraint);
        }
Beispiel #7
0
        public S3(string url, Dictionary<string, string> options)
        {
            //We need to do custom parsing because we allow non-valid urls
            System.Text.RegularExpressions.Match m = URL_PARSING.Match(url);
            if (!m.Success)
                throw new Exception(string.Format(Strings.S3Backend.UnableToParseURLError, url));

            string host = m.Groups["hostname"].Value;
            m_prefix = m.Groups["prefix"].Value;

            string awsID = null;
            string awsKey = null;

            if (options.ContainsKey("ftp-username"))
                awsID = options["ftp-username"];
            if (options.ContainsKey("ftp-password"))
                awsKey = options["ftp-password"];

            if (options.ContainsKey("aws_access_key_id"))
                awsID = options["aws_access_key_id"];
            if (options.ContainsKey("aws_secret_access_key"))
                awsKey = options["aws_secret_access_key"];

            string s3host;
            options.TryGetValue(SERVER_NAME, out s3host);
            if (string.IsNullOrEmpty(s3host))
                s3host = DEFAULT_S3_HOST;

            bool euBuckets = Utility.Utility.ParseBoolOption(options, EU_BUCKETS_OPTION);
            bool useRRS = Utility.Utility.ParseBoolOption(options, RRS_OPTION);
            bool useSSL = Utility.Utility.ParseBoolOption(options, SSL_OPTION);

            string locationConstraint;
            options.TryGetValue(LOCATION_OPTION, out locationConstraint);

            if (!string.IsNullOrEmpty(locationConstraint) && euBuckets)
                throw new Exception(string.Format(Strings.S3Backend.OptionsAreMutuallyExclusiveError, LOCATION_OPTION, EU_BUCKETS_OPTION));

            if (euBuckets)
                locationConstraint = S3_EU_REGION_NAME;

            //Fallback to previous formats
            if (host.Contains(DEFAULT_S3_HOST))
            {
                Uri u = new Uri(url);
                host = u.Host;
                m_prefix = "";

                if (host.ToLower() == s3host)
                {
                    m_bucket = System.Web.HttpUtility.UrlDecode(u.PathAndQuery);

                    if (m_bucket.StartsWith("/"))
                        m_bucket = m_bucket.Substring(1);

                    if (m_bucket.Contains("/"))
                    {
                        m_prefix = m_bucket.Substring(m_bucket.IndexOf("/") + 1);
                        m_bucket = m_bucket.Substring(0, m_bucket.IndexOf("/"));
                    }
                }
                else
                {
                    //Subdomain type lookup
                    if (host.ToLower().EndsWith("." + s3host))
                    {
                        m_bucket = host.Substring(0, host.Length - ("." + s3host).Length);
                        host = s3host;
                        m_prefix = System.Web.HttpUtility.UrlDecode(u.PathAndQuery);

                        if (m_prefix.StartsWith("/"))
                            m_prefix = m_prefix.Substring(1);
                    }
                    else
                        throw new Exception(string.Format(Strings.S3Backend.UnableToDecodeBucketnameError, url));
                }

                try { Console.Error.WriteLine(string.Format(Strings.S3Backend.DeprecatedUrlFormat, "s3://" + m_bucket + "/" + m_prefix)); }
                catch { }
            }
            else
            {
                //The new simplified url style s3://bucket/prefix
                m_bucket = host;
                host = s3host;
            }

            if (string.IsNullOrEmpty(awsID))
                throw new Exception(Strings.S3Backend.NoAMZUserIDError);
            if (string.IsNullOrEmpty(awsKey))
                throw new Exception(Strings.S3Backend.NoAMZKeyError);

            m_options = options;
            m_prefix = m_prefix.Trim();
            if (m_prefix.Length != 0 && !m_prefix.EndsWith("/"))
                m_prefix += "/";

            m_wrapper = new S3Wrapper(awsID, awsKey, locationConstraint, host, useRRS, useSSL);
        }