public void MultipartUploadPositiveTest()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            // Initiate a multipart upload
            var initRequest = new InitiateMultipartUploadRequest(_bucketName, key);

            initRequest.RequestPayer = RequestPayer.Requester;
            var initResult = _ossPayerClient.InitiateMultipartUpload(initRequest);

            // Sets the part size as 100K
            const int partSize = 100 * 1024;
            var       partFile = new FileInfo(Config.UploadTestFile);
            // Calculates the part count
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            // creates a list of PartETag
            var partETags = new List <PartETag>();

            //upload the file
            using (var fs = new FileStream(partFile.FullName, FileMode.Open))
            {
                for (var i = 0; i < partCount; i++)
                {
                    // skip to the start of each part
                    long skipBytes = partSize * i;
                    fs.Position = skipBytes;

                    // calculates the part size
                    var size = partSize < partFile.Length - skipBytes
                        ? partSize
                        : partFile.Length - skipBytes;

                    // uploads the part
                    var uploadPartRequest = new UploadPartRequest(_bucketName, key, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    uploadPartRequest.RequestPayer = RequestPayer.Requester;
                    var uploadPartResult = _ossPayerClient.UploadPart(uploadPartRequest);

                    // adds the result to the list
                    partETags.Add(uploadPartResult.PartETag);
                }
            }

            var listpartRequest = new ListPartsRequest(_bucketName, key, initResult.UploadId);

            listpartRequest.RequestPayer = RequestPayer.Requester;

            var listpartResult = _ossPayerClient.ListParts(listpartRequest);

            Assert.AreEqual(listpartResult.UploadId, initResult.UploadId);

            var completeRequest = new CompleteMultipartUploadRequest(_bucketName, key, initResult.UploadId);

            completeRequest.RequestPayer = RequestPayer.Requester;
            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }
            var completeMultipartUploadResult = _ossPayerClient.CompleteMultipartUpload(completeRequest);

            //delete the object
            string contentCRC;

            using (var fs = new FileStream(Config.UploadTestFile, FileMode.Open))
            {
                contentCRC = OssUtils.ComputeContentCrc64(fs, fs.Length);
            }
            var getResult = _ossClient.GetObject(_bucketName, key);

            Assert.AreEqual(getResult.Metadata.Crc64, contentCRC);
            _ossClient.DeleteObject(_bucketName, key);

            //listuploads
            // Initiate a multipart upload
            initRequest = new InitiateMultipartUploadRequest(_bucketName, key);
            initResult  = _ossClient.InitiateMultipartUpload(initRequest);
            var uploadPartRequest1 = new UploadPartRequest(_bucketName, key, initResult.UploadId)
            {
                InputStream = new MemoryStream(Encoding.ASCII.GetBytes("hello world")),
                PartSize    = 11,
                PartNumber  = 1
            };

            _ossClient.UploadPart(uploadPartRequest1);

            var listUploadsRequest = new ListMultipartUploadsRequest(_bucketName);

            listUploadsRequest.RequestPayer = RequestPayer.Requester;
            listUploadsRequest.Prefix       = key;
            var listUploadsResult = _ossPayerClient.ListMultipartUploads(listUploadsRequest);
            int cnt = 0;

            foreach (var multipartUpload in listUploadsResult.MultipartUploads)
            {
                Assert.AreEqual(multipartUpload.UploadId, initResult.UploadId);
                Assert.AreEqual(multipartUpload.Key, key);
                cnt++;
            }
            Assert.AreEqual(cnt, 1);

            //abort
            var abortRequest = new AbortMultipartUploadRequest(_bucketName, key, initResult.UploadId);

            abortRequest.RequestPayer = RequestPayer.Requester;
            _ossPayerClient.AbortMultipartUpload(abortRequest);


            listUploadsRequest        = new ListMultipartUploadsRequest(_bucketName);
            listUploadsRequest.Prefix = key;
            listUploadsResult         = _ossClient.ListMultipartUploads(listUploadsRequest);
            cnt = 0;
            foreach (var multipartUpload in listUploadsResult.MultipartUploads)
            {
                Assert.AreEqual(multipartUpload.UploadId, initResult.UploadId);
                Assert.AreEqual(multipartUpload.Key, key);
                cnt++;
            }
            Assert.AreEqual(cnt, 0);
        }
Example #2
0
        public void TestDisableCrc()
        {
            Common.ClientConfiguration config = new Common.ClientConfiguration();
            config.EnableCrcCheck = false;
            IOss ossClient  = OssClientFactory.CreateOssClient(config);
            var  targetFile = OssTestUtils.GetTargetFileName(_className);

            targetFile = Path.Combine(Config.DownloadFolder, targetFile);
            var objectKeyName = "test-object-disable-crc";

            try
            {
                // put & get
                PutObjectResult putObjectResult = ossClient.PutObject(_bucketName, objectKeyName, Config.UploadTestFile);
                var             actualCrc       = putObjectResult.ResponseMetadata[HttpHeaders.HashCrc64Ecma];

                OssObject ossObject   = ossClient.GetObject(_bucketName, objectKeyName);
                var       expectedCrc = OssUtils.ComputeContentCrc64(ossObject.Content, ossObject.ContentLength);

                Assert.AreEqual(expectedCrc, actualCrc);
                ossClient.DeleteObject(_bucketName, objectKeyName);

                // put & get by uri
                var testStr    = FileUtils.GenerateOneKb();
                var content    = Encoding.ASCII.GetBytes(testStr);
                var now        = DateTime.Now;
                var expireDate = now.AddSeconds(120);
                var uri        = _ossClient.GeneratePresignedUri(_bucketName, objectKeyName, expireDate, SignHttpMethod.Put);
                var putResult  = ossClient.PutObject(uri, new MemoryStream(content));

                expectedCrc = putResult.ResponseMetadata[HttpHeaders.HashCrc64Ecma];
                actualCrc   = OssUtils.ComputeContentCrc64(new MemoryStream(content), content.Length);
                Assert.AreEqual(expectedCrc, actualCrc);

                uri         = _ossClient.GeneratePresignedUri(_bucketName, objectKeyName, expireDate, SignHttpMethod.Get);
                ossObject   = ossClient.GetObject(uri);
                expectedCrc = OssUtils.ComputeContentCrc64(ossObject.Content, ossObject.ContentLength);

                Assert.AreEqual(expectedCrc, actualCrc);
                ossClient.DeleteObject(_bucketName, objectKeyName);

                // upload & download
                var uploadObjectResult = ossClient.ResumableUploadObject(_bucketName, objectKeyName, Config.MultiUploadTestFile, null,
                                                                         Config.DownloadFolder);
                actualCrc = uploadObjectResult.ResponseMetadata[HttpHeaders.HashCrc64Ecma];

                DownloadObjectRequest downloadObjectRequest = new DownloadObjectRequest(_bucketName, objectKeyName, targetFile);
                var metadata = ossClient.ResumableDownloadObject(downloadObjectRequest);

                Assert.AreEqual(metadata.Crc64, actualCrc);
                ossClient.DeleteObject(_bucketName, objectKeyName);

                // append
                using (var fs = File.Open(Config.UploadTestFile, FileMode.Open))
                {
                    var fileLength          = fs.Length;
                    var appendObjectRequest = new AppendObjectRequest(_bucketName, objectKeyName)
                    {
                        Content = fs,
                    };
                    var appendObjectResult = _ossClient.AppendObject(appendObjectRequest);
                    fs.Seek(0, SeekOrigin.Begin);
                    actualCrc = OssUtils.ComputeContentCrc64(fs, fs.Length);
                    Assert.AreEqual(appendObjectResult.HashCrc64Ecma.ToString(), actualCrc);
                    ossClient.DeleteObject(_bucketName, objectKeyName);
                }
            }
            finally
            {
                try
                {
                    FileUtils.DeleteFile(targetFile);
                }
                catch (Exception e)
                {
                    LogUtility.LogWarning("Delete file {0} failed with Exception {1}", targetFile, e.Message);
                }
            }
        }
        public void OssUtilsTest()
        {
            var conf = new ClientConfiguration();

            Assert.AreEqual(OssUtils.IsBucketNameValid(""), false);

            var str = OssUtils.MakeResourcePath(new Uri("http://192.168.1.1"), "bucket", "key/abc/");

            Assert.AreEqual(str, "bucket/key/abc/");

            //cname
            conf.IsCname = true;
            var uri = OssUtils.MakeBucketEndpoint(new Uri("http://endpoint"), "bucket", conf);

            Assert.AreEqual(uri.ToString(), "http://endpoint/");

            //ip endpoint & endpoint port
            conf.IsCname = false;
            uri          = OssUtils.MakeBucketEndpoint(new Uri("http://192.168.1.1"), "bucket", conf);
            Assert.AreEqual(uri.ToString(), "http://192.168.1.1/");

            uri = OssUtils.MakeBucketEndpoint(new Uri("http://192.168.1.1:3128"), "bucket", conf);
            Assert.AreEqual(uri.ToString(), "http://192.168.1.1:3128/");

            //bucket
            conf.IsCname = false;
            uri          = OssUtils.MakeBucketEndpoint(new Uri("http://endpoint"), null, conf);
            Assert.AreEqual(uri.ToString(), "http://endpoint/");

            conf.IsCname = false;
            uri          = OssUtils.MakeBucketEndpoint(new Uri("http://endpoint"), "bucket", conf);
            Assert.AreEqual(uri.ToString(), "http://bucket.endpoint/");

            //TrimQuotes
            Assert.AreEqual(OssUtils.TrimQuotes("\"test\""), "test");
            Assert.AreEqual(OssUtils.TrimQuotes("test\""), "test");
            Assert.AreEqual(OssUtils.TrimQuotes("\"test"), "test");
            Assert.AreEqual(OssUtils.TrimQuotes(null), null);

            //ComputeContentCrc64
            var content = new MemoryStream(Encoding.ASCII.GetBytes(""));

            str = OssUtils.ComputeContentCrc64(content, 1);
            Assert.AreEqual(str, string.Empty);

            //JoinETag
            var etagList = new List <string>();

            etagList.Add("etag1");
            etagList.Add("etag2");
            str = OssUtils.JoinETag(etagList);
            Assert.AreEqual(str, "etag1, etag2");

            //GetResourcePathFromSignedUrl
            str = OssUtils.GetResourcePathFromSignedUrl(new Uri("http://endpoint/key"));
            Assert.AreEqual(str, "key");

            str = OssUtils.GetResourcePathFromSignedUrl(new Uri("http://endpoint"));
            Assert.AreEqual(str, "");

            //GetParametersFromSignedUrl
        }