示例#1
0
        public virtual Task <GetOssObjectsResponse> GetObjectsAsync(GetOssObjectsRequest request)
        {
            // 先定位检索的目录
            var filePath = CalculateFilePath(request.BucketName, request.Prefix);

            if (!Directory.Exists(filePath))
            {
                throw new BusinessException(code: OssManagementErrorCodes.ContainerNotFound);
                // throw new ContainerNotFoundException($"Can't not found container {request.BucketName} in file system");
            }
            // 目录也属于Oss对象,需要抽象的文件系统集合来存储
            var fileSystemNames = Directory.GetFileSystemEntries(filePath);
            int maxFilesCount   = fileSystemNames.Length;

            // 排序所有文件与目录
            Array.Sort(fileSystemNames, delegate(string x, string y)
            {
                // 检索的是文件系统名称
                // 需要判断是否为文件夹进行升序排序
                // 参考 OssObjectComparer

                var xFolder = Directory.Exists(x);
                var yFolder = Directory.Exists(y);

                if (xFolder && yFolder)
                {
                    return(x.CompareTo(y));
                }

                if (xFolder && !yFolder)
                {
                    return(-1);
                }

                if (!xFolder && yFolder)
                {
                    return(1);
                }

                return(x.CompareTo(y));
            });

            // 需要计算从哪个位置截断
            int markIndex = 0;

            if (!request.Marker.IsNullOrWhiteSpace())
            {
                markIndex = fileSystemNames.FindIndex(x => x.EndsWith(request.Marker));
                if (markIndex < 0)
                {
                    markIndex = 0;
                }
            }

            // 需要截断Oss对象列表
            var copyFileSystemNames = fileSystemNames;

            if (markIndex > 0)
            {
                copyFileSystemNames = fileSystemNames[(markIndex + 1)..];
示例#2
0
        public virtual async Task <GetOssObjectsResponse> GetObjectsAsync(GetOssObjectsRequest request)
        {
            var ossClient = await CreateClientAsync();

            var objectPath = GetBasePath(request.Prefix);
            var marker     = !objectPath.IsNullOrWhiteSpace() && !request.Marker.IsNullOrWhiteSpace()
                ? request.Marker.Replace(objectPath, "")
                : request.Marker;
            var aliyunRequest = new ListObjectsRequest(request.BucketName)
            {
                Marker       = !marker.IsNullOrWhiteSpace() ? objectPath + marker : marker,
                Prefix       = objectPath,
                MaxKeys      = request.MaxKeys,
                EncodingType = request.EncodingType,
                Delimiter    = request.Delimiter
            };
            var objectsResponse = ossClient.ListObjects(aliyunRequest);

            var ossObjects = objectsResponse.ObjectSummaries
                             .Where(x => !x.Key.Equals(objectsResponse.Prefix))  // 过滤当前的目录返回值
                             .Select(x => new OssObject(
                                         !objectPath.IsNullOrWhiteSpace() && !x.Key.Equals(objectPath)
                                    ? x.Key.Replace(objectPath, "")
                                    : x.Key, // 去除目录名称
                                         request.Prefix,
                                         x.LastModified,
                                         x.Size,
                                         x.LastModified,
                                         new Dictionary <string, string>
            {
                { "Id", x.Owner?.Id },
                { "DisplayName", x.Owner?.DisplayName }
            },
                                         x.Key.EndsWith("/"))
            {
                FullName = x.Key
            })
                             .ToList();

            // 当 Delimiter 为 / 时, objectsResponse.CommonPrefixes 可用于代表层级目录
            if (objectsResponse.CommonPrefixes.Any())
            {
                ossObjects.InsertRange(0,
                                       objectsResponse.CommonPrefixes
                                       .Select(x => new OssObject(
                                                   x.Replace(objectPath, ""),
                                                   request.Prefix,
                                                   null,
                                                   0L,
                                                   null,
                                                   null,
                                                   true)));
            }
            // 排序
            // TODO: 是否需要客户端来排序
            ossObjects.Sort(new OssObjectComparer());

            return(new GetOssObjectsResponse(
                       objectsResponse.BucketName,
                       request.Prefix,
                       marker,
                       !objectPath.IsNullOrWhiteSpace() && !objectsResponse.NextMarker.IsNullOrWhiteSpace()
                    ? objectsResponse.NextMarker.Replace(objectPath, "")
                    : objectsResponse.NextMarker,
                       objectsResponse.Delimiter,
                       objectsResponse.MaxKeys,
                       ossObjects));
        }