Ejemplo n.º 1
0
            public void MFinish()
            {
                HttpWebRequest  request  = null;
                HttpWebResponse response = null;

                try
                {
                    string url = URL(PROCESS_TYPE.MFINISH);
                    request           = (HttpWebRequest)WebRequest.Create(url);
                    request.Method    = "POST";
                    request.KeepAlive = false;
                    Utils.SetHeaders(request, "", m_bucket, m_key, "POST");

                    MemoryStream ms = new MemoryStream();
                    StreamWriter sw = new StreamWriter(ms);
                    for (int idx = 0; idx < m_etags.Count; ++idx)
                    {
                        sw.Write(m_etags[idx] + ",");
                    }
                    sw.Flush();
                    ms.Position = 0;
                    Utils.Copy(request.GetRequestStream(), ms);

                    response = HttpWebResponseExt.GetResponseNoException(request);
                    Stream body = response.GetResponseStream();
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        string e = UFileErrorSerializer.FormatString(body);
                        throw new Exception(string.Format("{0} {1}", response.StatusDescription, e));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    throw;
                }
                finally
                {
                    if (request != null)
                    {
                        request.Abort();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }
Ejemplo n.º 2
0
            public void MInit()
            {
                HttpWebRequest  request  = null;
                HttpWebResponse response = null;

                try
                {
                    string url = URL(PROCESS_TYPE.MINIT);
                    request           = (HttpWebRequest)WebRequest.Create(url);
                    request.Method    = "POST";
                    request.KeepAlive = false;
                    Utils.SetHeaders(request, "", m_bucket, m_key, "POST");
                    response = HttpWebResponseExt.GetResponseNoException(request);
                    Stream body = response.GetResponseStream();

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        string e = UFileErrorSerializer.FormatString(body);
                        throw new Exception(string.Format("{0} {1}", response.StatusDescription, e));
                    }

                    //parsing to get the uploadid & blksize
                    MInitResponse minitRes = new MInitResponse();
                    minitRes.Parse(body);

                    m_uploadid = minitRes.GetUploadId();
                    m_blk_size = minitRes.GetBlkSize();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    throw;
                }
                finally
                {
                    if (request != null)
                    {
                        request.Abort();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }
Ejemplo n.º 3
0
        public static void GetFile(string bucket, string key, Stream stream)
        {
            HttpWebRequest  request  = null;
            HttpWebResponse response = null;

            try
            {
                request           = (HttpWebRequest)WebRequest.Create(Utils.GetURL(bucket, key));
                request.KeepAlive = false;
                Utils.SetHeaders(request, string.Empty, bucket, key, "GET");

                response = HttpWebResponseExt.GetResponseNoException(request);
                Stream body = response.GetResponseStream();
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    string e = UFileErrorSerializer.FormatString(body);
                    throw new Exception(string.Format("{0} {1}", response.StatusDescription, e));
                }

                int    bytesRead;
                byte[] buffer = new byte[1024];
                while ((bytesRead = body.Read(buffer, 0, buffer.Length)) != 0)
                {
                    stream.Write(buffer, 0, bytesRead);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw;
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
            return;
        }
Ejemplo n.º 4
0
        public static string PutFile(string bucket, string key, string file)
        {
            if (!System.IO.File.Exists(file))
            {
                throw new Exception(string.Format("{0} does not exist", file));
            }
            string          strResult = string.Empty;
            HttpWebRequest  request   = null;
            HttpWebResponse response  = null;

            try
            {
                request           = (HttpWebRequest)WebRequest.Create(Utils.GetURL(bucket, key));
                request.KeepAlive = false;
                Utils.SetHeaders(request, file, bucket, key, "PUT");
                Utils.CopyFile(request, file);

                response = HttpWebResponseExt.GetResponseNoException(request);
                Stream body = response.GetResponseStream();
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    string e = UFileErrorSerializer.FormatString(body);
                    throw new Exception(string.Format("{0} {1}", response.StatusDescription, e));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw;
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
            return(strResult);
        }
Ejemplo n.º 5
0
        public static string DeleteFile(string bucket, string key)
        {
            string          strResult = string.Empty;
            HttpWebRequest  request   = null;
            HttpWebResponse response  = null;

            try
            {
                request           = (HttpWebRequest)WebRequest.Create(Utils.GetURL(bucket, key));
                request.KeepAlive = false;
                Utils.SetHeaders(request, string.Empty, bucket, key, "DELETE");

                response = HttpWebResponseExt.GetResponseNoException(request);
                Stream body = response.GetResponseStream();
                if (response.StatusCode != HttpStatusCode.NoContent)
                {
                    string e = UFileErrorSerializer.FormatString(body);
                    throw new Exception(string.Format("{0} {1}", response.StatusDescription, e));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw;
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
            return(strResult);
        }
Ejemplo n.º 6
0
            //part == -1 means the caller wants to upload all the parts one after after another
            //otherwise,MUpload will only upload one indicated part(for example,you need to upload
            //the same part when this part upload failed before)
            public void MUpload(long part = -1)
            {
                long total_parts_upload = -1;

                if (part == -1)
                {
                    m_part_number = 0;
                }
                else
                {
                    m_part_number      = part;
                    total_parts_upload = 1;
                }

                HttpWebRequest  request  = null;
                HttpWebResponse response = null;

                try
                {
                    long parts_uploaded = 0;
                    bool finished       = false;
                    while (true)
                    {
                        if (finished || (total_parts_upload != -1 && parts_uploaded >= total_parts_upload))
                        {
                            break;
                        }

                        string url = URL(PROCESS_TYPE.MUPLOAD);
                        request           = (HttpWebRequest)WebRequest.Create(url);
                        request.Method    = "PUT";
                        request.KeepAlive = false;

                        Utils.SetHeaders(request, m_filename, m_bucket, m_key, "PUT");
                        m_file.Seek(m_part_number * m_blk_size, 0);

                        MemoryStream ms = new MemoryStream();
                        long         n  = Utils.CopyNBit(ms, m_file, m_blk_size);
                        if (n < m_blk_size)
                        {
                            finished = true;
                            if (n == 0)
                            {
                                break;
                            }
                        }
                        ms.Position = 0;
                        //set content-length
                        request.ContentLength = n;
                        Utils.CopyNBit(request.GetRequestStream(), ms, n);

                        response = HttpWebResponseExt.GetResponseNoException(request);
                        Stream body = response.GetResponseStream();

                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            string e = UFileErrorSerializer.FormatString(body);
                            throw new Exception(string.Format("{0} {1}", response.StatusDescription, e));
                        }
                        else
                        {
                            string etag = response.GetResponseHeader("ETag");
                            m_etags.Add(etag);
                        }
                        response.Close();

                        m_part_number  += 1;
                        parts_uploaded += 1;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    throw;
                }
                finally
                {
                    if (request != null)
                    {
                        request.Abort();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }