예제 #1
0
        /**
         * 开始下载,并新建一个线程用于IO操作
         */
        private void start()
        {
            GetObjectRequest getObjectRequest = new GetObjectRequest(this.bucketName, this.key, this.file);
            getObjectRequest.setProgressListener(this);

            Thread thread = new Thread(delegate() {
                KS3Object ks3Obj = null;
                try
                {
                     ks3Obj = ks3Client.getObject(getObjectRequest);
                }
                catch (ServiceException e)
                {
                    showMessage(e.ToString(), "错误");
                }
                catch (ProgressInterruptedException e)
                {
                    // Do nothing here ...
                }
                catch
                {
                    showMessage("未知错误,请稍后再试", "错误");
                }
                finally
                {
                    if (ks3Obj != null && ks3Obj.getObjectContent() != null)
                        ks3Obj.getObjectContent().Close();
                }

            });
            thread.Start();
        }
예제 #2
0
        private static bool getObject()
        {
            try
            {
                // Get Object(download and store in memory)
                Console.WriteLine("--- Download and Store in Memory ---");

                GetObjectRequest getShortContent = new GetObjectRequest(bucketName, objKeyNameMemoryData);
                getShortContent.setRange(0, 24);
                KS3Object ks3Object = ks3Client.getObject(getShortContent);

                StreamReader sr = new StreamReader(ks3Object.getObjectContent());
                Console.WriteLine("Content:\n" + sr.ReadToEnd());
                sr.Close();
                ks3Object.getObjectContent().Close();

                Console.WriteLine("------------------------------------\n");
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }

            try
            {
                // Get Object(download and save as a file)
                Console.WriteLine("--- Download a File ---");

                // I need to get the Content-Length to set the listener.
                ObjectMetadata objectMetadata = ks3Client.getObjectMetadata(bucketName, objKeyNameFileData);

                SampleListener downloadListener = new SampleListener(objectMetadata.getContentLength());
                GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objKeyNameFileData, new FileInfo(outFilePath));
                getObjectRequest.setProgressListener(downloadListener);
                KS3Object obj = ks3Client.getObject(getObjectRequest);
                obj.getObjectContent().Close(); // The file was opened in [KS3ObjectResponseHandler], so I close it first.

                Console.WriteLine("Success. See the file downloaded at {0}", outFilePath);
                Console.WriteLine("-----------------------\n");
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }

            return true;
        }