Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public virtual Task DeleteObjectAsync(GetOssObjectRequest request)
        {
            var objectName = request.Path.IsNullOrWhiteSpace()
                ? request.Object
                : request.Path.EnsureEndsWith('/') + request.Object;
            var filePath = CalculateFilePath(request.Bucket, objectName);

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            else if (Directory.Exists(filePath))
            {
                if (Directory.GetFileSystemEntries(filePath).Length > 0)
                {
                    throw new BusinessException(code: OssManagementErrorCodes.ObjectDeleteWithNotEmpty);
                }
                Directory.Delete(filePath);
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 3
0
        public virtual async Task DeleteObjectAsync(GetOssObjectRequest request)
        {
            var ossClient = await CreateClientAsync();

            var objectPath = GetBasePath(request.Path);

            var objectName = objectPath.IsNullOrWhiteSpace()
                ? request.Object
                : objectPath + request.Object;

            if (BucketExists(ossClient, request.Bucket) &&
                ObjectExists(ossClient, request.Bucket, objectName))
            {
                var objectListing = ossClient.ListObjects(request.Bucket, objectName);
                if (objectListing.CommonPrefixes.Any() ||
                    objectListing.ObjectSummaries.Any())
                {
                    throw new BusinessException(code: OssManagementErrorCodes.ObjectDeleteWithNotEmpty);
                    // throw new ObjectDeleteWithNotEmptyException("00201", $"Can't not delete oss object {request.Object}, because it is not empty!");
                }
                ossClient.DeleteObject(request.Bucket, objectName);
            }
        }
Exemplo n.º 4
0
        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);
            }
        }