forked from loic-sharma/BaGet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Alibaba Cloud (Aliyun) OSS storage (loic-sharma#459)
- Loading branch information
Showing
11 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Use Alibaba (Aliyun) Object Storage Service | ||
|
||
You can store packages to [Alibaba(Aliyun) OSS](https://www.alibabacloud.com/product/oss). | ||
|
||
## Configure BaGet | ||
|
||
You can modify BaGet's configurations by editing the `appsettings.json` file. For the full list of configurations, please refer to [BaGet's configuration](../configuration.md) guide. | ||
|
||
### Aliyun OSS Storage | ||
|
||
Update the `appsettings.json` file: | ||
|
||
```json | ||
{ | ||
... | ||
|
||
"Storage": { | ||
"Type": "AliyunOss", | ||
"Endpoint": "oss-us-west-1.aliyuncs.com", | ||
"Bucket": "foo", | ||
"AccessKey": "", | ||
"AccessKeySecret": "", | ||
"Prefix": "lib/baget" // optional | ||
}, | ||
|
||
... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Aliyun.OSS; | ||
using BaGet.Aliyun.Configuration; | ||
using BaGet.Core; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace BaGet.Aliyun | ||
{ | ||
public class AliyunStorageService : IStorageService | ||
{ | ||
private const string Separator = "/"; | ||
private readonly string _bucket; | ||
private readonly string _prefix; | ||
private readonly OssClient _client; | ||
|
||
public AliyunStorageService(IOptionsSnapshot<AliyunStorageOptions> options, OssClient client) | ||
{ | ||
if (options == null) | ||
throw new ArgumentNullException(nameof(options)); | ||
|
||
_bucket = options.Value.Bucket; | ||
_prefix = options.Value.Prefix; | ||
_client = client ?? throw new ArgumentNullException(nameof(client)); | ||
|
||
if (!string.IsNullOrEmpty(_prefix) && !_prefix.EndsWith(Separator)) | ||
_prefix += Separator; | ||
} | ||
|
||
private string PrepareKey(string path) | ||
{ | ||
return _prefix + path.Replace("\\", Separator); | ||
} | ||
|
||
public async Task<Stream> GetAsync(string path, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
var ossObject = await Task.Factory.FromAsync(_client.BeginGetObject, _client.EndGetObject, _bucket, PrepareKey(path), null); | ||
|
||
return ossObject.ResponseStream; | ||
} | ||
catch (Exception) | ||
{ | ||
// TODO | ||
throw; | ||
} | ||
} | ||
|
||
public Task<Uri> GetDownloadUriAsync(string path, CancellationToken cancellationToken = default) | ||
{ | ||
var uri = _client.GeneratePresignedUri(_bucket, PrepareKey(path)); | ||
|
||
return Task.FromResult(uri); | ||
} | ||
|
||
public async Task<StoragePutResult> PutAsync(string path, Stream content, string contentType, CancellationToken cancellationToken = default) | ||
{ | ||
// TODO: Uploads should be idempotent. This should fail if and only if the blob | ||
// already exists but has different content. | ||
|
||
var metadata = new ObjectMetadata | ||
{ | ||
ContentType = contentType, | ||
}; | ||
|
||
var putResult = await Task<PutObjectResult>.Factory.FromAsync(_client.BeginPutObject, _client.EndPutObject, _bucket, PrepareKey(path), content, metadata); | ||
|
||
switch (putResult.HttpStatusCode) | ||
{ | ||
case System.Net.HttpStatusCode.OK: | ||
return StoragePutResult.Success; | ||
|
||
// TODO: check sdk documents | ||
//case System.Net.HttpStatusCode.Conflict: | ||
// return StoragePutResult.Conflict; | ||
|
||
//case System.Net.HttpStatusCode.Found: | ||
// return StoragePutResult.AlreadyExists; | ||
|
||
default: | ||
return StoragePutResult.Success; | ||
} | ||
} | ||
|
||
public Task DeleteAsync(string path, CancellationToken cancellationToken = default) | ||
{ | ||
_client.DeleteObject(_bucket, PrepareKey(path)); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<PackageTags>NuGet;Alibaba;Cloud</PackageTags> | ||
<Description>The libraries to host BaGet on Alibaba Cloud (Aliyun).</Description> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.9.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BaGet.Core\BaGet.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using BaGet.Core; | ||
|
||
namespace BaGet.Aliyun.Configuration | ||
{ | ||
public class AliyunStorageOptions | ||
{ | ||
[Required] | ||
public string AccessKey { get; set; } | ||
|
||
[Required] | ||
public string AccessKeySecret { get; set; } | ||
|
||
[Required] | ||
public string Endpoint { get; set; } | ||
|
||
[Required] | ||
public string Bucket { get; set; } | ||
|
||
public string Prefix { get; set; } | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/BaGet.Aliyun/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using Aliyun.OSS; | ||
using BaGet.Aliyun.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace BaGet.Aliyun.Extensions | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddAliyunStorageService(this IServiceCollection services) | ||
{ | ||
services.AddSingleton(provider => | ||
{ | ||
var options = provider.GetRequiredService<IOptions<AliyunStorageOptions>>().Value; | ||
|
||
return new OssClient(options.Endpoint, options.AccessKey, options.AccessKeySecret); | ||
}); | ||
|
||
services.AddTransient<AliyunStorageService>(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ public enum StorageType | |
AwsS3 = 2, | ||
GoogleCloud = 3, | ||
Null = 4, | ||
AliyunOss = 5, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters