Exemplo n.º 1
0
        public static byte[] Make(string scope)
        {
            Encoding encoding = Encoding.ASCII;

            byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY);
            byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY);
            byte[] upToken   = null;
            try
            {
                byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(scope));
                byte[] digestBase64 = null;
                using (HMACSHA1 hmac = new HMACSHA1(secretKey))
                {
                    byte[] digest = hmac.ComputeHash(policyBase64);
                    digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest));
                }
                using (MemoryStream buffer = new MemoryStream())
                {
                    buffer.Write(accessKey, 0, accessKey.Length);
                    buffer.WriteByte((byte)':');
                    buffer.Write(digestBase64, 0, digestBase64.Length);
                    buffer.WriteByte((byte)':');
                    buffer.Write(policyBase64, 0, policyBase64.Length);
                    upToken = buffer.ToArray();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return(upToken);
        }
Exemplo n.º 2
0
        public static PutFileRet Mkfile(string host, Client client, string entryURI, long fsize,
                                        string customMeta, string callbackParam, string[] ctxs)
        {
            string url = host + "/rs-mkfile/" + Base64UrlSafe.Encode(entryURI) +
                         "/fsize/" + Convert.ToString(fsize);

            if (!String.IsNullOrEmpty(callbackParam))
            {
                url += "/params/" + Base64UrlSafe.Encode(callbackParam);
            }
            if (!String.IsNullOrEmpty(customMeta))
            {
                url += "/meta/" + Base64UrlSafe.Encode(customMeta);
            }

            using (Stream body = new MemoryStream())
            {
                for (int i = 0; i < ctxs.Length; i++)
                {
                    byte[] bctx = Encoding.ASCII.GetBytes(ctxs[i]);
                    body.Write(bctx, 0, bctx.Length);
                    if (i != ctxs.Length - 1)
                    {
                        body.WriteByte((byte)',');
                    }
                }
                body.Seek(0, SeekOrigin.Begin);
                CallRet ret = client.CallWithBinary(url, "text/plain", body, body.Length);
                return(new PutFileRet(ret));
            }
        }
Exemplo n.º 3
0
        public override void SetAuth(HttpWebRequest request, Stream body)
        {
            byte[] secretKey = Encoding.ASCII.GetBytes(Config.SECRET_KEY);
            using (HMACSHA1 hmac = new HMACSHA1(secretKey))
            {
                string pathAndQuery      = request.Address.PathAndQuery;
                byte[] pathAndQueryBytes = Encoding.ASCII.GetBytes(pathAndQuery);
                using (MemoryStream buffer = new MemoryStream())
                {
                    buffer.Write(pathAndQueryBytes, 0, pathAndQueryBytes.Length);
                    buffer.WriteByte((byte)'\n');
                    if (request.ContentType == "application/x-www-form-urlencoded" && body != null)
                    {
                        if (!body.CanSeek)
                        {
                            throw new Exception("stream can not seek");
                        }
                        StreamUtil.Copy(body, buffer);
                        body.Seek(0, SeekOrigin.Begin);
                    }
                    byte[] digest       = hmac.ComputeHash(buffer.ToArray());
                    string digestBase64 = Base64UrlSafe.Encode(digest);

                    string authHead = "QBox " + Config.ACCESS_KEY + ":" + digestBase64;
                    request.Headers.Add("Authorization", authHead);
                }
            }
        }
Exemplo n.º 4
0
        public PutFileRet PutFile(string key, string mimeType, string localFile, string customMeta)
        {
            string entryURI = BucketName + ":" + key;

            if (String.IsNullOrEmpty(mimeType))
            {
                mimeType = "application/octet-stream";
            }
            string url = Config.IO_HOST + "/rs-put/" + Base64UrlSafe.Encode(entryURI) +
                         "/mimeType/" + Base64UrlSafe.Encode(mimeType);

            if (!String.IsNullOrEmpty(customMeta))
            {
                url += "/meta/" + Base64UrlSafe.Encode(customMeta);
            }

            try
            {
                using (FileStream fs = File.OpenRead(localFile))
                {
                    CallRet callRet = Conn.CallWithBinary(url, mimeType, fs, fs.Length);
                    return(new PutFileRet(callRet));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(new PutFileRet(new CallRet(HttpStatusCode.BadRequest, e)));
            }
        }
Exemplo n.º 5
0
        public CallRet Delete(string key)
        {
            string entryURI = BucketName + ":" + key;
            string url      = Config.RS_HOST + "/delete/" + Base64UrlSafe.Encode(entryURI);

            return(Conn.Call(url));
        }
Exemplo n.º 6
0
        public StatRet Stat(string key)
        {
            string  entryURI = BucketName + ":" + key;
            string  url      = Config.RS_HOST + "/stat/" + Base64UrlSafe.Encode(entryURI);
            CallRet callRet  = Conn.Call(url);

            return(new StatRet(callRet));
        }
Exemplo n.º 7
0
        public GetRet GetIfNotModified(string key, string attName, string hash)
        {
            string entryURI = BucketName + ":" + key;
            string url      = Config.RS_HOST + "/get/" + Base64UrlSafe.Encode(entryURI) + "/attName/"
                              + Base64UrlSafe.Encode(attName) + "/base/" + hash;
            CallRet callRet = Conn.Call(url);

            return(new GetRet(callRet));
        }
Exemplo n.º 8
0
        public PutFileRet SaveAs(string url, string specStr, string key)
        {
            string entryURI = BucketName + ":" + key;

            url = url + specStr + "/save-as/" + Base64UrlSafe.Encode(entryURI);
            CallRet callRet = Conn.Call(url);

            return(new PutFileRet(callRet));
        }
Exemplo n.º 9
0
        public static PutFileRet PutFileWithUpToken(
            string upToken, string tableName, string key, string mimeType,
            string localFile, string customMeta, string callbackParam, UInt32 crc32)
        {
            string entryURI = tableName + ":" + key;

            if (String.IsNullOrEmpty(mimeType))
            {
                mimeType = "application/octet-stream";
            }
            string action = "/rs-put/" + Base64UrlSafe.Encode(entryURI) +
                            "/mimeType/" + Base64UrlSafe.Encode(mimeType);

            if (!String.IsNullOrEmpty(customMeta))
            {
                action += "/meta/" + Base64UrlSafe.Encode(customMeta);
            }
            if (crc32 != 0)
            {
                action += "/crc32/" + crc32.ToString();
            }

            try
            {
                var postParams = new Dictionary <string, object>();
                postParams["auth"]   = upToken;
                postParams["action"] = action;
                postParams["file"]   = new FileParameter(localFile, mimeType);
                if (!String.IsNullOrEmpty(callbackParam))
                {
                    postParams["params"] = callbackParam;
                }
                CallRet callRet = MultiPartFormDataPost.Post(Config.UP_HOST + "/upload", postParams);
                return(new PutFileRet(callRet));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(new PutFileRet(new CallRet(HttpStatusCode.BadRequest, e)));
            }
        }
Exemplo n.º 10
0
        public static PutFileRet PutFile(
            string url, string bucketName, string key, string mimeType,
            string localFile, string customMeta, string callbackParam)
        {
            string entryURI = bucketName + ":" + key;

            if (String.IsNullOrEmpty(mimeType))
            {
                mimeType = "application/octet-stream";
            }
            string action = "/rs-put/" + Base64UrlSafe.Encode(entryURI) +
                            "/mimeType/" + Base64UrlSafe.Encode(mimeType);

            if (!String.IsNullOrEmpty(customMeta))
            {
                action += "/meta/" + Base64UrlSafe.Encode(customMeta);
            }

            try
            {
                var postParams = new Dictionary <string, object>();
                postParams["action"] = action;
                postParams["file"]   = new FileParameter(localFile, mimeType);
                if (!String.IsNullOrEmpty(callbackParam))
                {
                    postParams["params"] = callbackParam;
                }
                CallRet callRet = MultiPartFormDataPost.Post(url, postParams);
                return(new PutFileRet(callRet));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(new PutFileRet(new CallRet(HttpStatusCode.BadRequest, e)));
            }
        }
Exemplo n.º 11
0
        public CallRet Publish(string domain)
        {
            String url = Config.RS_HOST + "/publish/" + Base64UrlSafe.Encode(domain) + "/from/" + BucketName;

            return(Conn.Call(url));
        }
Exemplo n.º 12
0
        public CallRet Unpublish(string domain)
        {
            string url = Config.RS_HOST + "/unpublish/" + Base64UrlSafe.Encode(domain);

            return(Conn.Call(url));
        }