Exemplo n.º 1
0
        public void Delete(string remotename)
        {
            try
            {
                cosXml = GetCosXml();
                string bucket = _cosOptions.Bucket;
                string key    = GetFullKey(remotename);

                DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);

                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

                DeleteObjectResult result = cosXml.DeleteObject(request);
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                Logging.Log.WriteErrorMessage(LOGTAG, "Delete", clientEx, "Delete failed: {0}", remotename);
                throw;
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                Logging.Log.WriteErrorMessage(LOGTAG, "Delete", serverEx, "Delete failed: {0}, {1}", remotename, serverEx.GetInfo());
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 删除单个对象
        /// </summary>
        /// <param name="key">对象在存储桶中的位置,即称对象键(也可以理解为在存储桶路径)</param>
        /// <param name="bucketName">存储桶,格式:BucketName-APPID</param>
        public static CosResultInfoResponseDto DeleteObject(string key, string bucketName = CosConfig.DEFAULT_BUCKET)
        {
            var cosXml   = InitializeCosConfig();
            var response = new CosResultInfoResponseDto();

            try
            {
                string bucket = $"{bucketName}-{AppId}"; //存储桶,格式:BucketName-APPID";
                DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
                //执行请求
                DeleteObjectResult result = cosXml.DeleteObject(request);
                //请求成功
                response.statusCode    = result.httpCode;
                response.statusMessage = result.httpMessage;
                return(response);
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                response.statusCode    = clientEx.errorCode;
                response.statusMessage = clientEx.Message;
                response.info          = clientEx.ToJson();
                return(response);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                response.statusCode    = serverEx.statusCode;
                response.statusMessage = serverEx.statusMessage;
                response.info          = serverEx.GetInfo();
                return(response);
            }
        }
Exemplo n.º 3
0
        public bool DeleteObject(string key)
        {
            try
            {
                DeleteObjectRequest request = new DeleteObjectRequest(options.Bucket, key);
                //执行请求
                DeleteObjectResult result = cosXml.DeleteObject(request);
                //请求成功
                // Console.WriteLine(result.GetResultInfo());

                logger.LogInformation($"DeleteObject[{key}]:{result.GetResultInfo()}");
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                logger.LogError(clientEx, $"DeleteObject");
                return(false);
                //请求失败
                //Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                logger.LogError(serverEx, $"DeleteObject");
                return(false);
                //请求失败
                // Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }

            return(true);
        }
Exemplo n.º 4
0
        /// 删除对象
        public void DeleteObject()
        {
            //.cssg-snippet-body-start:[delete-object]
            try
            {
                string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
                string key    = "exampleobject";            //对象键
                DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
                //执行请求
                DeleteObjectResult result = cosXml.DeleteObject(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
        }
Exemplo n.º 5
0
        /// <summary>
        /// 删除文件
        /// </summary>
        public static bool Delete(this OSSSetting setting, string objectName)
        {
            OssClient          client = new OssClient(setting.endpoint, setting.accessKeyId, setting.accessKeySecret);
            DeleteObjectResult result = client.DeleteObject(setting.bucketName, objectName);

            return(true);
        }
Exemplo n.º 6
0
        public async Task <ResponseModel> DeleteObject(string buketName)
        {
            try
            {
                string bucket = _buketName + "-" + _appid; //存储桶名称 格式:BucketName-APPID
                string key    = "exampleobject";           //对象在存储桶中的位置,即称对象键.
                DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
                //执行请求
                DeleteObjectResult result = await Task.FromResult(_cosXml.DeleteObject(request));

                return(new ResponseModel {
                    Code = 200, Message = result.GetResultInfo()
                });
            }
            catch (CosClientException clientEx)
            {
                return(new ResponseModel {
                    Code = 0, Message = "CosClientException: " + clientEx.Message
                });
            }
            catch (CosServerException serverEx)
            {
                return(new ResponseModel {
                    Code = 0, Message = "CosServerException: " + serverEx.GetInfo()
                });
            }
        }
Exemplo n.º 7
0
        public static void AsynDeleteObject(COSXML.CosXml cosXml, string bucket, string key)
        {
            DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);

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

            //执行请求
            cosXml.DeleteObject(request, delegate(CosResult result)
            {
                DeleteObjectResult getObjectResult = result as DeleteObjectResult;
                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());
                }
            });
        }
Exemplo n.º 8
0
        internal static void DeleteObject(CosXmlServer cosXml, string cosKey)
        {
            DeleteObjectRequest request = new DeleteObjectRequest(bucket, cosKey);

            DeleteObjectResult result = cosXml.DeleteObject(request);

            Console.WriteLine(result.GetResultInfo());
        }
Exemplo n.º 9
0
        public override void DeleteFile(IVirgoFile file)
        {
            var ossSettings = _config.FileUploadOptions.Settings.Where(x => x.Key.ToLower() == "tencos").Select(x => x.Value).FirstOrDefault();
            FileHandlerOptions groupInfo = null;

            if (string.IsNullOrEmpty(file.ExtraInfo))
            {
                groupInfo = ossSettings?.FirstOrDefault();
            }
            else
            {
                groupInfo = ossSettings?.Where(x => x.GroupName.ToLower() == file.ExtraInfo.ToLower()).FirstOrDefault();
                if (groupInfo == null)
                {
                    groupInfo = ossSettings?.FirstOrDefault();
                }
            }
            if (groupInfo == null)
            {
                return;
            }
            try
            {
                CosXmlConfig config = new CosXmlConfig.Builder()
                                      .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms
                                      .SetReadWriteTimeoutMs(40000)  //设置读写超时时间,单位毫秒,默认45000ms
                                      .IsHttps(false)                //设置默认 HTTPS 请求
                                      .SetAppid(groupInfo.AppId)     //设置腾讯云账户的账户标识 APPID
                                      .SetRegion(groupInfo.GroupName)
                                      .Build();
                long durationSecond = 600;          //每次请求签名有效时长,单位为秒
                QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(groupInfo.Secret,
                                                                                                        groupInfo.Key, durationSecond);
                CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider);
                string bucket = groupInfo.GroupLocation + "-" + groupInfo.AppId;
                string key    = file.FileName; //对象键
                DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
                //执行请求
                DeleteObjectResult result = cosXml.DeleteObject(request);
                //请求成功
                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
            return;
        }
        public async Task <bool> RemoveObjectAsync(string bucketName, string objectName)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            if (string.IsNullOrEmpty(objectName))
            {
                throw new ArgumentNullException(nameof(objectName));
            }
            bucketName = ConvertBucketName(bucketName);
            DeleteObjectRequest request = new DeleteObjectRequest(bucketName, objectName);
            DeleteObjectResult  result  = _client.DeleteObject(request);

            return(await Task.FromResult(result.IsSuccessful()));
        }
Exemplo n.º 11
0
        public void DeleteObject()
        {
            try
            {
                DeleteObjectRequest request = new DeleteObjectRequest(bucket, commonKey);

                //执行请求
                DeleteObjectResult result = cosXml.DeleteObject(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);
            }
        }
Exemplo n.º 12
0
        /// 移动对象
        public async void MoveObject()
        {
            TransferConfig transferConfig = new TransferConfig();

            // 初始化 TransferManager
            TransferManager transferManager = new TransferManager(cosXml, transferConfig);

            //.cssg-snippet-body-start:[move-object]
            string sourceAppid  = "1250000000";              //账号 appid
            string sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶
            string sourceRegion = "COS_REGION";              //源对象的存储桶所在的地域
            string sourceKey    = "sourceObject";            //源对象键
            //构造源对象属性
            CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,
                                                               sourceRegion, sourceKey);

            string bucket = "examplebucket-1250000000"; //目标存储桶,格式:BucketName-APPID
            string key    = "exampleobject";            //目标对象的对象键

            COSXMLCopyTask copyTask = new COSXMLCopyTask(bucket, key, copySource);

            try {
                // 拷贝对象
                COSXML.Transfer.COSXMLCopyTask.CopyTaskResult result = await
                                                                       transferManager.CopyAsync(copyTask);

                Console.WriteLine(result.GetResultInfo());

                // 删除对象
                DeleteObjectRequest request      = new DeleteObjectRequest(sourceBucket, sourceKey);
                DeleteObjectResult  deleteResult = cosXml.DeleteObject(request);
                // 打印结果
                Console.WriteLine(deleteResult.GetResultInfo());
            } catch (Exception e) {
                Console.WriteLine("CosException: " + e);
            }
            //.cssg-snippet-body-end
        }
Exemplo n.º 13
0
        public static void DeleteObject(COSXML.CosXml cosXml, string bucket, string key)
        {
            try
            {
                DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

                //执行请求
                DeleteObjectResult result = cosXml.DeleteObject(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());
            }
        }
Exemplo n.º 14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="bucket"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public bool Delete(string bucket, string key)
 {
     try
     {
         DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
         //执行请求
         DeleteObjectResult result = cosXml.DeleteObject(request);
         return(true);
     }
     catch (COSXML.CosException.CosClientException clientEx)
     {
         //请求失败
         Console.WriteLine("CosClientException: " + clientEx);
         Debug.WriteLine("CosClientException: " + clientEx);
         return(false);
     }
     catch (COSXML.CosException.CosServerException serverEx)
     {
         //请求失败
         Console.WriteLine("CosServerException: " + serverEx.GetInfo());
         Debug.WriteLine("CosServerException: " + serverEx.GetInfo());
         return(false);
     }
 }