Пример #1
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);
        }
Пример #2
0
 public void Dispose()
 {
     if (m_options != null)
         m_options = null;
     if (m_wrapper != null)
     {
         m_wrapper.Dispose();
         m_wrapper = null;
     }
 }