Ejemplo n.º 1
0
        /// 删除多个对象
        public void DeleteMultiObject()
        {
            //.cssg-snippet-body-start:[delete-multi-object]
            try
            {
                string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
                DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);
                //设置返回结果形式
                request.SetDeleteQuiet(false);
                //对象key
                string        key     = "exampleobject"; //对象键
                List <string> objects = new List <string>();
                objects.Add(key);
                request.SetObjectKeys(objects);
                //执行请求
                DeleteMultiObjectResult result = cosXml.DeleteMultiObjects(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
        }
Ejemplo n.º 2
0
        public static void AsyncMultiDeleteObject(COSXML.CosXml cosXml, string bucket, List <string> keys)
        {
            DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);

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

            //设置返回结果形式
            request.SetDeleteQuiet(false);

            //设置删除的keys
            request.SetObjectKeys(keys);

            //执行请求
            cosXml.DeleteMultiObjects(request, delegate(CosResult result)
            {
                DeleteMultiObjectResult getObjectResult = result as DeleteMultiObjectResult;
                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());
                }
            });
        }
Ejemplo n.º 3
0
        public static void MultiDeleteObject(COSXML.CosXml cosXml, string bucket, List <string> keys)
        {
            try
            {
                DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

                //设置返回结果形式
                request.SetDeleteQuiet(false);

                //设置删除的keys
                request.SetObjectKeys(keys);

                //执行请求
                DeleteMultiObjectResult result = cosXml.DeleteMultiObjects(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());
            }
        }
Ejemplo n.º 4
0
        public void MultiDeleteObject()
        {
            try
            {
                DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);

                //设置返回结果形式
                request.SetDeleteQuiet(false);

                //设置删除的keys
                List <string> keys = new List <string>();
                keys.Add(commonKey);
                keys.Add(multiKey);
                request.SetObjectKeys(keys);

                //执行请求
                DeleteMultiObjectResult result = cosXml.DeleteMultiObjects(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);
            }
        }
Ejemplo n.º 5
0
        /// 指定前缀批量删除对象
        public void DeletePrefix()
        {
            //.cssg-snippet-body-start:[delete-prefix]
            try
            {
                String nextMarker = null;

                // 循环请求直到没有下一页数据
                do
                {
                    string           bucket      = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
                    string           prefix      = "folder1/";                 //指定前缀
                    GetBucketRequest listRequest = new GetBucketRequest(bucket);
                    //获取 folder1/ 下的所有对象以及子目录
                    listRequest.SetPrefix(prefix);
                    listRequest.SetMarker(nextMarker);
                    //执行列出对象请求
                    GetBucketResult listResult = cosXml.GetBucket(listRequest);
                    ListBucket      info       = listResult.listBucket;
                    // 对象列表
                    List <ListBucket.Contents> objects = info.contentsList;
                    // 下一页的下标
                    nextMarker = info.nextMarker;

                    DeleteMultiObjectRequest deleteRequest = new DeleteMultiObjectRequest(bucket);
                    //设置返回结果形式
                    deleteRequest.SetDeleteQuiet(false);
                    //对象列表
                    List <string> deleteObjects = new List <string>();
                    foreach (var content in objects)
                    {
                        deleteObjects.Add(content.key);
                    }
                    deleteRequest.SetObjectKeys(deleteObjects);
                    //执行批量删除请求
                    DeleteMultiObjectResult deleteResult = cosXml.DeleteMultiObjects(deleteRequest);
                    //打印请求结果
                    Console.WriteLine(deleteResult.GetResultInfo());
                } while (nextMarker != null);
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
            //.cssg-snippet-body-end
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     删除容器
        /// </summary>
        /// <param name="containerName"></param>
        /// <returns></returns>
        public async Task DeleteContainer(string containerName)
        {
            //删除目录等于删除该目录下的所有文件
            var objs = await ListBlobs(containerName);

            var count = objs.Count / 1000 + (objs.Count % 1000 > 0 ? 1 : 0);

            for (var i = 0; i < count; i++)
            {
                var request = new DeleteMultiObjectRequest(_tcConfig.BucketName);
                request.SetObjectKeys(objs.Skip(i * 1000).Take(1000).Select(p => p.Name).ToList());
                var response = _cosXmlServer.DeleteMultiObjects(request);
                await response.HandlerError("删除对象时出错(删除目录会删除该目录下所有的文件)!");
            }
        }
        public async Task <bool> RemoveObjectAsync(string bucketName, List <string> objectNames)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            if (objectNames == null || objectNames.Count == 0)
            {
                throw new ArgumentNullException(nameof(objectNames));
            }
            bucketName = ConvertBucketName(bucketName);
            DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucketName);

            //设置返回结果形式
            request.SetDeleteQuiet(false);
            request.SetObjectKeys(objectNames);
            DeleteMultiObjectResult result = _client.DeleteMultiObjects(request);

            return(await Task.FromResult(result.IsSuccessful()));
        }
Ejemplo n.º 8
0
        public bool DeleteMutiObjects(List <string> keys)
        {
            try
            {
                //存储桶,格式:BucketName-APPID
                DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(options.Bucket);
                //设置返回结果形式
                request.SetDeleteQuiet(false);
                //对象key

                //List<string> objects = new List<string>();
                //objects.Add(key);
                request.SetObjectKeys(keys);
                //执行请求
                DeleteMultiObjectResult result = cosXml.DeleteMultiObjects(request);
                //请求成功
                //   Console.WriteLine(result.GetResultInfo());

                logger.LogInformation($"DeleteMutiObjects[{string.Join("|", keys)}]:{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);
        }