Esempio n. 1
0
        private static void GetObjectCallback(IAsyncResult ar)
        {
            try
            {
                var result = client.EndGetObject(ar);

                using (var stream = result.Content)
                {
                    var buffer    = new byte[1024 * 1024];
                    var bytesRead = 0;
                    while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        // 处理读出的数据
                    }
                }

                Console.WriteLine(ar.AsyncState as string);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                _event.Set();
            }
        }
        private static void GetObjectCallback(IAsyncResult ar)
        {
            try
            {
                var result = client.EndGetObject(ar);

                using (var requestStream = result.Content)
                {
                    using (var fs = File.Open(dirToDownload + "/sample2.data", FileMode.OpenOrCreate))
                    {
                        int length = 4 * 1024;
                        var buf    = new byte[length];
                        do
                        {
                            length = requestStream.Read(buf, 0, length);
                            fs.Write(buf, 0, length);
                        } while (length != 0);
                    }
                }

                Console.WriteLine(ar.AsyncState as string);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                _event.Set();
            }
        }
Esempio n. 3
0
 private static void GetObjectCallback(IAsyncResult ar)
 {
     try
     {
         var    result      = ossClient.EndGetObject(ar);
         byte[] buffer      = new byte[4096];
         var    reponseBody = result.Content;
         using (reponseBody)
         {
             while (reponseBody.Read(buffer, 0, 4096) != 0)
             {
                 ;
             }
         }
         Console.WriteLine("ETag: " + result.Metadata.ETag);
         Console.WriteLine("AsyncState:" + ar.AsyncState as string);
     }
     catch (Exception ex)
     {
         OssException ae = ex as OssException;
         Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                           ae.ErrorCode, ae.Message, ae.RequestId, ae.HostId);
     }
     finally
     {
         _evnet.Set();
     }
 }
        public async Task <Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            var response = await Task.Factory.FromAsync(
                (request, callback, state) => _client.BeginGetObject(request, callback, state),
                result => _client.EndGetObject(result),
                new GetObjectRequest(_bucket, NormalizePath(path)),
                null
                ).AnyContext();

            return(response.Content);
        }
Esempio n. 5
0
        private static void GetObjectCallback(IAsyncResult ar)
        {
            Hashtable       state       = (Hashtable)ar.AsyncState;
            Action <string> getCallback = (Action <string>)state["callback"];

            try
            {
                OssClient ossClient     = (OssClient)state["client"];
                string    ossfilename   = (string)state["ossfilename"];
                string    localfilename = (string)state["localfilename"];

                var result   = ossClient.EndGetObject(ar);
                var metadata = result.Metadata;

                var requestStream = result.Content;
                var fs            = new FileStream(localfilename, FileMode.OpenOrCreate);
                int bufLength     = 4 * 1024;
                var buf           = new byte[bufLength];

                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                md5.Initialize();

                int offset        = 0;
                int contentLength = (int)metadata.ContentLength;
                while (offset < contentLength)
                {
                    int readSize = bufLength;
                    if (offset + readSize > contentLength)
                    {
                        readSize = contentLength - offset;
                    }

                    readSize = requestStream.Read(buf, 0, readSize);
                    fs.Write(buf, 0, readSize);

                    if (offset + readSize < contentLength)                     // 不是最后一块
                    {
                        md5.TransformBlock(buf, 0, Convert.ToInt32(readSize), buf, 0);
                    }
                    else                     // 最后一块
                    {
                        md5.TransformFinalBlock(buf, 0, Convert.ToInt32(readSize));
                    }

                    offset += readSize;
                }
                fs.Close();

                byte[] md5Hash = md5.Hash;
                md5.Clear();

                string ext       = System.IO.Path.GetExtension(ossfilename);
                string buffermd5 = Utils.File.FormatMD5(md5Hash) + ext;
                if (buffermd5 == ossfilename)
                {
                    _dispatcher.Enqueue(() => {
                        if (getCallback != null)
                        {
                            getCallback(localfilename);
                        }
                    });
                }
                else
                {
                    System.IO.File.Delete(localfilename);
                    _dispatcher.Enqueue(() => {
                        if (getCallback != null)
                        {
                            getCallback("CheckMD5Failed");
                        }
                    });
                }

                Console.WriteLine(ar.AsyncState as string);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                _dispatcher.Enqueue(() => {
                    if (getCallback != null)
                    {
                        if (ex is ServiceException)
                        {
                            string errorCode = ((ServiceException)ex).ErrorCode;
                            getCallback(errorCode);
                            if (errorCode == "InvalidAccessKeyId")
                            {
                                FetchSTSToken();
                            }
                        }
                        else
                        {
                            getCallback(ex.Message);
                        }
                    }
                });
            }
            finally
            {
//				_event.Set();
            }
        }