/// <summary> /// 复制文件(两个文件需要在同一账号下) /// 对于小于1G的文件(不支持跨地域拷贝。例如,不支持将杭州存储空间里的文件拷贝到青岛),另外需将分片设置为4M,其他分类不支持 /// </summary> /// <param name="request"></param> /// <returns></returns> public CopyFileResultDto CopyTo(CopyFileParam request) { return(ToolCommon.GetResponse(() => { new CopyFileParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var targetBucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket, request.OptBucket); if (!request.IsForce) { var newBasePersistentOps = request.PersistentOps.Clone(); newBasePersistentOps.Bucket = request.OptBucket; var existRet = this.Exist(new ExistParam(request.OptKey, newBasePersistentOps)); if (existRet.State) { return new CopyFileResultDto(false, request.SourceKey, "复制失败,文件已存在"); } } if (Core.Tools.GetChunkUnit(_aLiYunConfig, request.PersistentOps.ChunkUnit).Id != ChunkUnit.U4096K.Id) { return CopySmallFile(client, bucket, request.SourceKey, targetBucket, request.OptKey); } return CopyBigFile(client, bucket, request.SourceKey, targetBucket, request.OptKey); }, message => new CopyFileResultDto(false, request.SourceKey, message))); }
/// <summary> /// 根据文件key集合删除 /// </summary> /// <param name="request"></param> /// <returns></returns> public IEnumerable <DeleteResultDto> RemoveRange(RemoveRangeParam request) { return(ToolCommon.GetResponse(() => { new RemoveRangeParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var ret = client.DeleteObjects(new DeleteObjectsRequest(bucket, request.Keys, false)); if (ret.HttpStatusCode == HttpStatusCode.OK) { List <DeleteResultDto> deleteResultList = new List <DeleteResultDto>(); request.Keys.ForEach(key => { if (ret.Keys.Any(x => x.Key == key)) { deleteResultList.Add(new DeleteResultDto(true, key, "success")); } else { deleteResultList.Add(new DeleteResultDto(true, key, $"delete lose,RequestId:{ret.RequestId}")); } }); return deleteResultList; } return request.Keys.Select(x => new DeleteResultDto(false, x, $"delete lose,RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}")); }, message => request.Keys.Select(x => new DeleteResultDto(false, x, message)))); }
/// <summary> /// 查询空间标签 /// </summary> /// <param name="request"></param> /// <returns></returns> public TagResultDto GetTags(GetTagsBucketParam request) { return(ToolCommon.GetResponse(() => { new GetTagsBucketParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); // 查看Bucket标签。 var ret = client.GetBucketTagging(bucket); if (ret != null && ret.HttpStatusCode == HttpStatusCode.OK) { return new TagResultDto(true, ret.Tags.Select(x => new KeyValuePair <string, string>(x.Key, x.Value)).ToList(), "success"); } if (ret != null) { return new TagResultDto(false, null, $"lose,RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}"); } return new TagResultDto(false, null, "lose"); }, message => new TagResultDto(false, null, message))); }
/// <summary> /// 根据文件流上传 /// </summary> /// <param name="param"></param> /// <param name="isResume">是否允许续传</param> /// <returns></returns> public UploadResultDto UploadStream(UploadByStreamParam param, bool isResume = false) { return(ToolCommon.GetResponse(() => { var zone = Core.Tools.GetZone(_aLiYunConfig, param.UploadPersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); return Upload(isResume, (bucket, newPersistentOps, objectMetadata) => { PutObjectResult ret; if (isResume) { var request = new UploadObjectRequest(bucket, param.Key, param.Stream) { PartSize = Core.Tools.GetPartSize( Core.Tools.GetChunkUnit(_aLiYunConfig, newPersistentOps.ChunkUnit, () => ChunkUnit.U2048K)), Metadata = objectMetadata }; ret = client.ResumableUploadObject(request); } else { ret = client.PutObject(bucket, param.Key, param.Stream, objectMetadata); } return ret; }, param.UploadPersistentOps); }, (message, ex) => new UploadResultDto(false, ex, message))); }
/// <summary> /// 批量设置生存时间(超时会自动删除) /// </summary> /// <param name="request"></param> /// <returns></returns> public List <ExpireResultDto> SetExpireRange(SetExpireRangeParam request) { return(ToolCommon.GetResponse(() => { var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucket); List <ExpireResultDto> list = new List <ExpireResultDto>(); request.Keys.ForEach(key => { LifecycleRule lcr = new LifecycleRule() { ID = "delete " + key + " files", Prefix = key, Status = RuleStatus.Enabled, ExpriationDays = request.Expire }; setBucketLifecycleRequest.AddLifecycleRule(lcr); list.Add(new ExpireResultDto(true, key, "success")); }); client.SetBucketLifecycle(setBucketLifecycleRequest); //调整生命周期 return list; }, message => { List <ExpireResultDto> list = new List <ExpireResultDto>(); request.Keys.ForEach(key => { list.Add(new ExpireResultDto(false, key, message)); }); return list; })); }
/// <summary> /// 修改文件MimeType /// </summary> /// <param name="request"></param> /// <returns></returns> public ChangeMimeResultDto ChangeMime(ChangeMimeParam request) { return(ToolCommon.GetResponse(() => { new ChangeMimeParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var ret = client.GetObject(bucket, request.Key); if (ret != null && ret.HttpStatusCode == HttpStatusCode.OK) { ObjectMetadata objectMetadata = Core.Tools.GetObjectMetadataBySourceObjectMetadata(ret.Metadata, "ContentType", request.MimeType); client.ModifyObjectMeta(bucket, request.Key, objectMetadata); return new ChangeMimeResultDto(true, request.Key, "success"); } if (ret != null) { return new ChangeMimeResultDto(false, request.Key, $"lose,RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}"); } return new ChangeMimeResultDto(false, request.Key, "lose"); }, message => new ChangeMimeResultDto(false, request.Key, message))); }
/// <summary> /// 检查文件是否存在 /// </summary> /// <param name="request"></param> /// <returns></returns> public OperateResultDto Exist(ExistParam request) { return(ToolCommon.GetResponse(() => { var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var exist = client.DoesObjectExist(bucket, request.Key); return new OperateResultDto(exist, "success"); }, message => new OperateResultDto(false, message))); }
/// <summary> /// 获取文件流(根据已授权的地址) /// </summary> /// <param name="request"></param> /// <returns></returns> public DownloadStreamResultDto DownloadStream(FileDownloadStreamParam request) { return(ToolCommon.GetResponse(() => { new FileDownloadStreamParamValidator().Validate(request).Check(HttpStatus.Err.Name); Uri uri = new Uri(request.Url); string host = $"{uri.Scheme}://{uri.Host}"; return new DownloadStreamResultDto(true, "success", new HttpClient(host).GetStream(request.Url.Replace(host, "")), null); }, message => new DownloadStreamResultDto(message))); }
/// <summary> /// 清除标签 /// </summary> /// <param name="request"></param> /// <returns></returns> public OperateResultDto ClearTag(ClearTagBucketParam request) { return(ToolCommon.GetResponse(() => { var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); // 查看Bucket标签。 client.DeleteBucketTagging(bucket); return new OperateResultDto(true, "success"); }, message => new OperateResultDto(false, message))); }
/// <summary> /// 清空防盗链规则 /// </summary> /// <param name="request"></param> /// <returns></returns> public OperateResultDto ClearReferer(ClearRefererParam request) { return(ToolCommon.GetResponse(() => { Check.TrueByString(request != null, $"{nameof(request)} is null", HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); client.SetBucketReferer(new SetBucketRefererRequest(bucket)); return new OperateResultDto(true, "success"); }, message => new OperateResultDto(false, message))); }
/// <summary> /// 设置空间访问权限 /// </summary> /// <param name="request"></param> /// <returns></returns> public OperateResultDto SetPermiss(SetPermissParam request) { return(ToolCommon.GetResponse(() => { new SetPermissParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); client.SetBucketAcl(Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket), Core.Tools.GetCannedAccessControl(request.Permiss)); return new OperateResultDto(true, "success"); }, message => new OperateResultDto(false, message))); }
/// <summary> /// 修改文件存储类型 /// </summary> /// <param name="request"></param> /// <returns></returns> public ChangeTypeResultDto ChangeType(ChangeTypeParam request) { return(ToolCommon.GetResponse(() => { new ChangeTypeParamValidator().Validate(request).Check(HttpStatus.Err.Name); var fileInfo = Get(new GetFileParam(request.Key, request.PersistentOps)); if (fileInfo.State) { var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); if (fileInfo.FileType.Id == StorageClass.Archive.Id) { //解除归档 RestoreObjectResult result = client.RestoreObject(bucket, request.Key); if (result != null && result.HttpStatusCode == HttpStatusCode.OK) { return new ChangeTypeResultDto(false, request.Key, "解除归档成功,请稍后再尝试更改文件存储类型"); } if (result != null) { return new ChangeTypeResultDto(false, request.Key, $"解除归档失败,请稍后重试,RequestId:{result.RequestId},HttpStatusCode:{result.HttpStatusCode}"); } return new ChangeTypeResultDto(false, request.Key, "解除归档失败,请稍后重试"); } var ret = client.GetObject(bucket, request.Key); if (ret != null && ret.HttpStatusCode == HttpStatusCode.OK) { ObjectMetadata objectMetadata = Core.Tools.GetObjectMetadataBySourceObjectMetadata(ret.Metadata, "x-oss-storage-class", Core.Tools.GetStorageClass(request.Type).ToString()); client.ModifyObjectMeta(bucket, request.Key, objectMetadata); return new ChangeTypeResultDto(true, request.Key, "success"); } if (ret != null) { return new ChangeTypeResultDto(false, request.Key, $"lose,RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}"); } return new ChangeTypeResultDto(false, request.Key, "lose"); } return new ChangeTypeResultDto(false, request.Key, fileInfo.Msg); }, message => new ChangeTypeResultDto(false, request.Key, message))); }
/// <summary> /// 得到访问地址 /// </summary> /// <param name="request"></param> /// <returns></returns> public GetVisitUrlResultDto GetVisitUrl(GetVisitUrlParam request) { return(ToolCommon.GetResponse(() => { new GetVisitUrlParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var req = new GeneratePresignedUriRequest(bucket, request.Key, SignHttpMethod.Get); var uri = client.GeneratePresignedUri(req); return new GetVisitUrlResultDto(uri.ToString(), "success"); }, message => new GetVisitUrlResultDto(message))); }
/// <summary> /// 设置防盗链 /// </summary> /// <param name="request"></param> /// <returns></returns> public OperateResultDto SetReferer(SetRefererParam request) { return(ToolCommon.GetResponse(() => { new SetRefererParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); client.SetBucketReferer(new SetBucketRefererRequest(bucket, request.RefererList, request.IsAllowNullReferer)); return new OperateResultDto(true, "success"); }, message => new OperateResultDto(false, message))); }
/// <summary> /// 判断空间是否存在 /// </summary> /// <param name="request"></param> /// <returns></returns> public OperateResultDto Exist(ExistBucketParam request) { return(ToolCommon.GetResponse(_logger, () => { Check.TrueByString(request != null, $"{nameof(request)} is null", HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); if (client.DoesBucketExist(bucket)) { return new OperateResultDto(true, "success"); } return new OperateResultDto(false, "the bucket is not find"); }, message => new OperateResultDto(false, message))); }
/// <summary> /// 获取空间列表 /// </summary> /// <param name="request"></param> /// <returns></returns> public BucketItemResultDto GetBucketList(GetBucketParam request) { return(ToolCommon.GetResponse(() => { var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); ListBucketsRequest listBucketsRequest = new ListBucketsRequest(); if (request.PageSize != -1) { listBucketsRequest.MaxKeys = request.PageSize; } if (request.TagFilters.Count > 0) { var keyValue = request.TagFilters.FirstOrDefault(); listBucketsRequest.Tag = new Tag() { Key = keyValue.Key, Value = keyValue.Value }; } if (!string.IsNullOrEmpty(request.Prefix)) { listBucketsRequest.Prefix = request.Prefix; } if (!string.IsNullOrEmpty(request.Marker)) { listBucketsRequest.Marker = request.Marker; } var ret = client.ListBuckets(listBucketsRequest); if (ret.HttpStatusCode == HttpStatusCode.OK) { return new BucketItemResultDto( ret.Buckets.Select(x => new BucketItemResultDto.BucketItemDto((int?)Core.Tools.GetZoneByLocation(x.Location), x.Name)).ToList(), ret.Prefix, ret.IsTruncated, ret.Marker, ret.NextMaker); } return new BucketItemResultDto(request.Prefix, request.Marker, $"lose RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}"); }, message => new BucketItemResultDto(request.Prefix, request.Marker, message))); }
/// <summary> /// 得到防盗链配置 /// </summary> /// <param name="request"></param> /// <returns></returns> public RefererResultDto GetReferer(GetRefererParam request) { return(ToolCommon.GetResponse(() => { Check.TrueByString(request != null, $"{nameof(request)} is null", HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var rc = client.GetBucketReferer(bucket); if (rc == null) { return new RefererResultDto("the bucket is not find"); } return new RefererResultDto(rc.AllowEmptyReferer, (rc.RefererList?.Referers ?? Array.Empty <string>()).ToList()); }, message => new RefererResultDto(message))); }
/// <summary> /// 创建空间 /// </summary> /// <param name="request"></param> /// <returns></returns> public OperateResultDto Create(CreateBucketParam request) { return(ToolCommon.GetResponse(() => { new CreateBucketParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); Bucket ret = request.StorageClass != null ? client.CreateBucket(request.BucketName, Core.Tools.GetStorageClass(request.StorageClass)) : client.CreateBucket(request.BucketName); if (ret != null) { return new OperateResultDto(true, "success"); } return new OperateResultDto(false, "lose"); }, message => new OperateResultDto(false, message))); }
/// <summary> /// 上传文件 /// </summary> /// <param name="isResume"></param> /// <param name="funcAction"></param> /// <param name="uploadPersistentOps"></param> /// <returns></returns> private UploadResultDto Upload(bool isResume, Func <string, UploadPersistentOps, ObjectMetadata, PutObjectResult> funcAction, UploadPersistentOps uploadPersistentOps) { return(ToolCommon.GetResponse(() => { var bucket = Core.Tools.GetBucket(_aLiYunConfig, uploadPersistentOps.Bucket); var persistentOps = base.GetUploadPersistentOps(uploadPersistentOps); var metadata = GetCallbackMetadata(persistentOps); PutObjectResult ret = funcAction.Invoke(bucket, persistentOps, metadata); if (ret.HttpStatusCode == HttpStatusCode.OK) { return new UploadResultDto(true, null, "success"); } return new UploadResultDto(false, ret, $"RequestId:{ret.RequestId}"); }, (message, ex) => new UploadResultDto(false, ex, message))); }
/// <summary> /// 根据key删除文件 /// </summary> /// <param name="request"></param> /// <returns></returns> public DeleteResultDto Remove(RemoveParam request) { return(ToolCommon.GetResponse(() => { new RemoveParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var ret = client.DeleteObject(bucket, request.Key); if (ret.HttpStatusCode == HttpStatusCode.OK) { return new DeleteResultDto(true, request.Key, "success"); } return new DeleteResultDto(false, request.Key, $"RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}"); }, message => new DeleteResultDto(false, request.Key, message))); }
/// <summary> /// 获取文件权限 /// </summary> /// <param name="request"></param> /// <returns></returns> public FilePermissResultInfo GetPermiss(GetFilePermissParam request) { return(ToolCommon.GetResponse(() => { new GetFilePermissParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var ret = client.GetObjectAcl(bucket, request.Key); if (ret.HttpStatusCode == HttpStatusCode.OK) { return new FilePermissResultInfo(true, Core.Tools.GetPermiss(ret.ACL), "success"); } return new FilePermissResultInfo(false, null, "文件不存在" + ret.ToString()); }, message => new FilePermissResultInfo(false, null, message))); }
/// <summary> /// 设置标签 /// </summary> /// <param name="request"></param> /// <returns></returns> public OperateResultDto SetTag(SetTagBucketParam request) { return(ToolCommon.GetResponse(() => { new SetTagBucketParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var setRequest = new SetBucketTaggingRequest(bucket); request.Tags.ForEach(tag => { setRequest.AddTag(new Tag() { Key = tag.Key, Value = tag.Value }); }); client.SetBucketTagging(setRequest); return new OperateResultDto(true, "success"); }, message => new OperateResultDto(false, message))); }
/// <summary> /// 设置生存时间(超时会自动删除) /// </summary> /// <param name="request"></param> /// <returns></returns> public ExpireResultDto SetExpire(SetExpireParam request) { return(ToolCommon.GetResponse(() => { new SetExpireParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucket); LifecycleRule lcr = new LifecycleRule() { ID = "delete " + request.Key + " files", Prefix = request.Key, Status = RuleStatus.Enabled, ExpriationDays = request.Expire }; setBucketLifecycleRequest.AddLifecycleRule(lcr); client.SetBucketLifecycle(setBucketLifecycleRequest); //调整生命周期 return new ExpireResultDto(true, request.Key, "success"); }, message => new ExpireResultDto(false, request.Key, message))); }
/// <summary> /// 获取空间的访问权限 /// </summary> /// <param name="persistentOps"></param> /// <returns></returns> public BucketPermissItemResultDto GetPermiss(BasePersistentOps persistentOps) { return(ToolCommon.GetResponse(() => { Check.True(persistentOps != null, "策略信息异常"); var zone = Core.Tools.GetZone(_aLiYunConfig, persistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, persistentOps.Bucket); var ret = client.GetBucketAcl(bucket); if (ret != null && ret.HttpStatusCode == HttpStatusCode.OK) { return new BucketPermissItemResultDto(true, Core.Tools.GetPermiss(ret.ACL), "success"); } if (ret != null) { return new BucketPermissItemResultDto(false, null, $"lose,RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}"); } return new BucketPermissItemResultDto(false, null, "lose"); }, message => new BucketPermissItemResultDto(false, null, message))); }
/// <summary> /// 移动文件(两个文件需要在同一账号下) /// </summary> /// <param name="request"></param> /// <returns></returns> public MoveFileResultDto Move(MoveFileParam request) { return(ToolCommon.GetResponse(() => { new MoveFileParamValidator(_aLiYunConfig).Validate(request).Check(HttpStatus.Err.Name); if (!request.IsForce) { var newBasePersistentOps = request.PersistentOps.Clone(); newBasePersistentOps.Bucket = request.OptBucket; var existRet = this.Exist(new ExistParam(request.OptKey, newBasePersistentOps)); if (existRet.State) { return new MoveFileResultDto(false, request.SourceKey, "移动失败,文件已存在"); } } CopyTo(new CopyFileParam(request.SourceKey, request.OptKey, request.OptBucket, request.IsForce, request.PersistentOps)); Remove(new RemoveParam(request.SourceKey, request.PersistentOps)); return new MoveFileResultDto(true, request.SourceKey, "success"); }, message => new MoveFileResultDto(false, request.SourceKey, message))); }
/// <summary> /// 下载文件(根据已授权的地址) /// </summary> /// <param name="request"></param> /// <returns></returns> public DownloadResultDto Download(FileDownloadParam request) { return(ToolCommon.GetResponse(() => { new FileDownloadParamValidator().Validate(request).Check(HttpStatus.Err.Name); Uri uri = new Uri(request.Url); string host = $"{uri.Scheme}://{uri.Host}"; using (var file = File.Open(request.SavePath, FileMode.OpenOrCreate)) { using (Stream stream = new HttpClient(host).GetStream(request.Url.Replace(host, ""))) { int length = 4 * 1024; var buf = new byte[length]; do { length = stream.Read(buf, 0, length); file.Write(buf, 0, length); } while (length != 0); } } return new DownloadResultDto(true, "success"); }, (message, ex) => new DownloadResultDto(false, message, ex))); }
/// <summary> /// 获取文件信息 /// </summary> /// <param name="request"></param> /// <returns></returns> public FileInfoDto Get(GetFileParam request) { return(ToolCommon.GetResponse(() => { new GetFileParamValidator().Validate(request).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, request.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, request.PersistentOps.Bucket); var ret = client.GetObject(bucket, request.Key); if (ret.HttpStatusCode == HttpStatusCode.OK) { string fileTypeStr = ret.Metadata.HttpMetadata.Where(x => x.Key == "x-oss-storage-class") .Select(x => x.Value.ToString()).FirstOrDefault(); StorageClass fileType = null; if (!string.IsNullOrEmpty(fileTypeStr)) { fileType = Core.Tools.GetStorageClass(fileTypeStr); } return new FileInfoDto(true, "success") { Hash = ret.Metadata.ContentMd5, Key = ret.Key, Size = ret.Metadata.ContentLength, PutTime = ret.Metadata.LastModified.ToUnixTimestamp(TimestampType.Second), MimeType = ret.Metadata.ContentType, FileType = fileType }; } return new FileInfoDto(false, $"lose RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}") { Key = request.Key }; }, message => new FileInfoDto(false, message))); }
/// <summary> /// 获取指定前缀的文件列表 /// </summary> /// <param name="filter">筛选</param> /// <returns></returns> public ListFileItemResultDto ListFiles(ListFileFilter filter) { return(ToolCommon.GetResponse(() => { new ListFileFilterValidator().Validate(filter).Check(HttpStatus.Err.Name); var zone = Core.Tools.GetZone(_aLiYunConfig, filter.PersistentOps.Zone, () => ZoneEnum.HangZhou); var client = _aLiYunConfig.GetClient(zone); var bucket = Core.Tools.GetBucket(_aLiYunConfig, filter.PersistentOps.Bucket); var listObjectsRequest = new ListObjectsRequest(bucket) { MaxKeys = filter.PageSize }; if (!string.IsNullOrEmpty(filter.Delimiter)) { listObjectsRequest.Delimiter = filter.Delimiter; } if (!string.IsNullOrEmpty(filter.Marker)) { listObjectsRequest.Marker = filter.Marker; } if (!string.IsNullOrEmpty(filter.Prefix)) { listObjectsRequest.Prefix = filter.Prefix; } var ret = client.ListObjects(listObjectsRequest); if (ret.HttpStatusCode == HttpStatusCode.OK) { var list = new ListFileItemResultDto(true, "success") { CommonPrefixes = ret.CommonPrefixes?.ToList() ?? new List <string>(), Marker = ret.NextMarker, Items = ret.ObjectSummaries.Select(x => new FileInfoDto(true, "success") { Key = x.Key, Hash = "", Size = x.Size, PutTime = x.LastModified.ToUnixTimestamp(TimestampType.Millisecond), MimeType = "", FileType = Core.Tools.GetStorageClass(x.StorageClass), }).ToList() }; if (filter.IsShowHash) { var fileList = GetList(new GetFileRangeParam(list.Items.Select(x => x.Key).ToList(), filter.PersistentOps)); list.Items.ForEach(item => { var fileInfo = fileList.FirstOrDefault(x => x.Key == item.Key); if (fileInfo != null) { item.Hash = fileInfo.Hash; } }); } return list; } return new ListFileItemResultDto(false, $"RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}"); }, message => new ListFileItemResultDto(false, message))); }