예제 #1
0
        public void AppendFile(string fileName, Stream stream)
        {
            fileName = GetFileName(fileName);
            //第一次追加文件的时候,文件可能已经存在,先获取文件的当前长度,如果不存在,position为0
            long position = 0;

            if (ExistFile(fileName))
            {
                var metadata = ossClient.GetObjectMetadata(Config.BucketName, fileName);

                if (metadata.ObjectType == "Appendable")
                {
                    position = metadata.ContentLength;
                }
                else
                {
                    throw new HimallIOException(IOErrorMsg.NoramlFileNotOperate.ToDescription());
                }
            }
            else
            {
                RecurseCreateFileDir(fileName);
            }

            var request = new AppendObjectRequest(Config.BucketName, fileName)
            {
                ObjectMetadata = new ObjectMetadata(),
                Content        = stream,
                Position       = position
            };

            ossClient.AppendObject(request);
        }
 private AppendObjectCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
                             IDeserializer <ServiceResponse, AppendObjectResult> deserializer,
                             AppendObjectRequest request)
     : base(client, endpoint, context, deserializer)
 {
     _request = request;
 }
        public AppendObjectResponse AppendObject(AppendObjectRequest request)
        {
            IAsyncResult asyncResult;

            asyncResult = invokeAppendObject <AppendObjectResponse>(request, null, null, true);
            return(EndAppendObject(asyncResult));
        }
예제 #4
0
        public void AppendObjectTest()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            long position = 0;
            var  request  = new AppendObjectRequest(_bucketName, key)
            {
                Content  = new MemoryStream(Encoding.ASCII.GetBytes(_data_200KB)),
                Position = position
            };

            Assert.AreEqual(request.TrafficLimit, 0);

            var result = _ossClient.AppendObject(request);

            Assert.AreEqual(_data_200KB.Length, result.NextAppendPosition);
            position = result.NextAppendPosition;
            Assert.AreEqual(result.ResponseMetadata.ContainsKey(HttpHeaders.QosDelayTime), false);


            request = new AppendObjectRequest(_bucketName, key)
            {
                Content      = new MemoryStream(Encoding.ASCII.GetBytes(_data_200KB)),
                Position     = position,
                TrafficLimit = 100 * 1024 * 8
            };
            Assert.AreEqual(request.TrafficLimit, 819200);
            result = _ossClient.AppendObject(request);
            Assert.IsTrue(result.HashCrc64Ecma != 0);
            Assert.AreEqual(result.ResponseMetadata.ContainsKey(HttpHeaders.QosDelayTime), true);
        }
        public static void SyncAppendObject(string bucketName)
        {
            const string key      = "AppendObject";
            long         position = 0;
            ulong        initCrc  = 0;

            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                position = metadata.ContentLength;
                initCrc  = ulong.Parse(metadata.Crc64);
            }
            catch (Exception)  {}

            try
            {
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = fs,
                        Position       = position,
                        InitCrc        = initCrc
                    };

                    var result = client.AppendObject(request);
                    position = result.NextAppendPosition;
                    initCrc  = result.HashCrc64Ecma;

                    Console.WriteLine("Append object succeeded, next append position:{0}", position);
                }

                // append object by using NextAppendPosition
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = fs,
                        Position       = position,
                        InitCrc        = initCrc
                    };

                    var result = client.AppendObject(request);
                    position = result.NextAppendPosition;

                    Console.WriteLine("Append object succeeded too, next append position:{0}", position);
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
 /// <summary>
 /// Start the asynchronous request for an appendable upload.
 /// </summary>
 /// <param name="request">Parameters in an appendable upload request</param>
 /// <param name="callback">Asynchronous request callback function</param>
 /// <param name="state">Asynchronous request status object</param>
 /// <returns>Response to the asynchronous request</returns>
 public IAsyncResult BeginAppendObject(AppendObjectRequest request, AsyncCallback callback, object state)
 {
     return(this.BeginDoRequest <AppendObjectRequest>(request, delegate()
     {
         if (request.ObjectKey == null)
         {
             throw new ObsException(Constants.InvalidObjectKeyMessage, ErrorType.Sender, Constants.InvalidObjectKey, "");
         }
     }, callback, state));
 }
예제 #7
0
 /// <summary>
 /// Perform an appendable upload.
 /// </summary>
 /// <param name="request">Parameters in an appendable upload request</param>
 /// <returns>Response to an appendable upload request</returns>
 public AppendObjectResponse AppendObject(AppendObjectRequest request)
 {
     return(this.DoRequest <AppendObjectRequest, AppendObjectResponse>(request, delegate()
     {
         if (request.ObjectKey == null)
         {
             throw new ObsException(Constants.InvalidObjectKeyMessage, ErrorType.Sender, Constants.InvalidObjectKey, "");
         }
     }));
 }
        public static void SyncAppendObject(string bucketName)
        {
            const string key = "AppendObject";
            long position = 0;
            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                position = metadata.ContentLength;
            }
            catch(Exception)  {}

            try
            {
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content = fs,
                        Position = position
                    };

                    var result = client.AppendObject(request);
                    position = result.NextAppendPosition;

                    Console.WriteLine("Append object succeeded, next append position:{0}", position);
                }

                // append object by using NextAppendPosition
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content = fs,
                        Position = position
                    };

                    var result = client.AppendObject(request);
                    position = result.NextAppendPosition;

                    Console.WriteLine("Append object succeeded too, next append position:{0}", position);
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        public void AppendObjectProgressTest()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            try
            {
                long position = 0;
                using (var fs = File.Open(Config.UploadTestFile, FileMode.Open))
                {
                    // the first time append
                    var fileLength = fs.Length / 2;
                    var request    = new AppendObjectRequest(_bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = new PartialWrapperStream(fs, fileLength),
                        Position       = position,
                    };
                    request.StreamTransferProgress += uploadProgressCallback;

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

                    // the second time append
                    fs.Position = fs.Length / 2;
                    fileLength  = fs.Length - fs.Length / 2;
                    request     = new AppendObjectRequest(_bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = new PartialWrapperStream(fs, fileLength),
                        Position       = position
                    };
                    request.StreamTransferProgress += uploadProgressCallback;

                    result = _ossClient.AppendObject(request);
                    Assert.AreEqual(fs.Length, result.NextAppendPosition);
                    Assert.IsTrue(result.HashCrc64Ecma != 0);
                }

                // check md5
                OssTestUtils.DownloadObject(_ossClient, _bucketName, key, _tmpLocalFile);
                var expectedHashDigest = FileUtils.ComputeContentMd5(Config.UploadTestFile);
                var actualHashDigest   = FileUtils.ComputeContentMd5(_tmpLocalFile);
                Assert.AreEqual(expectedHashDigest, actualHashDigest);
            }
            finally
            {
                if (OssTestUtils.ObjectExists(_ossClient, _bucketName, key))
                {
                    _ossClient.DeleteObject(_bucketName, key);
                }
                File.Delete(_tmpLocalFile);
            }
        }
예제 #10
0
        /// <summary>
        /// 追加上传
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="fileToUpload"></param>
        /// <returns></returns>
        public string SyncAppendObject(string bucketName, string fileToUpload)
        {
            string key      = DateTime.Now.ToSerialNumber() + Path.GetExtension(fileToUpload);
            long   position = 0;

            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                position = metadata.ContentLength;
            }
            catch (Exception) { }

            try
            {
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = fs,
                        Position       = position
                    };

                    var result = client.AppendObject(request);
                    position = result.NextAppendPosition;
                }

                // append object by using NextAppendPosition
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = fs,
                        Position       = position
                    };

                    var result = client.AppendObject(request);
                    position = result.NextAppendPosition;
                }
            }
            catch (OssException ex)
            {
            }
            catch (Exception ex)
            {
            }
            return(key);
        }
        public static AppendObjectCommand Create(IServiceClient client, Uri endpoint, ExecutionContext context,
                                                 AppendObjectRequest request)
        {
            OssUtils.CheckBucketName(request.BucketName);
            OssUtils.CheckObjectKey(request.Key);

            if (request.Content == null)
            {
                throw new ArgumentNullException("request.Content");
            }

            request.ObjectMetadata = request.ObjectMetadata ?? new ObjectMetadata();
            if (request.ObjectMetadata.ContentType == null)
            {
                request.ObjectMetadata.ContentType = HttpUtils.GetContentType(request.Key, null);
            }

            var conf           = OssUtils.GetClientConfiguration(client);
            var originalStream = request.Content;
            var streamLength   = request.Content.Length;

            // setup progress
            var callback = request.StreamTransferProgress;

            if (callback != null)
            {
                originalStream  = OssUtils.SetupProgressListeners(originalStream, conf.ProgressUpdateInterval, client, callback);
                request.Content = originalStream;
            }

            // wrap input stream in MD5Stream
            if (conf.EnalbeMD5Check)
            {
                var hashStream = new MD5Stream(originalStream, null, streamLength);
                request.Content = hashStream;
                context.ResponseHandlers.Add(new MD5DigestCheckHandler(hashStream));
            }
            else if (conf.EnableCrcCheck && request.InitCrc != null)
            {
                var hashStream = new Crc64Stream(originalStream, null, streamLength, request.InitCrc.Value);
                request.Content = hashStream;
                context.ResponseHandlers.Add(new Crc64CheckHandler(hashStream));
            }

            return(new AppendObjectCommand(client, endpoint, context,
                                           DeserializerFactory.GetFactory().CreateAppendObjectReusltDeserializer(),
                                           request));
        }
예제 #12
0
        //追加文件(防止添加重复相片)
        public static void AppendObject()
        {
            //第一次追加文件的时候,文件可能已经存在,先获取文件的当前长度,如果不存在,position为0,如果存在则不会添加进去
            long position = 0;

            try
            {
                var client   = new OssClient("oss-cn-shenzhen.aliyuncs.com", "LTAId7dsrQHujhU5", "O3nQOqai4yXrvGCKNbvgrKuU8f7U7p");
                var metadata = client.GetObjectMetadata("flowera", "2.jpg");
                position = metadata.ContentLength;
            }
            catch (Exception) { }
            try
            {
                var client = new OssClient("oss-cn-shenzhen.aliyuncs.com", "LTAId7dsrQHujhU5", "O3nQOqai4yXrvGCKNbvgrKuU8f7U7p");
                using (var fs = File.Open("D:/Users/pc/Desktop/个人文件/AlbumProject/AlbumProject/Content/2.jpg", FileMode.Open))
                {
                    var request = new AppendObjectRequest("flowera", "2.jpg")
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = fs,
                        Position       = position
                    };
                    var result = client.AppendObject(request);
                    // 设置下次追加文件时的position位置
                    position = result.NextAppendPosition;
                    Console.WriteLine("Append object succeeded, next append position:{0}", position);
                }
                // 再次追加文件,这时候的position值可以从上次的结果中获取到
                using (var fs = File.Open("D:/Users/pc/Desktop/个人文件/AlbumProject/AlbumProject/Content/2.jpg", FileMode.Open))
                {
                    var request = new AppendObjectRequest("flowera", "2.jpg")
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = fs,
                        Position       = position
                    };
                    var result = client.AppendObject(request);
                    position = result.NextAppendPosition;
                    Console.WriteLine("Append object succeeded, next append position:{0}", position);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Append object failed, {0}", ex.Message);
            }
        }
        public static void SyncAppendObjectWithPartSize(string bucketName)
        {
            const long   partSize = 1 * 1024 * 1024;
            const string key      = "AppendObjectWithPartsize";
            long         position = 0;
            ulong        initCrc  = 0;

            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                position = metadata.ContentLength;
                initCrc  = ulong.Parse(metadata.Crc64);
            }
            catch (Exception) { }

            try
            {
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    do
                    {
                        fs.Seek(position, SeekOrigin.Begin);
                        var request = new AppendObjectRequest(bucketName, key)
                        {
                            ObjectMetadata = new ObjectMetadata(),
                            Content        = new PartialWrapperStream(fs, partSize),
                            Position       = position,
                            InitCrc        = initCrc
                        };
                        var result = client.AppendObject(request);
                        position = result.NextAppendPosition;
                        initCrc  = result.HashCrc64Ecma;
                        Console.WriteLine("Append object succeeded, next append position:{0}, crc64:{1}", position, initCrc);
                    } while (position < fs.Length);
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
예제 #14
0
        public static AppendObjectCommand Create(IServiceClient client, Uri endpoint, ExecutionContext context,
                                                 AppendObjectRequest request)
        {
            OssUtils.CheckBucketName(request.BucketName);
            OssUtils.CheckObjectKey(request.Key);

            if (request.Content == null)
            {
                throw new ArgumentNullException("request.Content");
            }

            request.ObjectMetadata = request.ObjectMetadata ?? new ObjectMetadata();
            if (request.ObjectMetadata.ContentType == null)
            {
                request.ObjectMetadata.ContentType = HttpUtils.GetContentType(request.Key, null);
            }

            return(new AppendObjectCommand(client, endpoint, context,
                                           DeserializerFactory.GetFactory().CreateAppendObjectReusltDeserializer(),
                                           request));
        }
        public static void AsyncAppendObject(string bucketName)
        {
            const string key      = "AsyncAppendObject";
            long         position = 0;

            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                position = metadata.ContentLength;
            }
            catch (Exception) { }

            try
            {
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = fs,
                        Position       = position
                    };


                    const string notice = "Append object succeeded";
                    client.BeginAppendObject(request, AppendObjectCallback, notice.Clone());

                    _event.WaitOne();
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
예제 #16
0
        public static void AppendObjectProgress(string bucketName)
        {
            const string key      = "AppendObjectProgress";
            long         position = 0;

            try
            {
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content        = fs,
                        Position       = position
                    };
                    request.StreamTransferProgress += streamProgressCallback;

                    var result = client.AppendObject(request);
                    position = result.NextAppendPosition;

                    Console.WriteLine("Append object succeeded, next append position:{0}", position);
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
            finally
            {
                client.DeleteObject(bucketName, key);
            }
        }
        static void AppendObject()
        {
            try
            {
                AppendObjectRequest request = new AppendObjectRequest()
                {
                    BucketName = bucketName,
                    ObjectKey  = objectName,
                    FilePath   = filePath,
                    Position   = 10
                };
                AppendObjectResponse response = client.AppendObject(request);

                Console.WriteLine("Append object response: {0}", response.StatusCode);
                Console.WriteLine("ETag: {0}", response.ETag);
                Console.WriteLine("NextPosition: {0}", response.NextPosition.ToString());
                Console.WriteLine("Object StorageClass: {0}", response.StorageClass.ToString());
            }
            catch (ObsException ex)
            {
                Console.WriteLine("Exception errorcode: {0}, when append object.", ex.ErrorCode);
                Console.WriteLine("Exception errormessage: {0}", ex.ErrorMessage);
            }
        }
 public IAsyncResult BeginAppendObject(AppendObjectRequest request, AsyncCallback callback, object state)
 {
     return(invokeAppendObject <AppendObjectResponse>(request, callback, state, false));
 }
 IAsyncResult invokeAppendObject <T>(AppendObjectRequest request, AsyncCallback callback, object state, bool synchronized)
     where T : AppendObjectResponse, new()
 {
     return(invokeUpdateObject <T>(request, callback, state, synchronized));
 }
예제 #20
0
 public AppendObjectResult AppendObject(AppendObjectRequest request)
 {
     return((Model.Object.AppendObjectResult)Excute(request, new Model.Object.AppendObjectResult()));
 }
예제 #21
0
 public void AppendObject(AppendObjectRequest request, Callback.OnSuccessCallback <CosResult> successCallback, Callback.OnFailedCallback failCallback)
 {
     Schedue(request, new AppendObjectResult(), successCallback, failCallback);
 }
예제 #22
0
        public static AppendUploadResult AppendUpload
        (
            IOssClientBuilder clientBuilder,
            string bucketName,
            string objectName,
            Stream streamToUpload,
            Action <AppendUploadOptions> options = null,
            EventHandler <StreamTransferProgressArgs> streamTransferProgress = null,
            CancellationToken cancellationToken = default(CancellationToken)
        )
        {
            if (clientBuilder == null)
            {
                throw new ArgumentNullException(nameof(clientBuilder));
            }
            if (streamToUpload == null)
            {
                throw new ArgumentNullException(nameof(streamToUpload));
            }

            AppendUploadOptions uploadOptions = new AppendUploadOptions();

            options?.Invoke(uploadOptions);
            int blockSize = Math.Max(MIN_BLOCK_SIZE, uploadOptions.InitialBlockSize);

            var            client   = clientBuilder.Build(o => o.EnableCrcCheck = true);
            long           position = 0;
            ObjectMetadata metadata = null;

            try
            {
                metadata = client.GetObjectMetadata(bucketName, objectName);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                // No such object
            }

            if (metadata != null)
            {
                if (string.Compare(metadata.ObjectType, "Appendable", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    if (metadata.ContentLength > 0)
                    {
                        if (metadata.ContentLength <= streamToUpload.Length)
                        {
                            if (uploadOptions.AllowResume)
                            {
                                position = metadata.ContentLength;
                                if (uploadOptions.VerifyBeforeResume)
                                {
                                    using (var stream = new PartialStream(streamToUpload, 0, position))
                                        using (var crc64 = new Crc64HashAlgorithm())
                                        {
                                            crc64.ComputeHash(stream);
                                            string localHash = BitConverter.ToUInt64(crc64.Hash, 0).ToString(CultureInfo.InvariantCulture);
                                            if (string.Compare(metadata.Crc64, localHash, StringComparison.InvariantCultureIgnoreCase) != 0)
                                            {
                                                if (uploadOptions.AllowOverwrite)
                                                {
                                                    client.DeleteObject(bucketName, objectName);
                                                    position = 0;
                                                }
                                                else
                                                {
                                                    throw new OssException("Hash mismatched, could not resume file");
                                                }
                                            }
                                        }
                                }
                            }
                            else if (uploadOptions.AllowOverwrite)
                            {
                                client.DeleteObject(bucketName, objectName);
                            }
                            else
                            {
                                throw new OssException("Could not resume or overwrite file");
                            }
                        }
                        else
                        {
                            if (uploadOptions.AllowOverwrite)
                            {
                                client.DeleteObject(bucketName, objectName);
                            }
                            else
                            {
                                throw new OssException("Could not resume, the length of remote object is longer than local object.");
                            }
                        }
                    }
                }
                else if (uploadOptions.AllowOverwrite)
                {
                    client.DeleteObject(bucketName, objectName);
                }
                else
                {
                    throw new OssException("The object is not appendable");
                }
            }

            if (streamToUpload.Length == position)
            {
                return(new AppendUploadResult {
                    ETag = metadata.ETag, Crc64 = metadata.Crc64, ContentLength = metadata.ContentLength
                });
            }

            while (true)
            {
                var result = Retry((retryCount) =>
                {
                    if (retryCount > 0)
                    {
                        blockSize = Math.Max(MIN_BLOCK_SIZE, blockSize >> 1);
                    }
                    var length  = Math.Min(blockSize, streamToUpload.Length - position);
                    var request = new AppendObjectRequest(bucketName, objectName)
                    {
                        Content  = new PartialStream(streamToUpload, position, length),
                        Position = position,
                        StreamTransferProgress = (s, e) =>
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            if (streamTransferProgress != null)
                            {
                                streamTransferProgress.Invoke(s, new StreamTransferProgressArgs(e.IncrementTransferred, position + e.TransferredBytes, streamToUpload.Length));
                            }
                        }
                    };
                    return(client.AppendObject(request));
                },
                                   cancellationToken: cancellationToken);
                position = result.NextAppendPosition;
                if (position >= streamToUpload.Length)
                {
                    return(new AppendUploadResult
                    {
                        ETag = result.ETag,
                        Crc64 = result.HashCrc64Ecma.ToString(CultureInfo.InvariantCulture),
                        ContentLength = result.NextAppendPosition,
                        AppendObjectResult = result
                    });
                }
            }
        }
        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 static void AsyncAppendObject(string bucketName)
        {
            const string key = "AsyncAppendObject";
            long position = 0;
            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                position = metadata.ContentLength;
            }
            catch (Exception) { }

            try
            {
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var request = new AppendObjectRequest(bucketName, key)
                    {
                        ObjectMetadata = new ObjectMetadata(),
                        Content = fs,
                        Position = position
                    };


                    const string notice = "Append object succeeded";
                    client.BeginAppendObject(request, AppendObjectCallback, notice.Clone());

                    _event.WaitOne();
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
예제 #25
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);
                }
            }
        }