public static void ClassInitialize()
        {
            //get a OSS client object
            _ossClient           = OssClientFactory.CreateOssClient();
            _ossClientDisableMD5 = OssClientFactory.CreateOssClientEnableMD5(false);
            //get current class name, which is prefix of bucket/object
            _className = TestContext.CurrentContext.Test.FullName;
            _className = _className.Substring(_className.LastIndexOf('.') + 1).ToLowerInvariant();
            //create the bucket
            _bucketName = OssTestUtils.GetBucketName(_className);
            _ossClient.CreateBucket(_bucketName);

            //upload sample object
            _objectKey = OssTestUtils.GetObjectKey(_className);
            //upload multipart sample object
            _bigObjectKey = _objectKey + ".js";

            // temporary local file
            _tmpLocalFile = _className + ".tmp";
        }
        public void PutObjectWithProcessTest()
        {
            IOss client = OssClientFactory.CreateOssClientEnableMD5(true);

            try
            {
                //just for code coverage
                using (Stream content = File.OpenRead(_localImageFile))
                {
                    var hashStream = new Common.Internal.MD5Stream(content, null, content.Length);
                    var putRequest = new PutObjectRequest(_bucketName, _keyName, hashStream);
                    putRequest.Process = _process;
                    client.PutObject(putRequest);
                }

                // get processed image
                GetObjectRequest request   = new GetObjectRequest(_bucketName, _keyName, _process);
                OssObject        ossObject = client.GetObject(request);

                // put processed image
                Stream seekableStream = ConvertStreamToSeekable(ossObject.Content);
                client.PutObject(_bucketName, _processedKey, seekableStream);

                // get info of processed image
                var imgInfo = GetOssImageObjectInfo(_bucketName, _processedKey);

                // check processed result
                Assert.AreEqual(imgInfo, _imageInfo);
            }
            catch (OssException e)
            {
                Assert.AreEqual(OssErrorCode.InvalidArgument, e.ErrorCode);
            }
            finally
            {
                _ossClient.DeleteObject(_bucketName, _keyName);
                _ossClient.DeleteObject(_bucketName, _processedKey);
            }
        }
Exemple #3
0
        public void MultipartUploadAbortInMiddleTest()
        {
            IOss client = OssClientFactory.CreateOssClientEnableMD5(true);

            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);

            Assert.IsTrue(partCount > 1, "Source file is too small to perform multipart upload");
            LogUtility.LogMessage("File {0} is splitted to {1} parts for multipart upload",
                                  sourceFile, partCount);

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

            //upload the file
            using (var fs = new FileStream(partFile.FullName, FileMode.Open))
            {
                //use partCount - 1, so that the last part is left
                for (var i = 0; i < partCount - 1; 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);
                    uploadPartRequest.InputStream = fs;
                    uploadPartRequest.PartSize    = size;
                    uploadPartRequest.PartNumber  = (i + 1);
                    var uploadPartResult = client.UploadPart(uploadPartRequest);

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

                    //list parts which are uploaded
                    var listPartsRequest = new ListPartsRequest(_bucketName, targetObjectKey, initResult.UploadId);
                    listPartsRequest.EncodingType = null;
                    var listPartsResult = _ossClient.ListParts(listPartsRequest);
                    //there should be only 1 part was not uploaded
                    Assert.AreEqual(i + 1, OssTestUtils.ToArray <Part>(listPartsResult.Parts).Count, "uploaded parts is not expected");
                }
            }
            //abort the upload
            var abortRequest = new AbortMultipartUploadRequest(_bucketName, targetObjectKey, initResult.UploadId);

            _ossClient.AbortMultipartUpload(abortRequest);
        }