예제 #1
0

        
예제 #2
0
        public void CreateFolder()
        {
            if (string.IsNullOrEmpty(m_project))
                throw new Exception(Strings.GoogleCloudStorage.ProjectIDMissingError(PROJECT_OPTION));

            var data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new CreateBucketRequest() {
                name = m_bucket,
                location = m_location,
                storageClass = m_storage_class
            }));

            var url = string.Format("{0}/b?project={1}", API_URL, m_project);

            var req = m_oauth.CreateRequest(url);
            req.Method = "POST";
            req.ContentLength = data.Length;
            req.ContentType = "application/json; charset=UTF-8";

            var areq = new AsyncHttpRequest(req);

            using(var rs = areq.GetRequestStream())
                rs.Write(data, 0, data.Length);

            m_oauth.ReadJSONResponse<BucketResourceItem>(areq);
        }
예제 #3
0
        public void Rename(string oldname, string newname)
        {
            var data = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new BucketResourceItem() {
                name = m_prefix + newname,
            }));

            var url = string.Format("{0}/b/{1}/o/{2}", API_URL, m_bucket, Library.Utility.Uri.UrlPathEncode(m_prefix + oldname));
            var req = m_oauth.CreateRequest(url);
            req.Method = "PATCH";
            req.ContentLength = data.Length;
            req.ContentType = "application/json; charset=UTF-8";

            var areq = new AsyncHttpRequest(req);
            using(var rs = areq.GetRequestStream())
                rs.Write(data, 0, data.Length);

            m_oauth.ReadJSONResponse<BucketResourceItem>(req);
        }
예제 #4
0
        public FileMetaData UploadFile(String path, Stream stream)
        {
            // start a session
            UploadSessionStartArg ussa = new UploadSessionStartArg();

            var url = string.Format("{0}/files/upload_session/start", CONTENT_API_URL);
            HttpWebRequest req = CreateRequest(url, "POST");
            req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(ussa);
            req.ContentType = "application/octet-stream";
            req.ContentLength = Math.Min(DROPBOX_MAX_CHUNK_UPLOAD,stream.Length);
            req.Timeout = 200000;

            var areq = new AsyncHttpRequest(req);

            byte[] buffer = new byte[Utility.Utility.DEFAULT_BUFFER_SIZE];

            UInt64 globalBytesRead = 0;
            using (var rs = areq.GetRequestStream())
            {
                int bytesRead = 0;
                do
                {
                    bytesRead = stream.Read(buffer, 0, (int)Utility.Utility.DEFAULT_BUFFER_SIZE);
                    globalBytesRead += (UInt64)bytesRead;
                    rs.Write(buffer, 0, bytesRead);

                }
                while (bytesRead > 0 && globalBytesRead < DROPBOX_MAX_CHUNK_UPLOAD);

            }

            //Console.WriteLine(((HttpWebResponse)areq.GetResponse()).StatusCode);

            UploadSessionStartResult ussr = ReadJSONResponse<UploadSessionStartResult>(areq); // pun intended

            // keep appending until finished
            // 1) read into buffer
            while (globalBytesRead < (UInt64)stream.Length)
            {

                UInt64 remaining = (UInt64)stream.Length - globalBytesRead;

                // start an append request
                UploadSessionAppendArg usaa = new UploadSessionAppendArg();
                usaa.cursor.session_id = ussr.session_id;
                usaa.cursor.offset = globalBytesRead;
                usaa.close = remaining < DROPBOX_MAX_CHUNK_UPLOAD;
                url = string.Format("{0}/files/upload_session/append_v2", CONTENT_API_URL);

                req = CreateRequest(url, "POST");
                req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(usaa);
                req.ContentType = "application/octet-stream";
                req.ContentLength = Math.Min(DROPBOX_MAX_CHUNK_UPLOAD, (long)remaining);
                req.Timeout = 200000;

                areq = new AsyncHttpRequest(req);

                UInt64 bytesReadInRequest = 0;
                using (var rs = areq.GetRequestStream())
                {
                    int bytesRead = 0;
                    do
                    {
                        bytesRead = stream.Read(buffer, 0, (int)Utility.Utility.DEFAULT_BUFFER_SIZE);
                        bytesReadInRequest += (UInt64)bytesRead;
                        globalBytesRead += (UInt64)bytesRead;
                        rs.Write(buffer, 0, bytesRead);

                    }
                    while (bytesRead > 0 && bytesReadInRequest < Math.Min(remaining, DROPBOX_MAX_CHUNK_UPLOAD));
                }
                HttpWebResponse response = GetResponse(areq);
                StreamReader sr = new StreamReader(response.GetResponseStream());
                sr.ReadToEnd();

            }

            // finish session and commit
            try
            {
                UploadSessionFinishArg usfa = new UploadSessionFinishArg();
                usfa.cursor.session_id = ussr.session_id;
                usfa.cursor.offset = (UInt64)globalBytesRead;
                usfa.commit.path = path;

                url = string.Format("{0}/files/upload_session/finish", CONTENT_API_URL);
                req = CreateRequest(url, "POST");
                req.Headers[API_ARG_HEADER] = JsonConvert.SerializeObject(usfa);
                req.ContentType = "application/octet-stream";
                req.Timeout = 200000;

                areq = new AsyncHttpRequest(req);

                //using (var rs = areq.GetRequestStream())
                //{
                //    int bytesRead = 0;
                //    do
                //    {
                //        bytesRead = stream.Read(buffer, 0, (int) Utility.Utility.DEFAULT_BUFFER_SIZE);
                //        globalBytesRead += (UInt64)bytesRead;
                //        rs.Write(buffer, 0, bytesRead);

                //    } while (bytesRead > 0);
                //}
                FileMetaData fmd = ReadJSONResponse<FileMetaData>(areq);
                return fmd;
            }
            catch (Exception ex)
            {
                handleDropboxException(ex);
                throw;
            }
        }