Exemple #1
0
 /// 获取第二页对象列表
 public void GetBucketNextPage()
 {
     //.cssg-snippet-body-start:[get-bucket-next-page]
     try
     {
         string           bucket  = "examplebucket-1250000000"; //格式:BucketName-APPID
         GetBucketRequest request = new GetBucketRequest(bucket);
         //上一次拉取数据的下标
         request.SetMarker(this.nextMarker);
         //执行请求
         GetBucketResult result = cosXml.GetBucket(request);
         //bucket的相关信息
         ListBucket info = result.listBucket;
     }
     catch (COSXML.CosException.CosClientException clientEx)
     {
         //请求失败
         Console.WriteLine("CosClientException: " + clientEx);
     }
     catch (COSXML.CosException.CosServerException serverEx)
     {
         //请求失败
         Console.WriteLine("CosServerException: " + serverEx.GetInfo());
     }
     //.cssg-snippet-body-end
 }
Exemple #2
0
        /// <summary>
        /// 获取bukect中的所有文件
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="config"></param>
        /// <param name="cosCredentialProvider"></param>
        /// <returns></returns>
        public static List <CloudFile> ListObjects()
        {
            if (BosConfig == null)
            {
                return(null);
            }

            //初始化 CosXmlServer
            CosXmlServer     cosXml  = getCosXmlServer();
            GetBucketRequest request = new GetBucketRequest(BosConfig.BucketName);

            //设置签名有效时长
            request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
            request.SetMaxKeys(1 + "");
            //执行请求
            GetBucketResult result = cosXml.GetBucket(request);

            ListBucket listBucket = result.listBucket;

            List <ListBucket.Contents> contents = listBucket.contentsList;

            List <CloudFile> cloudFiles = new List <CloudFile>();

            foreach (ListBucket.Contents content in contents)
            {
                CloudFile cloudFile = new CloudFile
                {
                    Key          = content.key,
                    Size         = content.size,
                    LastModified = content.lastModified
                };
                cloudFiles.Add(cloudFile);
            }

            while (result.listBucket.nextMarker != null)
            {
                request.SetMarker(result.listBucket.nextMarker);

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

                foreach (ListBucket.Contents content in result.listBucket.contentsList)
                {
                    CloudFile cloudFile = new CloudFile
                    {
                        Key          = content.key,
                        Size         = content.size,
                        LastModified = content.lastModified
                    };

                    cloudFiles.Add(cloudFile);
                }
            }

            Console.WriteLine(cloudFiles);
            return(cloudFiles);
        }
        /// 指定前缀批量删除对象
        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
        }
Exemple #4
0
        public IEnumerable <IFileEntry> List()
        {
            bool   isTruncated = true;
            string filename    = null;

            while (isTruncated)
            {
                cosXml = GetCosXml();
                string bucket = _cosOptions.Bucket;
                string prefix = _cosOptions.Path;

                GetBucketRequest request = new GetBucketRequest(bucket);

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

                if (!string.IsNullOrEmpty(filename))
                {
                    request.SetMarker(filename);
                }

                if (!string.IsNullOrEmpty(prefix))
                {
                    request.SetPrefix(prefix);
                }

                GetBucketResult result = cosXml.GetBucket(request);

                ListBucket info = result.listBucket;

                isTruncated = result.listBucket.isTruncated;
                filename    = result.listBucket.nextMarker;

                foreach (var item in info.contentsList)
                {
                    var last = DateTime.Parse(item.lastModified);

                    var fileName = item.key;
                    if (!string.IsNullOrWhiteSpace(prefix))
                    {
                        fileName = fileName.Substring(prefix.Length);

                        if (fileName.StartsWith("/", StringComparison.Ordinal))
                        {
                            fileName = fileName.Trim('/');
                        }
                    }

                    yield return(new FileEntry(fileName, item.size, last, last));
                }
            }
        }
        public async Task <List <Item> > ListObjectsAsync(string bucketName, string prefix = null)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            bucketName = ConvertBucketName(bucketName);
            ListBucket  info       = null;
            string      nextMarker = null;
            List <Item> items      = new List <Item>();

            do
            {
                GetBucketRequest request = new GetBucketRequest(bucketName);
                if (!string.IsNullOrEmpty(nextMarker))
                {
                    request.SetMarker(nextMarker);
                }
                if (!string.IsNullOrEmpty(prefix))
                {
                    request.SetPrefix(prefix);
                }
                //执行请求
                GetBucketResult result = _client.GetBucket(request);
                //bucket的相关信息
                info = result.listBucket;
                if (info.isTruncated)
                {
                    // 数据被截断,记录下数据下标
                    nextMarker = info.nextMarker;
                }
                foreach (var item in info.contentsList)
                {
                    items.Add(new Item()
                    {
                        Key          = item.key,
                        LastModified = item.lastModified,
                        ETag         = item.eTag,
                        Size         = (ulong)item.size,
                        IsDir        = false,
                        BucketName   = bucketName,
                        VersionId    = null,
                    });
                }
            } while (info.isTruncated);
            return(await Task.FromResult(items));
        }