Ejemplo n.º 1
0
        public override async Task <string> SaveAsync(FileProviderSaveArgs args)
        {
            var fileName      = FileNameCalculator.Calculate(args);
            var configuration = args.Configuration.GetS3Configuration();
            var client        = GetS3Client(args);
            var containerName = GetContainerName(args);

            if (!args.OverrideExisting && await FileExistsAsync(client, containerName, fileName, args.CancellationToken))
            {
                throw new FileAlreadyExistsException($"Saving File '{args.FileId}' does already exists in the container '{containerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
            }

            if (configuration.CreateBucketIfNotExists)
            {
                await CreateBucketIfNotExists(client, containerName);
            }

            if (configuration.EnableSlice && (args.FileStream.Length > configuration.SliceSize))
            {
                await MultipartUploadAsync(client, containerName, fileName, args.FileStream, configuration.UseChunkEncoding, configuration.SliceSize);
            }
            else
            {
                await SingleUploadAsync(client, containerName, fileName, args.FileStream, configuration.UseChunkEncoding);
            }

            args.FileStream?.Dispose();

            return(fileName);
        }
Ejemplo n.º 2
0
        public override Task <string> GetAccessUrlAsync(FileProviderAccessArgs args)
        {
            if (!args.Configuration.HttpAccess)
            {
                return(Task.FromResult(string.Empty));
            }

            var fileName      = FileNameCalculator.Calculate(args);
            var client        = GetS3Client(args);
            var containerName = GetContainerName(args);
            var configuration = args.Configuration.GetS3Configuration();

            var preSignedUrlRequest = new GetPreSignedUrlRequest()
            {
                BucketName = containerName,
                Key        = fileName,
                Protocol   = (Protocol)configuration.Protocol
            };

            preSignedUrlRequest.Expires = args.Expires ?? Clock.Now.AddSeconds(600);

            var accessUrl = client.GetPreSignedURL(preSignedUrlRequest);

            return(Task.FromResult(accessUrl));
        }
Ejemplo n.º 3
0
        public override async Task <bool> ExistsAsync(FileProviderExistsArgs args)
        {
            var fileName      = FileNameCalculator.Calculate(args);
            var client        = GetS3Client(args);
            var containerName = GetContainerName(args);

            return(await FileExistsAsync(client, containerName, fileName, args.CancellationToken));
        }
Ejemplo n.º 4
0
        public override async Task <bool> DeleteAsync(FileProviderDeleteArgs args)
        {
            var configuration = args.Configuration.GetFastDFSConfiguration();

            ConfigurationFactory.AddIfNotContains(configuration);

            var fileId        = FileNameCalculator.Calculate(args);
            var containerName = GetContainerName(configuration, args);

            return(await Client.RemoveFileAsync(containerName, fileId, configuration.ClusterName));
        }
Ejemplo n.º 5
0
        public override async Task <bool> DownloadAsync(FileProviderDownloadArgs args)
        {
            var fileName      = FileNameCalculator.Calculate(args);
            var client        = GetS3Client(args);
            var containerName = GetContainerName(args);

            var getObjectResponse = await client.GetObjectAsync(containerName, fileName);

            await getObjectResponse.WriteResponseStreamToFileAsync(args.Path, true, args.CancellationToken);

            return(true);
        }
Ejemplo n.º 6
0
        public override async Task <bool> ExistsAsync(FileProviderExistsArgs args)
        {
            var configuration = args.Configuration.GetFastDFSConfiguration();

            ConfigurationFactory.AddIfNotContains(configuration);

            var fileId        = FileNameCalculator.Calculate(args);
            var containerName = GetContainerName(configuration, args);

            var storageNode = await Client.GetStorageNodeAsync(containerName, configuration.ClusterName);

            var fileInfo = await Client.GetFileInfo(storageNode, fileId, configuration.ClusterName);

            return(fileInfo != null);
        }
Ejemplo n.º 7
0
        public override async Task <bool> DeleteAsync(FileProviderDeleteArgs args)
        {
            var fileName      = FileNameCalculator.Calculate(args);
            var client        = GetS3Client(args);
            var containerName = GetContainerName(args);

            if (await FileExistsAsync(client, containerName, fileName, args.CancellationToken))
            {
                await client.DeleteAsync(containerName, fileName, null, args.CancellationToken);

                return(true);
            }

            return(false);
        }
Ejemplo n.º 8
0
        public override Task <string> GetAccessUrlAsync(FileProviderAccessArgs args)
        {
            if (!args.Configuration.HttpAccess)
            {
                return(Task.FromResult(string.Empty));
            }

            var configuration = args.Configuration.GetFastDFSConfiguration();

            ConfigurationFactory.AddIfNotContains(configuration);

            var fileId        = FileNameCalculator.Calculate(args);
            var containerName = GetContainerName(configuration, args);

            var accessUrl = BuildAccessUrl(configuration, containerName, fileId);

            return(Task.FromResult(accessUrl));
        }
Ejemplo n.º 9
0
        public override async Task <bool> DownloadAsync(FileProviderDownloadArgs args)
        {
            var configuration = args.Configuration.GetFastDFSConfiguration();

            ConfigurationFactory.AddIfNotContains(configuration);

            var fileId        = FileNameCalculator.Calculate(args);
            var containerName = GetContainerName(configuration, args);
            var storageNode   = await Client.GetStorageNodeAsync(containerName, configuration.ClusterName);

            try
            {
                await Client.DownloadFileEx(storageNode, fileId, args.Path, configuration.ClusterName);

                return(true);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogLevel.Error);
                return(false);
            }
        }
Ejemplo n.º 10
0
        public override async Task <Stream> GetOrNullAsync(FileProviderGetArgs args)
        {
            var configuration = args.Configuration.GetFastDFSConfiguration();

            ConfigurationFactory.AddIfNotContains(configuration);

            var fileId        = FileNameCalculator.Calculate(args);
            var containerName = GetContainerName(configuration, args);
            var storageNode   = await Client.GetStorageNodeAsync(containerName, configuration.ClusterName);

            try
            {
                var content = await Client.DownloadFileAsync(storageNode, fileId, configuration.ClusterName);

                return(new MemoryStream(content));
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogLevel.Error);
                return(null);
            }
        }
Ejemplo n.º 11
0
        public override async Task <Stream> GetOrNullAsync(FileProviderGetArgs args)
        {
            var fileName      = FileNameCalculator.Calculate(args);
            var client        = GetS3Client(args);
            var containerName = GetContainerName(args);

            if (!await FileExistsAsync(client, containerName, fileName, args.CancellationToken))
            {
                return(null);
            }

            var getObjectResponse = await client.GetObjectAsync(containerName, fileName);

            if (getObjectResponse.ResponseStream != null)
            {
                var memoryStream = new MemoryStream();
                await getObjectResponse.ResponseStream.CopyToAsync(memoryStream);

                return(memoryStream);
            }
            return(null);
        }
Ejemplo n.º 12
0
        public override Task <string> GetAccessUrlAsync(FileProviderAccessArgs args)
        {
            var fileName      = FileNameCalculator.Calculate(args);
            var client        = GetS3Client(args);
            var containerName = GetContainerName(args);

            var configuration = args.Configuration.GetS3Configuration();

            var preSignedUrlRequest = new GetPreSignedUrlRequest()
            {
                BucketName = containerName,
                Key        = fileName,
                Protocol   = (Protocol)configuration.Protocol
            };

            if (args.Expires.HasValue)
            {
                preSignedUrlRequest.Expires = args.Expires.Value;
            }

            var accessUrl = client.GetPreSignedURL(preSignedUrlRequest);

            return(Task.FromResult(accessUrl));
        }