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> 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); } }