예제 #1
0
        private static bool putObject()
        {
            try
            {
                Console.WriteLine("--- Upload a File ---");

                FileInfo         file             = new FileInfo("H:/haixt111/[email protected]");//  d:/test1.doc
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objKeyNameFileData, file);

                //CannedAccessControlList cannedAcl = new CannedAccessControlList(CannedAccessControlList.PRIVATE);
                //putObjectRequest.setCannedAcl(cannedAcl);

                SampleListener sampleListener = new SampleListener(file.Length);
                putObjectRequest.setProgressListener(sampleListener);
                PutObjectResult putObjectResult = ks3Client.putObject(putObjectRequest);

                Console.WriteLine("Upload Completed. eTag=" + putObjectResult.getETag() + ", MD5=" + putObjectResult.getContentMD5());
                Console.WriteLine("---------------------\n");
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }

            return(true);
        }
예제 #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);
        }
예제 #3
0
        /**
         * 分块上传例子
         * **/
        private static bool uploadPart()
        {
            string path = @"you file path";//上传文件路径,例如E:\tool\aa.rar
            InitiateMultipartUploadResult result = multipartUp();
            FileInfo file           = new FileInfo(path);
            int      part           = 5 * 1024 * 1024;
            int      numBytesToRead = (int)file.Length;
            int      i    = 0;
            XElement root = new XElement("CompleteMultipartUpload");//初始化一个xml,以备分块上传完成后调用complete方法提交本次上传的文件以通知服务端合并分块

            //开始读取文件
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                while (numBytesToRead > 0)
                {
                    UploadPartRequest request = new UploadPartRequest(
                        result.getBucket(), result.getKey(), result.getUploadId(),
                        i + 1);
                    //每次读取5M文件内容,如果最后一次内容不及5M则按实际大小取值
                    int    count = Convert.ToInt32((i * part + part) > file.Length ? file.Length - i * part : part);
                    byte[] data  = new byte[count];
                    int    n     = fs.Read(data, 0, count);
                    request.setInputStream(new MemoryStream(data));
                    ProgressListener sampleListener = new SampleListener(count); //实例一个更新进度的监听类,实际使用中可自己定义实现
                    request.setProgressListener(sampleListener);
                    PartETag tag = ks3Client.uploadPart(request);                //上传本次分块内容
                    Console.WriteLine(tag.ToString());
                    if (n == 0)
                    {
                        break;
                    }
                    numBytesToRead -= n;

                    XElement partE = new XElement("Part");
                    partE.Add(new XElement("PartNumber", i + 1));
                    partE.Add(new XElement("ETag", tag.geteTag()));
                    root.Add(partE);
                    i++;
                }
            }
            //所有分块上传完成后发起complete request,通知服务端合并分块
            CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(result.getBucket(), result.getKey(), result.getUploadId());

            completeRequest.setContent(new MemoryStream(System.Text.Encoding.Default.GetBytes(root.ToString())));
            CompleteMultipartUploadResult completeResult = ks3Client.completeMultipartUpload(completeRequest);

            return(true);
        }
예제 #4
0
        private static bool putObject()
        {
            try
            {
                //Console.WriteLine("--- create a folder: ---");
                //Stream streamNull = new MemoryStream();
                //PutObjectResult createFolder = ks3Client.putObject("bucketcors", "jrtest", streamNull, null);
                //Console.WriteLine("---------------------\n");

                //// Put Object(upload a short content)
                //Console.WriteLine("--- Upload a Short Content: ---");
                //String sampleContent = "This is a sample content.(25 characters before, included the 4 spaces)";
                //Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleContent));
                //PutObjectResult shortContentResult = ks3Client.putObject("bucketcors", "jrtest/aa", stream, null);

                //Console.WriteLine("Upload Completed. eTag=" + shortContentResult.getETag() + ", MD5=" + shortContentResult.getContentMD5());
                //Console.WriteLine("-------------------------------\n");

                //Put Object(upload a file)
                Console.WriteLine("--- Upload a File ---");
                bucketName         = "kingsoft.test.ml";
                objKeyNameFileData = "testPut2.mp4";
                FileInfo                file             = new FileInfo("d:/hengping.mp4");
                PutObjectRequest        putObjectRequest = new PutObjectRequest(bucketName, objKeyNameFileData, file);
                CannedAccessControlList cannedAcl        = new CannedAccessControlList(CannedAccessControlList.PRIVATE);
                putObjectRequest.setCannedAcl(cannedAcl);

                SampleListener sampleListener = new SampleListener(file.Length);
                putObjectRequest.setProgressListener(sampleListener);
                PutObjectResult putObjectResult = ks3Client.putObject(putObjectRequest);

                Console.WriteLine("Upload Completed. eTag=" + putObjectResult.getETag() + ", MD5=" + putObjectResult.getContentMD5());
                Console.WriteLine("---------------------\n");
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }

            return(true);
        }
예제 #5
0
        private static bool putObject()
        {
            try
            {
                // Put Object(upload a short content)
                Console.WriteLine("--- Upload a Short Content: ---");

                String sampleContent = "This is a sample content.(25 characters before, included the 4 spaces)";
                Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleContent));
                PutObjectResult shortContentResult = ks3Client.putObject(bucketName, objKeyNameMemoryData, stream, null);

                Console.WriteLine("Upload Completed. eTag=" + shortContentResult.getETag() + ", MD5=" + shortContentResult.getContentMD5());
                Console.WriteLine("-------------------------------\n");

                // Put Object(upload a file)
                Console.WriteLine("--- Upload a File ---");

                FileInfo file = new FileInfo(inFilePath);
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objKeyNameFileData, file);
                SampleListener sampleListener = new SampleListener(file.Length);
                putObjectRequest.setProgressListener(sampleListener);
                PutObjectResult putObjectResult = ks3Client.putObject(putObjectRequest);

                Console.WriteLine("Upload Completed. eTag=" + putObjectResult.getETag() + ", MD5=" + putObjectResult.getContentMD5());
                Console.WriteLine("---------------------\n");
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }

            return true;
        }
예제 #6
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;
        }