public void GeneratePresignedUriStsTokenTest()
        {
            var targetObject = OssTestUtils.GetObjectKey(_className);

            try
            {
                var client = new OssClient(Config.Endpoint,
                                           new Common.Authentication.DefaultCredentialsProvider(
                                               new Common.Authentication.DefaultCredentials(Config.AccessKeyId, Config.AccessKeySecret, "test-token")));

                var gpuRequest = new GeneratePresignedUriRequest(_bucketName, targetObject, SignHttpMethod.Get)
                {
                    ContentType = "text/rtf",
                    Expiration  = DateTime.Now.AddHours(1),
                };
                var uri = client.GeneratePresignedUri(gpuRequest);
                Assert.AreNotEqual(uri.ToString().IndexOf("security-token=test-token"), -1);
            }
            catch
            {
                Assert.IsTrue(false);
            }

            var request = new GeneratePresignedUriRequest(_bucketName, targetObject, SignHttpMethod.Get)
            {
                ContentType = "text/rtf",
                Expiration  = DateTime.Now.AddHours(1),
            };

            request.BucketName = null;
            _ossClient.GeneratePresignedUri(request);

            request.BucketName = _bucketName;
            request.Key        = targetObject;
            _ossClient.GeneratePresignedUri(request);
        }
        public void ResumableDownloadBigFileWithMd5CheckTest()
        {
            var targetFile = OssTestUtils.GetTargetFileName(_className);

            targetFile = Path.Combine(Config.DownloadFolder, targetFile);

            var key = OssTestUtils.GetObjectKey(_className);

            _ossClient.PutObject(_bucketName, key, Config.MultiUploadTestFile);

            var config = new ClientConfiguration();

            config.EnalbeMD5Check = true;
            var client = OssClientFactory.CreateOssClient(config);

            try
            {
                DownloadObjectRequest request = new DownloadObjectRequest(_bucketName, key, targetFile);
                var metadata           = client.ResumableDownloadObject(request);
                var expectedETag       = metadata.ETag;
                var downloadedFileETag = FileUtils.ComputeContentMd5(targetFile);
                Assert.AreEqual(expectedETag.ToLowerInvariant(), downloadedFileETag.ToLowerInvariant());
            }
            finally
            {
                try
                {
                    FileUtils.DeleteFile(targetFile);
                }
                catch (Exception e)
                {
                    LogUtility.LogWarning("Delete file {0} failed with Exception {1}", targetFile, e.Message);
                }
                _ossClient.DeleteObject(_bucketName, key);
            }
        }
 public void GeneratePresignedUriWithExecptionTest()
 {
     try
     {
         var targetObject = OssTestUtils.GetObjectKey(_className);
         var gpuRequest   = new GeneratePresignedUriRequest(_bucketName, targetObject, SignHttpMethod.Head)
         {
             ContentType = "text/rtf",
             Expiration  = DateTime.Now.AddHours(1),
         };
         gpuRequest.UserMetadata.Add("Author", "oss");
         gpuRequest.UserMetadata.Add("Test", "test");
         gpuRequest.AddQueryParam("x-param-null", "");
         gpuRequest.AddQueryParam("x-param-space0", " ");
         gpuRequest.AddQueryParam("x-param-value", "value");
         gpuRequest.AddQueryParam("x-param-space1", " ");
         var uri = _ossClient.GeneratePresignedUri(gpuRequest);
         Assert.IsTrue(false);
     }
     catch
     {
         Assert.IsTrue(true);
     }
 }
 public void InvalidObjectKeyTest()
 {
     foreach (var invalidKeyName in OssTestUtils.InvalidObjectKeyNamesList)
     {
         try
         {
             //try to upload the object with invalid key name
             OssTestUtils.UploadObject(_ossClient, _bucketName, invalidKeyName,
                                       Config.UploadSampleFile, new ObjectMetadata());
             Assert.Fail("Upload should not pass for invalid object key name {0}", invalidKeyName);
         }
         catch (ArgumentException)
         {
             Assert.IsTrue(true);
         }
         finally
         {
             if (OssTestUtils.ObjectExists(_ossClient, _bucketName, invalidKeyName))
             {
                 _ossClient.DeleteObject(_bucketName, invalidKeyName);
             }
         }
     }
 }
        public void ModifyObjectMetaWithAB2C()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            try
            {
                var meta = new ObjectMetadata()
                {
                    ContentType  = "text/rtf",
                    CacheControl = "public"
                };

                _ossClient.PutObject(_bucketName, key, Config.UploadSampleFile, meta);
                Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, key));

                var oldMeta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual(meta.ContentType, oldMeta.ContentType);
                Assert.AreEqual(meta.CacheControl, oldMeta.CacheControl);

                _ossClient.ModifyObjectMeta(_bucketName, key, new ObjectMetadata()
                {
                    ContentType = "application/vnd.wap.wmlc"
                });

                var newMeta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual("application/vnd.wap.wmlc", newMeta.ContentType);
                Assert.AreEqual(null, newMeta.CacheControl);
            }
            finally
            {
                if (OssTestUtils.ObjectExists(_ossClient, _bucketName, key))
                {
                    _ossClient.DeleteObject(_bucketName, key);
                }
            }
        }
        public void GetObjectUsingRangeTest()
        {
            var targetFile = OssTestUtils.GetTargetFileName(_className);

            targetFile = Path.Combine(Config.DownloadFolder, targetFile);
            try
            {
                OssTestUtils.DownloadObjectUsingRange(_ossClient, _bucketName, _objectKey, targetFile);
                var expectedETag       = _ossClient.GetObjectMetadata(_bucketName, _objectKey).ETag;
                var downloadedFileETag = FileUtils.ComputeContentMd5(targetFile);
                Assert.AreEqual(expectedETag.ToLowerInvariant(), downloadedFileETag.ToLowerInvariant());
            }
            finally
            {
                try
                {
                    FileUtils.DeleteFile(targetFile);
                }
                catch (Exception e)
                {
                    LogUtility.LogWarning("Delete file {0} failed with Exception {1}", targetFile, e.Message);
                }
            }
        }
        public void CreateAndDeleteBucketDefaultRegionTest()
        {
            var settings = AccountSettings.Load();
            //point to default region
            var ossClient = new OssClient(settings.OssEndpoint, settings.OssAccessKeyId, settings.OssAccessKeySecret);

            //get a random bucketName
            var bucketName = OssTestUtils.GetBucketName(_className);

            //assert bucket does not exist
            Assert.IsFalse(OssTestUtils.BucketExists(ossClient, bucketName),
                           string.Format("Bucket {0} should not exist before creation", bucketName));

            //create a new bucket
            ossClient.CreateBucket(bucketName);
            Assert.IsTrue(ossClient.DoesBucketExist(bucketName),
                          string.Format("Bucket {0} should exist after creation", bucketName));

            //delete the bucket
            ossClient.DeleteBucket(bucketName);
            OssTestUtils.WaitForCacheExpire();
            Assert.IsFalse(ossClient.DoesBucketExist(bucketName),
                           string.Format("Bucket {0} should not exist after deletion", bucketName));
        }
        public void PutObjectWithPreSignedUriWithoutCrc()
        {
            var testStr = FileUtils.GenerateOneKb();
            var bytes   = Encoding.ASCII.GetBytes(testStr);

            var now = DateTime.Now;
            //set expiration time to 5 seconds later
            var expireDate   = now.AddSeconds(5);
            var targetObject = OssTestUtils.GetObjectKey(_className);
            var uri          = _ossClient.GeneratePresignedUri(_bucketName, targetObject, expireDate, SignHttpMethod.Put);

            try
            {
                Common.ClientConfiguration config = new Common.ClientConfiguration();
                config.EnableCrcCheck = false;
                IOss ossClient = OssClientFactory.CreateOssClient(config);
                var  putResult = ossClient.PutObject(uri, new MemoryStream(bytes));
                Assert.AreEqual(putResult.HttpStatusCode, HttpStatusCode.OK);
            }
            catch (WebException ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        public void SetBucketAclUseRequestTest()
        {
            _ossClient.SetBucketAcl(
                new SetBucketAclRequest(_bucketName, CannedAccessControlList.PublicRead));
            OssTestUtils.WaitForCacheExpire();
            var acl = _ossClient.GetBucketAcl(_bucketName);

            Assert.AreEqual(acl.ACL, CannedAccessControlList.PublicRead);

            //set to readwrite
            _ossClient.SetBucketAcl(
                new SetBucketAclRequest(_bucketName, CannedAccessControlList.PublicReadWrite));
            OssTestUtils.WaitForCacheExpire();
            acl = _ossClient.GetBucketAcl(_bucketName);
            Assert.AreEqual(acl.ACL, CannedAccessControlList.PublicReadWrite);

            //set to private
            _ossClient.SetBucketAcl(
                new SetBucketAclRequest(_bucketName, CannedAccessControlList.Private));
            OssTestUtils.WaitForCacheExpire();
            acl = _ossClient.GetBucketAcl(_bucketName);
            Assert.AreEqual(acl.ACL, CannedAccessControlList.Private);
            Assert.AreEqual(acl.ToString().IndexOf("ACL=Private") != -1, true);
        }
        public void SetBucketRefersPositiveTest()
        {
            //initialize refer list
            var referList = new List <string>
            {
                "http://*.aliyun.com", "http://wwww.alibaba.com"
            };
            //use construct which pass in 3 parameters
            var sbrRequest = new SetBucketRefererRequest(_bucketName, referList, false);

            _ossClient.SetBucketReferer(sbrRequest);
            OssTestUtils.WaitForCacheExpire();
            var referConfig = _ossClient.GetBucketReferer(_bucketName);

            Assert.AreEqual(2, referConfig.RefererList.Referers.Length);
            Assert.IsFalse(referConfig.AllowEmptyReferer);


            referList.Add("http://www.taobao?.com");
            //use construct which pass in 2 parameters, and allowEmptyRefer set to true
            sbrRequest = new SetBucketRefererRequest(_bucketName, referList);
            _ossClient.SetBucketReferer(sbrRequest);
            OssTestUtils.WaitForCacheExpire();
            referConfig = _ossClient.GetBucketReferer(_bucketName);
            Assert.AreEqual(3, referConfig.RefererList.Referers.Length);
            //it is true this time
            Assert.IsTrue(referConfig.AllowEmptyReferer);

            //use construct which pass in 1 parameter, which means set it back to init status
            sbrRequest = new SetBucketRefererRequest(_bucketName);
            _ossClient.SetBucketReferer(sbrRequest);
            OssTestUtils.WaitForCacheExpire();
            referConfig = _ossClient.GetBucketReferer(_bucketName);
            Assert.IsNull(referConfig.RefererList.Referers);
            Assert.IsTrue(referConfig.AllowEmptyReferer);
        }
Beispiel #11
0
        public void EnableLoggingNonExistTargetBucketNameTest()
        {
            //generate the target bucket name
            var targetBucketName = OssTestUtils.GetBucketName(_className);

            //target bucket should not exist
            Assert.IsFalse(OssTestUtils.BucketExists(_ossClient, targetBucketName));

            var sblRequest = new SetBucketLoggingRequest(_bucketName, targetBucketName, LogPrefix);

            try
            {
                _ossClient.SetBucketLogging(sblRequest);
                Assert.Fail("Set Bucket logging should not pass with non exist target bucket name");
            }
            catch (OssException e)
            {
                Assert.AreEqual(OssErrorCode.InvalidTargetBucketForLogging, e.ErrorCode);
            }
            finally
            {
                _ossClient.DeleteBucketLogging(_bucketName);
            }
        }
Beispiel #12
0
        public void ProxyAuthTimeoutTest()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            try
            {
                //put object
                _ossClient.PutObject(_bucketName, key, Config.UploadTestFile);
                Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, key));

                OssTestUtils.WaitForCacheExpire();

                //list object
                var allObjects     = _ossClient.ListObjects(_bucketName, key);
                var allObjectsSumm = OssTestUtils.ToArray <OssObjectSummary>(allObjects.ObjectSummaries);
                Assert.AreEqual(1, allObjectsSumm.Count);

                OssTestUtils.WaitForCacheExpire();

                //list object
                allObjects     = _ossClient.ListObjects(_bucketName, key);
                allObjectsSumm = OssTestUtils.ToArray <OssObjectSummary>(allObjects.ObjectSummaries);
                Assert.AreEqual(1, allObjectsSumm.Count);

                OssTestUtils.WaitForCacheExpire();

                //list object
                allObjects     = _ossClient.ListObjects(_bucketName, key);
                allObjectsSumm = OssTestUtils.ToArray <OssObjectSummary>(allObjects.ObjectSummaries);
                Assert.AreEqual(1, allObjectsSumm.Count);
            }
            finally
            {
                _ossClient.DeleteObject(_bucketName, key);
            }
        }
Beispiel #13
0
        public void MultipartUploadCallbackVarTest()
        {
            // Initiate a multipart upload
            var initRequest = new InitiateMultipartUploadRequest(_bucketName, _bigObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // Sets the part size as 1M
            const int partSize = 1024 * 1024 * 1;
            var       partFile = new FileInfo(Config.MultiUploadTestFile);
            // 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, _bigObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    var uploadPartResult = _ossClient.UploadPart(uploadPartRequest);

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

            string callbackHeaderBuilder         = new CallbackHeaderBuilder(_callbackUrl, _callbackBody).Build();
            string CallbackVariableHeaderBuilder = new CallbackVariableHeaderBuilder().
                                                   AddCallbackVariable("x:var1", "x:value1").AddCallbackVariable("x:var2", "x:value2").Build();
            var metadata = new ObjectMetadata();

            metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder);
            metadata.AddHeader(HttpHeaders.CallbackVar, CallbackVariableHeaderBuilder);

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

            completeRequest.Metadata = metadata;
            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }
            var completeMultipartUploadResult = _ossClient.CompleteMultipartUpload(completeRequest);

            Assert.IsTrue(completeMultipartUploadResult.IsSetResponseStream());
            Assert.AreEqual(_callbackOkResponse, GetCallbackResponse(completeMultipartUploadResult));
            Assert.AreEqual(completeMultipartUploadResult.HttpStatusCode, HttpStatusCode.OK);
            Assert.AreEqual(completeMultipartUploadResult.ContentLength, _callbackOkResponse.Length);
            Assert.AreEqual(completeMultipartUploadResult.RequestId.Length, "58DB0ACB686D42D5B4163D75".Length);
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ContentType], "application/json");
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ETag], completeMultipartUploadResult.ETag);
            Assert.IsFalse(completeMultipartUploadResult.ResponseMetadata.ContainsKey(HttpHeaders.ContentMd5));
            Assert.IsTrue(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.HashCrc64Ecma].Length > 0);
            Assert.IsTrue(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ServerElapsedTime].Length > 0);
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.Date].Length, "Wed, 29 Mar 2017 01:15:58 GMT".Length);

            //delete the object
            _ossClient.DeleteObject(_bucketName, _bigObjectKey);
        }
Beispiel #14
0
        public void CompleteMultipartUploadWithListParts()
        {
            var sourceFile = Config.MultiUploadTestFile;
            //get target object name
            var targetObjectKey = OssTestUtils.GetObjectKey(_className);

            var initRequest = new InitiateMultipartUploadRequest(_bucketName, targetObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // Set the part size
            const int partSize = 1024 * 1024 * 1;

            var partFile = new FileInfo(sourceFile);
            // Calculate the part count
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            LogUtility.LogMessage("File {0} is splitted to {1} parts for multipart upload",
                                  sourceFile, partCount);

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

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

                    // Create a UploadPartRequest, uploading parts
                    var uploadPartRequest = new UploadPartRequest(_bucketName, targetObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    _ossClient.UploadPart(uploadPartRequest);
                }
            }

            var    lmuRequest = new ListMultipartUploadsRequest(_bucketName);
            var    lmuListing = _ossClient.ListMultipartUploads(lmuRequest);
            string mpUpload   = null;

            foreach (var t in lmuListing.MultipartUploads)
            {
                if (t.UploadId == initResult.UploadId)
                {
                    mpUpload = t.UploadId;
                }
            }

            Assert.IsNotNull(mpUpload, "The multipart uploading should be in progress");

            PartListing partList = _ossClient.ListParts(new ListPartsRequest(_bucketName, targetObjectKey, mpUpload));

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

            foreach (var part in partList.Parts)
            {
                completeRequest.PartETags.Add(part.PartETag);
            }
            _ossClient.CompleteMultipartUpload(completeRequest);

            Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, targetObjectKey));

            //delete the object
            _ossClient.DeleteObject(_bucketName, targetObjectKey);
        }
Beispiel #15
0
        public void MultipartUploadPartCopyComplexStepTest()
        {
            //get target object name
            var targetObjectKey = OssTestUtils.GetObjectKey(_className);

            var initRequest = new InitiateMultipartUploadRequest(_bucketName, targetObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // Set the part size
            const int partSize = 1024 * 512 * 1;

            var sourceObjectMeta = _ossClient.GetObjectMetadata(_bucketName, _sourceObjectKey);
            // Calculate the part count
            var partCount = OssTestUtils.CalculatePartCount(sourceObjectMeta.ContentLength, partSize);

            LogUtility.LogMessage("Object {0} is splitted to {1} parts for multipart upload part copy",
                                  _sourceObjectKey, partCount);

            // Create a list to save result
            var partETags = new List <PartETag>();

            for (var i = 0; i < partCount; i++)
            {
                // Skip to the start position
                long skipBytes = partSize * i;

                // calculate the part size
                var size = partSize < sourceObjectMeta.ContentLength - skipBytes
                    ? partSize
                    : sourceObjectMeta.ContentLength - skipBytes;

                // Create a UploadPartRequest, uploading parts
                var uploadPartCopyRequest =
                    new UploadPartCopyRequest(_bucketName, targetObjectKey, _bucketName, _sourceObjectKey, initResult.UploadId)
                {
                    BeginIndex = skipBytes,
                    PartSize   = size,
                    PartNumber = (i + 1),
                    ModifiedSinceConstraint = DateTime.Now.AddDays(-1)
                };
                uploadPartCopyRequest.MatchingETagConstraints.Add(_objectETag);
                var uploadPartCopyResult = _ossClient.UploadPartCopy(uploadPartCopyRequest);

                // Save the result
                partETags.Add(uploadPartCopyResult.PartETag);
            }

            var lmuRequest = new ListMultipartUploadsRequest(_bucketName);
            var lmuListing = _ossClient.ListMultipartUploads(lmuRequest);

            string mpUpload = null;

            foreach (var t in lmuListing.MultipartUploads)
            {
                if (t.UploadId == initResult.UploadId)
                {
                    mpUpload = t.UploadId;
                    break;
                }
            }

            Assert.IsNotNull(mpUpload, "The multipart uploading should be in progress");

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

            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }
            _ossClient.CompleteMultipartUpload(completeRequest);

            Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, targetObjectKey));

            //delete the object
            _ossClient.DeleteObject(_bucketName, targetObjectKey);
        }
Beispiel #16
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 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);
        }
Beispiel #18
0
        public void MultipartUploadWithoutCallbackTest()
        {
            // initiates a multipart upload
            var initRequest = new InitiateMultipartUploadRequest(_bucketName, _bigObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            Assert.AreEqual(initResult.HttpStatusCode, HttpStatusCode.OK);
            Assert.IsTrue(initResult.ContentLength > 0);
            Assert.AreEqual(initResult.RequestId.Length, "58DB0ACB686D42D5B4163D75".Length);
            Assert.AreEqual(initResult.ResponseMetadata[HttpHeaders.ContentType], "application/xml");
            Assert.IsFalse(initResult.ResponseMetadata.ContainsKey(HttpHeaders.ETag));
            Assert.IsFalse(initResult.ResponseMetadata.ContainsKey(HttpHeaders.ContentMd5));
            Assert.IsFalse(initResult.ResponseMetadata.ContainsKey(HttpHeaders.HashCrc64Ecma));
            Assert.IsTrue(initResult.ResponseMetadata[HttpHeaders.ServerElapsedTime].Length > 0);
            Assert.AreEqual(initResult.ResponseMetadata[HttpHeaders.Date].Length, "Wed, 29 Mar 2017 01:15:58 GMT".Length);

            // Sets part size 1M
            const int partSize = 1024 * 1024 * 1;
            var       partFile = new FileInfo(Config.MultiUploadTestFile);
            // calculates the part count
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            // creats the list for 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++)
                {
                    // Skips to the start position of each part
                    long skipBytes = partSize * i;
                    fs.Position = skipBytes;

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

                    // creates a UploadPartRequest, uploading parts
                    var uploadPartRequest = new UploadPartRequest(_bucketName, _bigObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    var uploadPartResult = _ossClient.UploadPart(uploadPartRequest);
                    Assert.AreEqual(uploadPartResult.HttpStatusCode, HttpStatusCode.OK);
                    Assert.AreEqual(uploadPartResult.ContentLength, 0);
                    Assert.AreEqual(uploadPartResult.RequestId.Length, "58DB0ACB686D42D5B4163D75".Length);
                    Assert.IsFalse(uploadPartResult.ResponseMetadata.ContainsKey(HttpHeaders.ContentType));
                    Assert.AreEqual(uploadPartResult.ResponseMetadata[HttpHeaders.ETag], uploadPartResult.ETag);
                    Assert.AreEqual(uploadPartResult.ResponseMetadata[HttpHeaders.ContentMd5].Length, "7GoU4OYaYroKXsbsG1f/lw==".Length);
                    Assert.IsTrue(uploadPartResult.ResponseMetadata[HttpHeaders.ServerElapsedTime].Length > 0);
                    Assert.AreEqual(uploadPartResult.ResponseMetadata[HttpHeaders.Date].Length, "Wed, 29 Mar 2017 01:15:58 GMT".Length);

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

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

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

            Assert.IsFalse(completeMultipartUploadResult.IsSetResponseStream());
            Assert.AreEqual(completeMultipartUploadResult.HttpStatusCode, HttpStatusCode.OK);
            Assert.IsTrue(completeMultipartUploadResult.ContentLength > 0);
            Assert.AreEqual(completeMultipartUploadResult.RequestId.Length, "58DB0ACB686D42D5B4163D75".Length);
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ContentType], "application/xml");
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ETag], completeMultipartUploadResult.ETag);
            Assert.IsFalse(completeMultipartUploadResult.ResponseMetadata.ContainsKey(HttpHeaders.ContentMd5));
            Assert.IsTrue(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.HashCrc64Ecma].Length > 0);
            Assert.IsTrue(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ServerElapsedTime].Length > 0);
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.Date].Length, "Wed, 29 Mar 2017 01:15:58 GMT".Length);

            //delete the object
            _ossClient.DeleteObject(_bucketName, _bigObjectKey);
        }
        public void MultipartUploadComplexStepTest()
        {
            var sourceFile = Config.MultiUploadTestFile;
            //get target object name
            var targetObjectKey = OssTestUtils.GetObjectKey(_className);

            var initRequest = new InitiateMultipartUploadRequest(_bucketName, targetObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // 设置每块为 1M
            const int partSize = 1024 * 1024 * 1;

            var partFile = new FileInfo(sourceFile);
            // 计算分块数目
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            LogUtility.LogMessage("File {0} is splitted to {1} parts for multipart upload",
                                  sourceFile, partCount);

            // 新建一个List保存每个分块上传后的ETag和PartNumber
            var partETags = new List <PartETag>();

            //upload the file
            using (var fs = new FileStream(partFile.FullName, FileMode.Open))
            {
                for (var i = 0; i < partCount; i++)
                {
                    // 跳到每个分块的开头
                    long skipBytes = partSize * i;
                    fs.Position = skipBytes;

                    // 计算每个分块的大小
                    var size = partSize < partFile.Length - skipBytes
                        ? partSize
                        : partFile.Length - skipBytes;

                    // 创建UploadPartRequest,上传分块
                    var uploadPartRequest = new UploadPartRequest(_bucketName, targetObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    var uploadPartResult = _ossClient.UploadPart(uploadPartRequest);

                    // 将返回的PartETag保存到List中。
                    partETags.Add(uploadPartResult.PartETag);
                }
            }

            var    lmuRequest = new ListMultipartUploadsRequest(_bucketName);
            var    lmuListing = _ossClient.ListMultipartUploads(lmuRequest);
            string mpUpload   = null;

            foreach (var t in lmuListing.MultipartUploads)
            {
                if (t.UploadId == initResult.UploadId)
                {
                    mpUpload = t.UploadId;
                }
            }

            Assert.IsNotNull(mpUpload, "The multipart uploading should be in progress");

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

            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }
            _ossClient.CompleteMultipartUpload(completeRequest);

            Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, targetObjectKey));

            //delete the object
            _ossClient.DeleteObject(_bucketName, targetObjectKey);
        }
        public void ObjectBasicTest()
        {
            //Get Case
            var key = OssTestUtils.GetObjectKey(_className);

            _ossClient.PutObject(_bucketName, key, new MemoryStream(Encoding.ASCII.GetBytes("hello world")));

            var getRequest = new GetObjectRequest(_bucketName, key);

            Assert.AreEqual(getRequest.RequestPayer, RequestPayer.BucketOwner);

            try
            {
                _ossPayerClient.GetObject(getRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            getRequest.RequestPayer = RequestPayer.Requester;
            var getResult = _ossPayerClient.GetObject(getRequest);

            Assert.AreEqual(getResult.ContentLength, 11);
            _ossClient.DeleteObject(_bucketName, key);

            //Put Case
            key = OssTestUtils.GetObjectKey(_className);
            var content    = new MemoryStream(Encoding.ASCII.GetBytes("hello world"));
            var putRequest = new PutObjectRequest(_bucketName, key, content);

            Assert.AreEqual(putRequest.RequestPayer, RequestPayer.BucketOwner);

            try
            {
                _ossPayerClient.PutObject(putRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            content    = new MemoryStream(Encoding.ASCII.GetBytes("hello world"));
            putRequest = new PutObjectRequest(_bucketName, key, content);
            putRequest.RequestPayer = RequestPayer.Requester;
            var putResult = _ossPayerClient.PutObject(putRequest);

            Assert.AreEqual(putResult.HttpStatusCode, HttpStatusCode.OK);

            //head object
            var headRequest = new GetObjectMetadataRequest(_bucketName, key);

            Assert.AreEqual(headRequest.RequestPayer, RequestPayer.BucketOwner);

            try
            {
                _ossPayerClient.GetObjectMetadata(headRequest);
                Assert.Fail("should not here.");
            }
            catch (Exception e)
            {
                Assert.IsTrue(true, e.Message);
            }

            headRequest.RequestPayer = RequestPayer.Requester;
            var headResult = _ossPayerClient.GetObjectMetadata(headRequest);

            Assert.AreEqual(headResult.ContentLength, 11);

            //Delete Case
            Assert.IsTrue(_ossClient.DoesObjectExist(_bucketName, key));

            var delReqeust = new DeleteObjectRequest(_bucketName, key);

            Assert.AreEqual(delReqeust.RequestPayer, RequestPayer.BucketOwner);

            try
            {
                _ossPayerClient.DeleteObject(delReqeust);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            delReqeust = new DeleteObjectRequest(_bucketName, key);
            delReqeust.RequestPayer = RequestPayer.Requester;
            _ossPayerClient.DeleteObject(delReqeust);

            Assert.IsFalse(_ossClient.DoesObjectExist(_bucketName, key));

            //delete objects
            var keys = new List <string>();

            keys.Add(key);
            var delsReqeust = new DeleteObjectsRequest(_bucketName, keys);

            Assert.AreEqual(delsReqeust.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                _ossPayerClient.DeleteObjects(delsReqeust);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            delsReqeust.RequestPayer = RequestPayer.Requester;
            _ossPayerClient.DeleteObjects(delsReqeust);

            //delete object versions
            var objects = new List <ObjectIdentifier>();

            objects.Add(new ObjectIdentifier(key));
            var delvsReqeust = new DeleteObjectVersionsRequest(_bucketName, objects);

            Assert.AreEqual(delvsReqeust.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                _ossPayerClient.DeleteObjectVersions(delvsReqeust);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            delvsReqeust.RequestPayer = RequestPayer.Requester;
            _ossPayerClient.DeleteObjectVersions(delvsReqeust);


            //list objets
            var lsRequest = new ListObjectsRequest(_bucketName);

            Assert.AreEqual(lsRequest.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                _ossPayerClient.ListObjects(lsRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            lsRequest.RequestPayer = RequestPayer.Requester;
            _ossPayerClient.ListObjects(lsRequest);
        }
        public void AppendObjectTest()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            var request = new AppendObjectRequest(_bucketName, key);

            Assert.AreEqual(request.RequestPayer, RequestPayer.BucketOwner);

            try
            {
                request = new AppendObjectRequest(_bucketName, key)
                {
                    Content  = new MemoryStream(Encoding.ASCII.GetBytes("hello world")),
                    Position = 0
                };
                var result = _ossPayerClient.AppendObject(request);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            try
            {
                long position = 0;
                using (var fs = File.Open(Config.UploadTestFile, FileMode.Open))
                {
                    var fileLength = fs.Length;
                    request = new AppendObjectRequest(_bucketName, key)
                    {
                        Content      = fs,
                        Position     = position,
                        RequestPayer = RequestPayer.Requester
                    };

                    var result = _ossPayerClient.AppendObject(request);
                    Assert.AreEqual(fileLength, result.NextAppendPosition);
                    position = result.NextAppendPosition;
                }

                using (var fs = File.Open(Config.UploadTestFile, FileMode.Open))
                {
                    var fileLength = fs.Length;
                    request = new AppendObjectRequest(_bucketName, key)
                    {
                        Content      = fs,
                        Position     = position,
                        RequestPayer = RequestPayer.Requester
                    };

                    var result = _ossPayerClient.AppendObject(request);
                    Assert.AreEqual(fileLength * 2, result.NextAppendPosition);
                    Assert.IsTrue(result.HashCrc64Ecma != 0);
                }

                var meta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual("Appendable", meta.ObjectType);
            }
            finally
            {
                if (OssTestUtils.ObjectExists(_ossClient, _bucketName, key))
                {
                    _ossClient.DeleteObject(_bucketName, key);
                }
            }
        }
        public void MultipartUploadCopyTest()
        {
            //get target object name
            var sourceObjectKey = OssTestUtils.GetObjectKey(_className);
            var targetObjectKey = OssTestUtils.GetObjectKey(_className);

            OssTestUtils.UploadObject(_ossClient, _bucketName, sourceObjectKey, Config.UploadTestFile);

            var initRequest = new InitiateMultipartUploadRequest(_bucketName, targetObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // Set the part size
            const int partSize = 100 * 1024;

            var sourceObjectMeta = _ossClient.GetObjectMetadata(_bucketName, sourceObjectKey);
            // Calculate the part count
            var partCount = OssTestUtils.CalculatePartCount(sourceObjectMeta.ContentLength, partSize);

            // Create a list to save result
            var partETags = new List <PartETag>();

            for (var i = 0; i < partCount; i++)
            {
                // Skip to the start position
                long skipBytes = partSize * i;

                // calculate the part size
                var size = partSize < sourceObjectMeta.ContentLength - skipBytes
                    ? partSize
                    : sourceObjectMeta.ContentLength - skipBytes;

                // Create a UploadPartRequest, uploading parts
                var uploadPartCopyRequest =
                    new UploadPartCopyRequest(_bucketName, targetObjectKey, _bucketName, sourceObjectKey, initResult.UploadId)
                {
                    BeginIndex = skipBytes,
                    PartSize   = size,
                    PartNumber = (i + 1),
                    ModifiedSinceConstraint = DateTime.Now.AddDays(-1),
                    RequestPayer            = RequestPayer.Requester
                };
                uploadPartCopyRequest.MatchingETagConstraints.Add(sourceObjectMeta.ETag);
                var uploadPartCopyResult = _ossPayerClient.UploadPartCopy(uploadPartCopyRequest);

                // Save the result
                partETags.Add(uploadPartCopyResult.PartETag);
            }

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

            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }
            _ossClient.CompleteMultipartUpload(completeRequest);

            var targetObjectMeta = _ossClient.GetObjectMetadata(_bucketName, targetObjectKey);

            Assert.AreEqual(targetObjectMeta.Crc64, sourceObjectMeta.Crc64);

            //delete the object
            _ossClient.DeleteObject(_bucketName, targetObjectKey);
        }
Beispiel #23
0
        public void MultipartUploadCallbackUriInvalidNegativeTest()
        {
            // initiates a multipart upload
            var initRequest = new InitiateMultipartUploadRequest(_bucketName, _bigObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // set part size 1MB
            const int partSize = 1024 * 1024 * 1;
            var       partFile = new FileInfo(Config.MultiUploadTestFile);
            // sets part count
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            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 start position of each part
                    long skipBytes = partSize * i;
                    fs.Position = skipBytes;

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

                    // creates a UploadPartRequest, uploading parts
                    var uploadPartRequest = new UploadPartRequest(_bucketName, _bigObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    var uploadPartResult = _ossClient.UploadPart(uploadPartRequest);

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

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

            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }

            string callbackHeaderBuilder = new CallbackHeaderBuilder("https://wwww.aliyun.com/", _callbackBody).Build();
            var    metadata = new ObjectMetadata();

            metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder);
            completeRequest.Metadata = metadata;

            try
            {
                _ossClient.CompleteMultipartUpload(completeRequest);
                Assert.Fail("Multipart upload callback should be not successfully with invald callback uri");
            }
            catch (OssException e)
            {
                Assert.AreEqual(OssErrorCode.CallbackFailed, e.ErrorCode);
                Assert.AreEqual("Error status : 502.", e.Message);
            }
            finally
            {
                _ossClient.DeleteObject(_bucketName, _bigObjectKey);
            }
        }
        public void MultipartUploadNegativeTest()
        {
            var key = OssTestUtils.GetObjectKey(_className);

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

            Assert.AreEqual(initRequest.RequestPayer, RequestPayer.BucketOwner);

            try
            {
                var initResult = _ossPayerClient.InitiateMultipartUpload(initRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            // uploads the part
            var uploadPartRequest = new UploadPartRequest(_bucketName, key, "initId")
            {
                InputStream = new MemoryStream(Encoding.ASCII.GetBytes("hello world")),
                PartSize    = 11,
                PartNumber  = 1
            };

            Assert.AreEqual(uploadPartRequest.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                var uploadPartResult = _ossPayerClient.UploadPart(uploadPartRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            //list part
            var listpartRequest = new ListPartsRequest(_bucketName, key, "initId");

            Assert.AreEqual(listpartRequest.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                var listPartResult = _ossPayerClient.ListParts(listpartRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            //abort
            var abortRequest = new AbortMultipartUploadRequest(_bucketName, key, "initId");

            Assert.AreEqual(abortRequest.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                _ossPayerClient.AbortMultipartUpload(abortRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            //complete
            var completeRequest = new CompleteMultipartUploadRequest(_bucketName, key, "initId");

            Assert.AreEqual(completeRequest.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                var listPartResult = _ossPayerClient.CompleteMultipartUpload(completeRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            //list upload
            var listUploadsRequest = new ListMultipartUploadsRequest(_bucketName);

            Assert.AreEqual(listUploadsRequest.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                var listUploadsResult = _ossPayerClient.ListMultipartUploads(listUploadsRequest);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }

            //uploadpart copy
            var uploadpartcopyRequet = new UploadPartCopyRequest(_bucketName, key, _bucketName, key, "uploadId")
            {
                BeginIndex = 0,
                PartNumber = 1,
                PartSize   = 100 * 1024
            };

            Assert.AreEqual(uploadpartcopyRequet.RequestPayer, RequestPayer.BucketOwner);
            try
            {
                var uploadpartcopyResult = _ossPayerClient.UploadPartCopy(uploadpartcopyRequet);
                Assert.Fail("should not here.");
            }
            catch (OssException e)
            {
                Assert.AreEqual(e.ErrorCode, "AccessDenied");
            }
        }
        public void MultipartUploadProgressTest()
        {
            // Initiate a multipart upload
            var initRequest = new InitiateMultipartUploadRequest(_bucketName, _bigObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // Set the part size
            const int partSize = 1024 * 1024 * 1;
            var       partFile = new FileInfo(Config.MultiUploadTestFile);
            // Calculate the part count
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            // Create a list to save the result
            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 position
                    long skipBytes = partSize * i;
                    fs.Position = skipBytes;

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

                    // Create a UploadPartRequest, uploading the part
                    var uploadPartRequest = new UploadPartRequest(_bucketName, _bigObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    uploadPartRequest.StreamTransferProgress += uploadProgressCallback;
                    var uploadPartResult = _ossClient.UploadPart(uploadPartRequest);

                    // Save the result
                    partETags.Add(uploadPartResult.PartETag);
                }
            }

            // Complete the multipart upload
            var completeRequest = new CompleteMultipartUploadRequest(_bucketName, _bigObjectKey, initResult.UploadId);

            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }
            _ossClient.CompleteMultipartUpload(completeRequest);

            // Download the file to check the hash digest
            OssTestUtils.DownloadObject(_ossClient, _bucketName, _bigObjectKey, _tmpLocalFile);
            var expectedHashDigest = FileUtils.ComputeContentMd5(Config.MultiUploadTestFile);
            var actualHashDigest   = FileUtils.ComputeContentMd5(_tmpLocalFile);

            Assert.AreEqual(expectedHashDigest, actualHashDigest);

            _ossClient.DeleteObject(_bucketName, _bigObjectKey);
            File.Delete(_tmpLocalFile);
        }
Beispiel #26
0
        public void ResumableUploadDownloadTest()
        {
            var filePath = OssTestUtils.GetTargetFileName(_className);

            filePath = Path.Combine(Config.DownloadFolder, filePath);
            FileUtils.PrepareSampleFile(filePath, 800);

            var fileName = filePath;
            var fileInfo = new FileInfo(fileName);
            var fileSize = fileInfo.Length;

            //  < PartSize
            var key     = OssTestUtils.GetObjectKey(_className);
            var request = new UploadObjectRequest(_bucketName, key, fileName)
            {
                PartSize = fileSize + 1,
            };

            Assert.AreEqual(request.TrafficLimit, 0);
            DateTime beforDT = System.DateTime.Now;
            var      result  = _ossClient.ResumableUploadObject(request);
            DateTime afterDT = System.DateTime.Now;
            TimeSpan ts      = afterDT.Subtract(beforDT);

            Assert.IsTrue(ts.TotalSeconds < 2);

            request = new UploadObjectRequest(_bucketName, key, fileName)
            {
                PartSize     = fileSize + 1,
                TrafficLimit = 100 * 1024 * 8
            };
            Assert.AreEqual(request.TrafficLimit, 819200);
            beforDT = System.DateTime.Now;
            result  = _ossClient.ResumableUploadObject(request);
            afterDT = System.DateTime.Now;
            ts      = afterDT.Subtract(beforDT);
            Assert.IsTrue(ts.TotalSeconds > 6);


            // > PartSize with single thread
            key     = OssTestUtils.GetObjectKey(_className);
            request = new UploadObjectRequest(_bucketName, key, fileName)
            {
                PartSize            = 400 * 1024,
                ParallelThreadCount = 1
            };
            Assert.AreEqual(request.TrafficLimit, 0);
            beforDT = DateTime.Now;
            result  = _ossClient.ResumableUploadObject(request);
            afterDT = DateTime.Now;
            ts      = afterDT.Subtract(beforDT).Duration();
            Assert.IsTrue(ts.TotalSeconds < 3);

            key     = OssTestUtils.GetObjectKey(_className);
            request = new UploadObjectRequest(_bucketName, key, fileName)
            {
                PartSize            = 400 * 1024,
                ParallelThreadCount = 1,
                TrafficLimit        = 100 * 1024 * 8
            };
            Assert.AreEqual(request.TrafficLimit, 819200);
            beforDT = System.DateTime.Now;
            result  = _ossClient.ResumableUploadObject(request);
            afterDT = System.DateTime.Now;
            ts      = afterDT.Subtract(beforDT);
            Assert.IsTrue(ts.TotalSeconds > 6);


            // > PartSize with 2 thread
            key     = OssTestUtils.GetObjectKey(_className);
            request = new UploadObjectRequest(_bucketName, key, fileName)
            {
                PartSize            = 200 * 1024,
                ParallelThreadCount = 2
            };
            Assert.AreEqual(request.TrafficLimit, 0);
            beforDT = DateTime.Now;
            result  = _ossClient.ResumableUploadObject(request);
            afterDT = DateTime.Now;
            ts      = afterDT.Subtract(beforDT).Duration();
            Assert.IsTrue(ts.TotalSeconds < 2);

            key     = OssTestUtils.GetObjectKey(_className);
            request = new UploadObjectRequest(_bucketName, key, fileName)
            {
                PartSize            = 200 * 1024,
                ParallelThreadCount = 2,
                TrafficLimit        = 100 * 1024 * 8
            };
            Assert.AreEqual(request.TrafficLimit, 819200);
            beforDT = System.DateTime.Now;
            result  = _ossClient.ResumableUploadObject(request);
            afterDT = System.DateTime.Now;
            ts      = afterDT.Subtract(beforDT);
            Assert.IsTrue(ts.TotalSeconds > 6);


            //download
            var targetFile = OssTestUtils.GetTargetFileName(_className);

            targetFile = Path.Combine(Config.DownloadFolder, targetFile);

            //  < PartSize
            var dRequest = new DownloadObjectRequest(_bucketName, key, targetFile)
            {
                PartSize = fileSize + 1,
            };

            Assert.AreEqual(dRequest.TrafficLimit, 0);
            beforDT = System.DateTime.Now;
            var metadata = _ossClient.ResumableDownloadObject(dRequest);

            afterDT = System.DateTime.Now;
            ts      = afterDT.Subtract(beforDT);
            Assert.IsTrue(ts.TotalSeconds < 2);
            FileUtils.DeleteFile(targetFile);

            // > PartSize with multi thread
            dRequest = new DownloadObjectRequest(_bucketName, key, targetFile)
            {
                PartSize            = 200 * 1024,
                ParallelThreadCount = 2,
                TrafficLimit        = 100 * 1024 * 8
            };
            Assert.AreEqual(dRequest.TrafficLimit, 819200);
            afterDT  = System.DateTime.Now;
            metadata = _ossClient.ResumableDownloadObject(dRequest);
            afterDT  = System.DateTime.Now;
            ts       = afterDT.Subtract(beforDT);
            Assert.IsTrue(ts.TotalSeconds > 4);
            FileUtils.DeleteFile(targetFile);

            // > PartSize with single thread
            dRequest = new DownloadObjectRequest(_bucketName, key, targetFile)
            {
                PartSize            = 200 * 1024,
                ParallelThreadCount = 1,
                TrafficLimit        = 100 * 1024 * 8
            };
            Assert.AreEqual(dRequest.TrafficLimit, 819200);
            afterDT  = System.DateTime.Now;
            metadata = _ossClient.ResumableDownloadObject(dRequest);
            afterDT  = System.DateTime.Now;
            ts       = afterDT.Subtract(beforDT);
            Assert.IsTrue(ts.TotalSeconds > 6);
            FileUtils.DeleteFile(targetFile);


            //FileUtils.DeleteFile(filePath);
        }
        //[Test]
        public void ListMultipartUploadsWithHiddenCharacters()
        {
            char[] buffer = new char[2];
            buffer[0] = Convert.ToChar(0x1c);
            buffer[1] = Convert.ToChar(0x1a);

            var newKey = _keyName + (new string(buffer)) + ".1.cd";

            try
            {
                var initRequest = new InitiateMultipartUploadRequest(_bucketName, newKey);
                var initResult  = _ossClient.InitiateMultipartUpload(initRequest);
                Assert.AreEqual(newKey, initRequest.Key);

                // 设置每块为 5M
                const int partSize = 5 * 1024 * 1024;

                var fileInfo = new FileInfo(Config.MultiUploadTestFile);

                // 计算分块数目
                var partCount = OssTestUtils.CalculatePartCount(fileInfo.Length, partSize);

                // 新建一个List保存每个分块上传后的ETag和PartNumber
                var partETags = new List <PartETag>();

                //upload the file
                using (var fs = new FileStream(fileInfo.FullName, FileMode.Open))
                {
                    for (var i = 0; i < partCount; i++)
                    {
                        // 跳到每个分块的开头
                        long skipBytes = partSize * i;
                        fs.Position = skipBytes;

                        // 计算每个分块的大小
                        long size = partSize < fileInfo.Length - skipBytes ? partSize : fileInfo.Length - skipBytes;

                        // 创建UploadPartRequest,上传分块
                        var uploadPartRequest = new UploadPartRequest(_bucketName, newKey, initResult.UploadId);
                        uploadPartRequest.InputStream = fs;
                        uploadPartRequest.PartSize    = size;
                        uploadPartRequest.PartNumber  = (i + 1);
                        var uploadPartResult = _ossClient.UploadPart(uploadPartRequest);

                        // 将返回的PartETag保存到List中。
                        partETags.Add(uploadPartResult.PartETag);

                        var multipartListing = _ossClient.ListMultipartUploads(new ListMultipartUploadsRequest(_bucketName));
                        foreach (var upload in multipartListing.MultipartUploads)
                        {
                            Assert.AreEqual(newKey, upload.Key);
                        }
                    }
                }

                var completeRequest = new CompleteMultipartUploadRequest(_bucketName, newKey, initResult.UploadId);
                foreach (var partETag in partETags)
                {
                    completeRequest.PartETags.Add(partETag);
                }
                _ossClient.CompleteMultipartUpload(completeRequest);
            }
            finally
            {
                var multipartListing = _ossClient.ListMultipartUploads(new ListMultipartUploadsRequest(_bucketName));
                foreach (var upload in multipartListing.MultipartUploads)
                {
                    var abortRequest = new AbortMultipartUploadRequest(_bucketName, newKey, upload.UploadId);
                    _ossClient.AbortMultipartUpload(abortRequest);
                }
                _ossClient.DeleteObject(_bucketName, newKey);
            }
        }
        public void SetObjectTaggingTest()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            _ossClient.PutObject(_bucketName, key, new MemoryStream(Encoding.ASCII.GetBytes("hello world")));

            var setRequest = new SetObjectTaggingRequest(_bucketName, key);

            var tag1 = new Tag
            {
                Key   = "project",
                Value = "projectone"
            };

            var tag2 = new Tag
            {
                Key   = "user",
                Value = "jsmith"
            };

            setRequest.AddTag(tag1);
            setRequest.AddTag(tag2);

            _ossClient.SetObjectTagging(setRequest);

            var result = _ossClient.GetObjectTagging(_bucketName, key);

            Assert.AreEqual(result.Tags.Count, setRequest.Tags.Count);
            Assert.AreEqual(result.Tags[0].Key, setRequest.Tags[0].Key);
            Assert.AreEqual(result.Tags[0].Value, setRequest.Tags[0].Value);
            Assert.AreEqual(result.Tags[1].Key, setRequest.Tags[1].Key);
            Assert.AreEqual(result.Tags[1].Value, setRequest.Tags[1].Value);


            var tags = new List <Tag>();

            tags.Add(tag2);
            setRequest.Tags = tags;
            _ossClient.SetObjectTagging(setRequest);

            result = _ossClient.GetObjectTagging(_bucketName, key);
            Assert.AreEqual(result.Tags.Count, 1);
            Assert.AreEqual(result.Tags[0].Key, tag2.Key);
            Assert.AreEqual(result.Tags[0].Value, tag2.Value);


            tags = new List <Tag>();
            tags.Add(tag1);
            setRequest = new SetObjectTaggingRequest(_bucketName, key, tags);
            _ossClient.SetObjectTagging(setRequest);

            result = _ossClient.GetObjectTagging(_bucketName, key);
            Assert.AreEqual(result.Tags.Count, 1);
            Assert.AreEqual(result.Tags[0].Key, tag1.Key);
            Assert.AreEqual(result.Tags[0].Value, tag1.Value);


            _ossClient.DeleteObjectTagging(_bucketName, key);

            result = _ossClient.GetObjectTagging(_bucketName, key);
            Assert.AreEqual(result.Tags.Count, 0);
        }
 public static void ClassCleanup()
 {
     OssTestUtils.CleanBucket(_ossClient, _bucketName);
 }
Beispiel #30
0
        public void MultipartUploadDisableMD5CheckTest()
        {
            // 初始化分片上传任务
            var initRequest = new InitiateMultipartUploadRequest(_bucketName, _bigObjectKey);
            var initResult  = _ossClientDisableMD5.InitiateMultipartUpload(initRequest);

            // 设置每块为 1M
            const int partSize = 1024 * 1024 * 1;
            var       partFile = new FileInfo(Config.MultiUploadTestFile);
            // 计算分块数目
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            // 新建一个List保存每个分块上传后的ETag和PartNumber
            var partETags = new List <PartETag>();

            //upload the file
            using (var fs = new FileStream(partFile.FullName, FileMode.Open))
            {
                for (var i = 0; i < partCount; i++)
                {
                    // 跳到每个分块的开头
                    long skipBytes = partSize * i;
                    fs.Position = skipBytes;

                    // 计算每个分块的大小
                    var size = partSize < partFile.Length - skipBytes
                        ? partSize
                        : partFile.Length - skipBytes;

                    // 创建UploadPartRequest,上传分块
                    var uploadPartRequest = new UploadPartRequest(_bucketName, _bigObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    var uploadPartResult = _ossClientDisableMD5.UploadPart(uploadPartRequest);

                    // 将返回的PartETag保存到List中。
                    partETags.Add(uploadPartResult.PartETag);
                }
            }

            // 提交上传任务
            var completeRequest = new CompleteMultipartUploadRequest(_bucketName, _bigObjectKey, initResult.UploadId);

            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }
            _ossClientDisableMD5.CompleteMultipartUpload(completeRequest);

            // 校验文件内容
            OssTestUtils.DownloadObject(_ossClientDisableMD5, _bucketName, _bigObjectKey, _tmpLocalFile);
            var expectedHashDigest = FileUtils.ComputeContentMd5(Config.MultiUploadTestFile);
            var actualHashDigest   = FileUtils.ComputeContentMd5(_tmpLocalFile);

            Assert.AreEqual(expectedHashDigest, actualHashDigest);

            _ossClientDisableMD5.DeleteObject(_bucketName, _bigObjectKey);
            File.Delete(_tmpLocalFile);
        }