Beispiel #1
0
        /// 恢复归档对象
        public void RestoreObject()
        {
            //.cssg-snippet-body-start:[restore-object]
            try
            {
                string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
                string key    = "exampleobject";            //对象键
                RestoreObjectRequest request = new RestoreObjectRequest(bucket, key);
                //恢复时间
                request.SetExpireDays(3);
                request.SetTier(COSXML.Model.Tag.RestoreConfigure.Tier.Bulk);

                //执行请求
                RestoreObjectResult result = cosXml.RestoreObject(request);
                //请求成功
                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }

            //.cssg-snippet-body-end
        }
Beispiel #2
0
        public static void AsynRestoreObject(COSXML.CosXml cosXml, string bucket, string key)
        {
            RestoreObjectRequest request = new RestoreObjectRequest(bucket, key);

            //设置签名有效时长
            request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

            //恢复时间
            request.SetExpireDays(3);
            request.SetTier(COSXML.Model.Tag.RestoreConfigure.Tier.Bulk);

            //执行请求
            cosXml.RestoreObject(request, delegate(CosResult result)
            {
                RestoreObjectResult getObjectResult = result as RestoreObjectResult;
                Console.WriteLine(getObjectResult.GetResultInfo());
            }, delegate(CosClientException clientEx, CosServerException serverEx)
            {
                if (clientEx != null)
                {
                    QLog.D("XIAO", clientEx.Message);
                    Console.WriteLine("CosClientException: " + clientEx.StackTrace);
                }
                if (serverEx != null)
                {
                    QLog.D("XIAO", serverEx.Message);
                    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                }
            });
        }
Beispiel #3
0
        public static void RestoreObject(COSXML.CosXml cosXml, string bucket, string key)
        {
            try
            {
                RestoreObjectRequest request = new RestoreObjectRequest(bucket, key);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

                //恢复时间
                request.SetExpireDays(3);
                request.SetTier(COSXML.Model.Tag.RestoreConfigure.Tier.Bulk);

                //执行请求
                RestoreObjectResult result = cosXml.RestoreObject(request);

                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                QLog.D("XIAO", clientEx.Message);
                Console.WriteLine("CosClientException: " + clientEx.StackTrace);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                QLog.D("XIAO", serverEx.Message);
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
        }
Beispiel #4
0
        public void RestoreObject()
        {
            try
            {
                RestoreObjectRequest request = new RestoreObjectRequest(bucket, commonKey);
                //恢复时间
                request.SetExpireDays(3);
                request.SetTier(COSXML.Model.Tag.RestoreConfigure.Tier.Bulk);

                //执行请求
                RestoreObjectResult result = cosXml.RestoreObject(request);

                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                Console.WriteLine("CosClientException: " + clientEx.Message);
                Assert.True(false);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                Assert.True(false);
            }
        }
        public static void RestoreArchiveObject(string bucketName, string key, bool waitUtilFinished = true, int maxWaitTimeInSeconds = 600)
        {
            RestoreObjectResult result = client.RestoreObject(bucketName, key);

            if (result.HttpStatusCode != HttpStatusCode.Accepted || !waitUtilFinished)
            {
                throw new OssException(result.RequestId + ", " + result.HttpStatusCode + " ,");
            }

            while (maxWaitTimeInSeconds > 0)
            {
                var    meta          = client.GetObjectMetadata(bucketName, key);
                string restoreStatus = meta.HttpMetadata["x-oss-restore"] as string;
                if (restoreStatus != null && restoreStatus.StartsWith("ongoing-request=\"false\"", StringComparison.InvariantCultureIgnoreCase))
                {
                    break;
                }

                Thread.Sleep(1000);
                maxWaitTimeInSeconds--;
            }

            if (maxWaitTimeInSeconds == 0)
            {
                throw new TimeoutException();
            }
        }
Beispiel #6
0
        /// <summary>
        /// 修改文件存储类型
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ChangeTypeResultDto ChangeType(ChangeTypeParam request)
        {
            return(ToolCommon.GetResponse(() =>
            {
                new ChangeTypeParamValidator().Validate(request).Check(HttpStatus.Err.Name);
                var fileInfo = Get(new GetFileParam(request.Key, request.PersistentOps));
                if (fileInfo.State)
                {
                    var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone,
                                                  () => ZoneEnum.HangZhou);
                    var client = _aLiYunConfig.GetClient(zone);
                    var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket);
                    if (fileInfo.FileType.Id == StorageClass.Archive.Id)
                    {
                        //解除归档
                        RestoreObjectResult result = client.RestoreObject(bucket, request.Key);
                        if (result != null && result.HttpStatusCode == HttpStatusCode.OK)
                        {
                            return new ChangeTypeResultDto(false, request.Key, "解除归档成功,请稍后再尝试更改文件存储类型");
                        }

                        if (result != null)
                        {
                            return new ChangeTypeResultDto(false, request.Key,
                                                           $"解除归档失败,请稍后重试,RequestId:{result.RequestId},HttpStatusCode:{result.HttpStatusCode}");
                        }

                        return new ChangeTypeResultDto(false, request.Key, "解除归档失败,请稍后重试");
                    }

                    var ret = client.GetObject(bucket, request.Key);
                    if (ret != null && ret.HttpStatusCode == HttpStatusCode.OK)
                    {
                        ObjectMetadata objectMetadata =
                            Core.Tools.GetObjectMetadataBySourceObjectMetadata(ret.Metadata, "x-oss-storage-class",
                                                                               Core.Tools.GetStorageClass(request.Type).ToString());
                        client.ModifyObjectMeta(bucket, request.Key, objectMetadata);
                        return new ChangeTypeResultDto(true, request.Key, "success");
                    }

                    if (ret != null)
                    {
                        return new ChangeTypeResultDto(false, request.Key,
                                                       $"lose,RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}");
                    }

                    return new ChangeTypeResultDto(false, request.Key, "lose");
                }

                return new ChangeTypeResultDto(false, request.Key, fileInfo.Msg);
            }, message => new ChangeTypeResultDto(false, request.Key, message)));
        }
Beispiel #7
0
 public void testRestoreHistoryObject()
 {
     try
     {
         RestoreObjectRequest request = new RestoreObjectRequest("000000000001",
                                                                 "2020-01-17.21-35-01.log");
         request.SetExpireDays(2);
         request.SetTier(RestoreConfigure.Tier.Standard);
         request.SetVersionId("MTg0NDUxNjQ1ODU5NjY2NDYyOTM");
         RestoreObjectResult result = QCloudServer.Instance().cosXml.RestoreObject(request);
         Console.WriteLine(result.GetResultInfo());
         Assert.True(true);
     }
     catch (COSXML.CosException.CosClientException clientEx)
     {
         Console.WriteLine("CosClientException: " + clientEx);
         Assert.True(false);
     }
     catch (COSXML.CosException.CosServerException serverEx)
     {
         Console.WriteLine("CosServerException: " + serverEx.GetInfo());
         Assert.True(serverEx.statusCode == 404);
     }
 }