public void testConfig()
        {
            String config =
                "access_key: 'testkey'\n"
                + "access_secret: 'test_asss'\n"
                + "host: qingcloud.com\n"
                + "port: 443\n"
                + "protocol: https\n";
            FileStream f     = new FileStream(System.Environment.CurrentDirectory + "/tmp/key.csv", FileMode.Open);
            Boolean    bConf = false;

            try
            {
                StreamReader output = new StreamReader(f);
                output.ReadToEnd();
                output.Close();
                f.Close();
                bConf = true;
            }
            catch (Exception e)
            {
                System.Console.Write(e.Message);
            }

            if (bConf)
            {
                EvnContext evnContext = EvnContext.loadFromFile(System.Environment.CurrentDirectory + "/tmp/key.csv");
                Assert.AreEqual(evnContext.getAccessKey(), "testkey");
                Assert.AreEqual(evnContext.getAccessSecret(), "testaccess");
                Assert.AreEqual(evnContext.getRequestUrl(), "https://qingcloud.com:443");
            }
        }
        public void testDefault()
        {
            EvnContext evnContext = new EvnContext("testkey", "test_asss");

            Assert.AreEqual(evnContext.getAccessKey(), "testkey");
            Assert.AreEqual(evnContext.getAccessSecret(), "test_asss");
            Assert.AreEqual(evnContext.getRequestUrl(), "https://qingstor.com");
            Assert.AreEqual(evnContext.getLog_level(), QSConstant.LOGGER_ERROR);
        }
示例#3
0
        public static string getObjectAuthRequestUrl(
            EvnContext evnContext,
            String zone,
            String bucketName,
            String objectName,
            int expiresSecond)
        {
            Dictionary <object, object> context = new Dictionary <object, object>();

            try
            {
                objectName = percentEncode(objectName);
                context.Add(QSConstant.PARAM_KEY_REQUEST_ZONE, zone);
                context.Add(QSConstant.EVN_CONTEXT_KEY, evnContext);
                context.Add("OperationName", "GetObject");
                context.Add("APIName", "GetObject");
                context.Add("ServiceName", "QingStor");
                context.Add("RequestMethod", "GET");
                context.Add("RequestURI", "/<bucket-name>/<object-key>");
                context.Add("bucketNameInput", bucketName);
                context.Add("objectNameInput", objectName);
                DateTime fi             = Convert.ToDateTime("1970-1-1");
                int      expiresTime    = (int)((DateTime.Now - fi).Seconds + expiresSecond);
                string   expireAuth     = getExpireAuth(context, expiresTime, new RequestInputModel());
                string   serviceUrl     = evnContext.getRequestUrl();
                string   storRequestUrl = serviceUrl.Replace("://", "://" + zone + ".");
                if (objectName != null && objectName.IndexOf("?") > 0)
                {
                    return(storRequestUrl + "/" + bucketName + "/" + objectName +
                           "&access_key_id=" + evnContext.getAccessKey() + "&expires=" + expiresTime.ToString() + "&signature=" + expireAuth);
                }
                else
                {
                    return(storRequestUrl + "/" + bucketName + "/" + objectName +
                           "?&access_key_id=" + evnContext.getAccessKey() + "&expires=" + expiresTime.ToString() + "&signature=" + expireAuth);
                }
            }
            catch (Exception e)
            {
                throw new QSException(e.Message);
            }
        }
        private HttpWebRequest buildRequest(Dictionary <object, object> context, RequestInputModel Params)
        {
            EvnContext evnContext = (EvnContext)context[QSConstant.EVN_CONTEXT_KEY];
            string     zone       = (string)context[QSConstant.PARAM_KEY_REQUEST_ZONE];

            Dictionary <object, object> paramsQuery   = QSParamInvokeUtil.getRequestParams(Params, QSConstant.PARAM_TYPE_QUERY);
            Dictionary <object, object> paramsBody    = QSParamInvokeUtil.getRequestParams(Params, QSConstant.PARAM_TYPE_BODY);
            Dictionary <object, object> paramsHeaders = QSParamInvokeUtil.getRequestParams(Params, QSConstant.PARAM_TYPE_HEADER);

            if (context.ContainsKey(QSConstant.PARAM_KEY_USER_AGENT))
            {
                paramsHeaders.Add(QSConstant.PARAM_KEY_USER_AGENT, context[QSConstant.PARAM_KEY_USER_AGENT]);
            }

            String requestApi = (String)context[QSConstant.PARAM_KEY_REQUEST_APINAME];

            //string FileBody = GetBodyFile(context[QSConstant.PARAM_KEY_OBJECT_NAME].ToString()).ToString();
            paramsHeaders = initHeadContentMd5(requestApi, paramsBody, paramsHeaders);

            string method      = (string)context[QSConstant.PARAM_KEY_REQUEST_METHOD];
            string bucketName  = (string)context[QSConstant.PARAM_KEY_BUCKET_NAME];
            string requestPath = (string)context[QSConstant.PARAM_KEY_REQUEST_PATH];

            string objectName = "";

            if (context.ContainsKey(QSConstant.PARAM_KEY_OBJECT_NAME))
            {
                objectName = context[QSConstant.PARAM_KEY_OBJECT_NAME].ToString();
            }
            if (!paramsHeaders.ContainsKey(QSConstant.HEADER_PARAM_KEY_CONTENTTYPE))
            {
                paramsHeaders.Add(QSConstant.HEADER_PARAM_KEY_CONTENTTYPE, MimeMapping.GetMimeMapping(objectName));
            }

            if (context.ContainsKey(QSConstant.PARAM_KEY_OBJECT_NAME))
            {
                requestPath = requestPath.Replace(QSConstant.BUCKET_NAME_REPLACE, bucketName);
                requestPath = requestPath.Replace(QSConstant.OBJECT_NAME_REPLACE, QSStringUtil.urlCharactersEncoding(objectName));
            }
            else
            {
                requestPath = requestPath.Replace(QSConstant.BUCKET_NAME_REPLACE, bucketName + "/");
            }

            string authSign =
                QSSignatureUtil.getAuth(
                    evnContext.getAccessKey(),
                    evnContext.getAccessSecret(),
                    method,
                    requestPath,
                    paramsQuery,
                    paramsHeaders);
            string requestSuffixPath = getRequestSuffixPath((string)context[QSConstant.PARAM_KEY_REQUEST_PATH], bucketName, objectName);

            paramsHeaders.Add("Authorization", authSign);
            logger.Info(authSign);
            string singedUrl =
                getSignedUrl(
                    evnContext.getRequestUrl(),
                    zone,
                    bucketName,
                    paramsQuery,
                    requestSuffixPath);

            if (singedUrl.IndexOf("&upload_id") != -1)//?part_number=0&upload_id=1asfdsf
            {
                if (singedUrl.IndexOf("upload_id") > singedUrl.IndexOf("part_number"))
                {
                    String temp1 = singedUrl.Substring(singedUrl.IndexOf("part_number"), singedUrl.IndexOf("&upload_id") - singedUrl.IndexOf("part_number"));
                    String temp2 = singedUrl.Substring(singedUrl.IndexOf("upload_id"), singedUrl.Length - singedUrl.IndexOf("upload_id"));
                    singedUrl = singedUrl.Substring(0, singedUrl.IndexOf("?") + 1) + temp2 + "&" + temp1; //singedUrl.Replace(temp1, temp2).Replace(temp2,temp1);
                }
            }
            logger.Info(singedUrl);
            if (QSConstant.PARAM_KEY_REQUEST_API_MULTIPART.Equals(requestApi))
            {
                HttpWebRequest request =
                    QSHttpRequestClient.getInstance()
                    .buildStorMultiUpload(
                        method, paramsBody, singedUrl, paramsHeaders, paramsQuery);
                return(request);
            }
            else
            {
                //System.Console.WriteLine(singedUrl);
                HttpWebRequest request =
                    QSHttpRequestClient.getInstance()
                    .buildStorRequest(method, paramsBody, singedUrl, paramsHeaders);
                return(request);
            }
        }