public virtual async Task <OssObject> GetObjectAsync(GetOssObjectRequest request) { var ossClient = await CreateClientAsync(); if (!BucketExists(ossClient, request.Bucket)) { throw new BusinessException(code: OssManagementErrorCodes.ContainerNotFound); // throw new ContainerNotFoundException($"Can't not found container {request.Bucket} in aliyun blob storing"); } var objectPath = GetBasePath(request.Path); var objectName = objectPath.IsNullOrWhiteSpace() ? request.Object : objectPath + request.Object; if (!ObjectExists(ossClient, request.Bucket, objectName)) { throw new BusinessException(code: OssManagementErrorCodes.ObjectNotFound); // throw new ContainerNotFoundException($"Can't not found object {objectName} in container {request.Bucket} with aliyun blob storing"); } var aliyunOssObjectRequest = new GetObjectRequest(request.Bucket, objectName, request.Process); var aliyunOssObject = ossClient.GetObject(aliyunOssObjectRequest); var ossObject = new OssObject( !objectPath.IsNullOrWhiteSpace() ? aliyunOssObject.Key.Replace(objectPath, "") : aliyunOssObject.Key, request.Path, aliyunOssObject.Metadata.LastModified, aliyunOssObject.Metadata.ContentLength, aliyunOssObject.Metadata.LastModified, aliyunOssObject.Metadata.UserMetadata, aliyunOssObject.Key.EndsWith("/")) { FullName = aliyunOssObject.Key }; if (aliyunOssObject.IsSetResponseStream()) { var memoryStream = new MemoryStream(); await aliyunOssObject.Content.CopyToAsync(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); ossObject.SetContent(memoryStream); } return(ossObject); }
public virtual async Task <OssObject> CreateObjectAsync(CreateOssObjectRequest request) { var ossClient = await CreateClientAsync(); var objectPath = GetBasePath(request.Path); var objectName = objectPath.IsNullOrWhiteSpace() ? request.Object : objectPath + request.Object; if (!request.Overwrite && ObjectExists(ossClient, request.Bucket, objectName)) { throw new BusinessException(code: OssManagementErrorCodes.ObjectAlreadyExists); } // 当一个对象名称是以 / 结尾时,不论该对象是否存有数据,都以目录的形式存在 // 详情见:https://help.aliyun.com/document_detail/31910.html if (objectName.EndsWith("/") && request.Content.IsNullOrEmpty()) { var emptyStream = new MemoryStream(); var emptyData = System.Text.Encoding.UTF8.GetBytes(""); await emptyStream.WriteAsync(emptyData, 0, emptyData.Length); request.SetContent(emptyStream); } // 没有bucket则创建 if (!BucketExists(ossClient, request.Bucket)) { ossClient.CreateBucket(request.Bucket); } var aliyunObjectRequest = new PutObjectRequest(request.Bucket, objectName, request.Content) { Metadata = new ObjectMetadata() }; if (request.ExpirationTime.HasValue) { aliyunObjectRequest.Metadata.ExpirationTime = DateTime.Now.Add(request.ExpirationTime.Value); } var aliyunObject = ossClient.PutObject(aliyunObjectRequest); var ossObject = new OssObject( !objectPath.IsNullOrWhiteSpace() ? objectName.Replace(objectPath, "") : objectName, objectPath, DateTime.Now, aliyunObject.ContentLength, DateTime.Now, aliyunObject.ResponseMetadata, objectName.EndsWith("/") // 名称结尾是 / 符号的则为目录:https://help.aliyun.com/document_detail/31910.html ) { FullName = objectName }; if (!Equals(request.Content, Stream.Null)) { request.Content.Seek(0, SeekOrigin.Begin); ossObject.SetContent(request.Content); } return(ossObject); }
public virtual async Task <OssObject> GetObjectAsync(GetOssObjectRequest request) { var objectPath = !request.Path.IsNullOrWhiteSpace() ? request.Path.EnsureEndsWith('/') : ""; var objectName = objectPath.IsNullOrWhiteSpace() ? request.Object : objectPath + request.Object; var filePath = CalculateFilePath(request.Bucket, objectName); if (!File.Exists(filePath)) { if (!Directory.Exists(filePath)) { throw new BusinessException(code: OssManagementErrorCodes.ObjectNotFound); // throw new ContainerNotFoundException($"Can't not found object {objectName} in container {request.Bucket} with file system"); } var directoryInfo = new DirectoryInfo(filePath); var ossObject = new OssObject( directoryInfo.Name.EnsureEndsWith('/'), objectPath, directoryInfo.CreationTime, 0L, directoryInfo.LastWriteTime, new Dictionary <string, string> { { "LastAccessTime", directoryInfo.LastAccessTime.ToString("yyyy-MM-dd HH:mm:ss") } }, true) { FullName = directoryInfo.FullName.Replace(Environment.ContentRootPath, "") }; return(ossObject); } else { var fileInfo = new FileInfo(filePath); var ossObject = new OssObject( fileInfo.Name, objectPath, fileInfo.CreationTime, fileInfo.Length, fileInfo.LastWriteTime, new Dictionary <string, string> { { "IsReadOnly", fileInfo.IsReadOnly.ToString() }, { "LastAccessTime", fileInfo.LastAccessTime.ToString("yyyy-MM-dd HH:mm:ss") } }) { FullName = fileInfo.FullName.Replace(Environment.ContentRootPath, "") }; using (var fileStream = File.OpenRead(filePath)) { var memoryStream = new MemoryStream(); await fileStream.CopyToAsync(memoryStream); ossObject.SetContent(memoryStream); if (!request.Process.IsNullOrWhiteSpace()) { using var serviceScope = ServiceProvider.CreateScope(); var context = new FileSystemOssObjectContext(request.Process, ossObject, serviceScope.ServiceProvider); foreach (var processer in Options.Processers) { await processer.ProcessAsync(context); if (context.Handled) { ossObject.SetContent(context.Content); break; } } } } return(ossObject); } }
public virtual async Task <OssObject> CreateObjectAsync(CreateOssObjectRequest request) { var objectPath = !request.Path.IsNullOrWhiteSpace() ? request.Path.EnsureEndsWith('/') : ""; var objectName = objectPath.IsNullOrWhiteSpace() ? request.Object : objectPath + request.Object; var filePath = CalculateFilePath(request.Bucket, objectName); if (!request.Content.IsNullOrEmpty()) { ThrowOfPathHasTooLong(filePath); FileMode fileMode = request.Overwrite ? FileMode.Create : FileMode.CreateNew; if (!request.Overwrite && File.Exists(filePath)) { throw new BusinessException(code: OssManagementErrorCodes.ObjectAlreadyExists); // throw new OssObjectAlreadyExistsException($"Can't not put object {objectName} in container {request.Bucket}, Because a file with the same name already exists in the directory!"); } DirectoryHelper.CreateIfNotExists(Path.GetDirectoryName(filePath)); using (var fileStream = File.Open(filePath, fileMode, FileAccess.Write)) { await request.Content.CopyToAsync(fileStream); await fileStream.FlushAsync(); } var fileInfo = new FileInfo(filePath); var ossObject = new OssObject( fileInfo.Name, objectPath, fileInfo.CreationTime, fileInfo.Length, fileInfo.LastWriteTime, new Dictionary <string, string> { { "IsReadOnly", fileInfo.IsReadOnly.ToString() }, { "LastAccessTime", fileInfo.LastAccessTime.ToString("yyyy-MM-dd HH:mm:ss") } }) { FullName = fileInfo.FullName.Replace(Environment.ContentRootPath, "") }; ossObject.SetContent(request.Content); return(ossObject); } else { ThrowOfPathHasTooLong(filePath); if (Directory.Exists(filePath)) { throw new BusinessException(code: OssManagementErrorCodes.ObjectAlreadyExists); // throw new OssObjectAlreadyExistsException($"Can't not put object {objectName} in container {request.Bucket}, Because a file with the same name already exists in the directory!"); } Directory.CreateDirectory(filePath); var directoryInfo = new DirectoryInfo(filePath); var ossObject = new OssObject( directoryInfo.Name.EnsureEndsWith('/'), objectPath, directoryInfo.CreationTime, 0L, directoryInfo.LastWriteTime, new Dictionary <string, string> { { "LastAccessTime", directoryInfo.LastAccessTime.ToString("yyyy-MM-dd HH:mm:ss") } }, true) { FullName = directoryInfo.FullName.Replace(Environment.ContentRootPath, "") }; ossObject.SetContent(request.Content); return(ossObject); } }