Beispiel #1
0
        private async Task DownloadInternal(string fid, Action <Stream> writer)
        {
            ConnectionAddress connectionAddress;

            if (_option.RestOption.EnableReadJwt)
            {
                var lookupRequest  = new LookupRequest(fid, true);
                var lookupResponse = await _executer.ExecuteAsync(lookupRequest);

                var url = lookupResponse.Locations.FirstOrDefault().Url;
                connectionAddress = new ConnectionAddress(url);
            }
            else
            {
                var connection = _connectionManager.GetVolumeConnectionByVolumeIdOrFid(fid);
                connectionAddress = connection.ConnectionAddress;
            }
            await Task.Factory.StartNew(() =>
            {
                var client           = GetDownloadClient(connectionAddress);
                IRestRequest request = new RestRequest("/{fid}");
                request.AddUrlSegment("fid", fid);
                request.ResponseWriter = writer;
                var response           = client.DownloadData(request);
            });
        }
        /// <summary>根据VolumeId(可以是Fid)获取Volume连接
        /// </summary>
        public Connection GetVolumeConnectionByVolumeIdOrFid(string volumeIdOrFid)
        {
            //如果是Fid,就转换成VolumeId
            if (StringUtil.IsFid(volumeIdOrFid))
            {
                volumeIdOrFid = StringUtil.GetVolumeId(volumeIdOrFid);
            }

            if (_volumeIdAddressMappers.TryGetValue(volumeIdOrFid, out ConnectionAddress connectionAddress))
            {
                return(GetVolumeConnectionInternal(connectionAddress));
            }
            else
            {
                var request = new LookupVolumeRequest(volumeIdOrFid);
                var task    = _seaweedfsExecuter.ExecuteAsync(request);
                task.Wait();
                //查询Volume返回
                var lookupVolumeResponse = task.Result;
                if (lookupVolumeResponse.IsSuccessful)
                {
                    //成功
                    lock (SyncObject)
                    {
                        //连接地址
                        connectionAddress = new ConnectionAddress(lookupVolumeResponse.Locations.FirstOrDefault().Url);
                        //添加映射
                        _volumeIdAddressMappers.TryAdd(volumeIdOrFid, connectionAddress);
                        return(GetVolumeConnectionInternal(connectionAddress));
                    }
                }
                else
                {
                    _logger.LogInformation("根据VolumeId获取Volume信息时出错,VolumeId:{0},HttpStatus:{1}", volumeIdOrFid, lookupVolumeResponse.StatusCode);
                    throw new Exception($"获取Volume信息出错,{lookupVolumeResponse.ErrorMessage}");
                }
            }
        }
        /// <summary>获取AssignFid
        /// </summary>
        /// <param name="replication">同步机制</param>
        /// <param name="dataCenter">数据中心</param>
        /// <param name="ttl">时效</param>
        /// <param name="collection">集合</param>
        /// <param name="count">数量</param>
        /// <returns><see cref="Seaweedfs.Client.Rest.AssignFileKeyResponse"/></returns>
        public async Task <AssignFileKeyResponse> AssignFileKey(string replication = "", int?count = 1, string dataCenter = "", string ttl = "", string collection = "")
        {
            var request = new AssignFileKeyRequest(replication, count, dataCenter, ttl, collection);

            return(await _seaweedfsExecuter.ExecuteAsync(request));
        }