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