Skip to content

Commit

Permalink
complete azure blob storage
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Dec 6, 2024
1 parent 2517ceb commit 41033ca
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/BE/Chats.BE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
<PackageReference Include="AWSSDK.S3" Version="3.7.409" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0" />
<PackageReference Include="Azure.Identity" Version="1.13.1" /> <!-- fix security issue from Azure.AI.OpenAI -->
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" /> <!-- fix security issue from Azure.AI.OpenAI -->
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion src/BE/Services/FileServices/FileServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private IFileService CreateNoCache(DBFileServiceType fileServiceType, string con
DBFileServiceType.Minio => new MinioFileService(JsonSerializer.Deserialize<MinioConfig>(config)!),
DBFileServiceType.AwsS3 => new AwsS3FileService(JsonSerializer.Deserialize<AwsS3Config>(config)!),
DBFileServiceType.AliyunOSS => new AliyunOSSFileService(JsonSerializer.Deserialize<AliyunOssConfig>(config)!),
DBFileServiceType.AzureBlobStorage => new AzureBlobStorageFileService(config),
DBFileServiceType.AzureBlobStorage => new AzureBlobStorageFileService(JsonSerializer.Deserialize<AzureBlobStorageConfig>(config)!),
_ => throw new ArgumentException($"Unsupported file service type: {fileServiceType}")
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/BE/Services/FileServices/FileUploadRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public record CreateDownloadUrlRequest
public required int FileId { get; init; }
public required string StorageKey { get; init; }
public TimeSpan ValidPeriod { get; init; } = TimeSpan.FromHours(2);
public DateTime ValidEnd => DateTime.UtcNow + ValidPeriod;
public DateTimeOffset ValidEnd => DateTimeOffset.UtcNow + ValidPeriod;

public static CreateDownloadUrlRequest FromFile(DB.File file)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AliyunOSSFileService(AliyunOssConfig config) : IFileService

public Uri CreateDownloadUrl(CreateDownloadUrlRequest req)
{
return _oss.GeneratePresignedUri(config.Bucket, req.StorageKey, req.ValidEnd, SignHttpMethod.Get);
return _oss.GeneratePresignedUri(config.Bucket, req.StorageKey, req.ValidEnd.UtcDateTime, SignHttpMethod.Get);
}

public Task<Stream> Download(string storageKey, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Uri CreateDownloadUrl(CreateDownloadUrlRequest req)
{
BucketName = _config.Bucket,
Key = req.StorageKey,
Expires = req.ValidEnd,
Expires = req.ValidEnd.UtcDateTime,
Verb = HttpVerb.GET
});
return new Uri(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace Chats.BE.Services.FileServices.Implementations.AzureBlobStorage;

public record AzureBlobStorageConfig
{
[JsonPropertyName("connectionString")]
public required string ConnectionString { get; init; }

[JsonPropertyName("containerName")]
public required string ContainerName { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Sas;

namespace Chats.BE.Services.FileServices.Implementations.AzureBlobStorage;

public class AzureBlobStorageFileService(string azureStorageConnectionString) : IFileService
public class AzureBlobStorageFileService(AzureBlobStorageConfig config) : IFileService
{
private readonly BlobContainerClient _containerClient = new(config.ConnectionString, config.ContainerName);

public Uri CreateDownloadUrl(CreateDownloadUrlRequest req)
{
throw new NotImplementedException();
BlobClient blobClient = _containerClient.GetBlobClient(req.StorageKey);
return blobClient.GenerateSasUri(BlobSasPermissions.Read, req.ValidEnd);
}

public Task<Stream> Download(string storageKey, CancellationToken cancellationToken)
{
throw new NotImplementedException();
BlobClient blobClient = _containerClient.GetBlobClient(storageKey);
return blobClient.OpenReadAsync(new BlobOpenReadOptions(allowModifications: false), cancellationToken);
}

public Task<string> Upload(FileUploadRequest request, CancellationToken cancellationToken)
public async Task<string> Upload(FileUploadRequest request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
SuggestedStorageInfo suggestedStorageInfo = SuggestedStorageInfo.FromFileName(request.FileName);
BlobClient blobClient = _containerClient.GetBlobClient(suggestedStorageInfo.StorageKey);
_ = await blobClient.UploadAsync(request.Stream, new BlobUploadOptions
{
HttpHeaders = new BlobHttpHeaders { ContentType = request.ContentType },
}, cancellationToken);
return suggestedStorageInfo.StorageKey;
}
}

0 comments on commit 41033ca

Please sign in to comment.