Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Storage): Support file upload, delete #68

Merged
merged 14 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Isolation/Masa.Contrib.Isolation.MultiEnvironment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ Install-Package Masa.Contrib.Isolation.MultiEnvironment
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

1. 配置appsettings.json
1. Configure `appsettings.json`
``` appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;",
"Isolations": [
{
"Environment": "development",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"Environment": "staging",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;"
},
"IsolationConnectionStrings": [
{
"Environment": "development",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"Environment": "staging",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
```
* 1.1 When the current environment is development: database address: server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ Install-Package Masa.Contrib.Isolation.MultiEnvironment
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

1. 配置appsettings.json
1. 配置`appsettings.json`
``` appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;",
"Isolations": [
{
"Environment": "development",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"Environment": "staging",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;"
},
"IsolationConnectionStrings": [
{
"Environment": "development",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"Environment": "staging",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
```

Expand Down
26 changes: 13 additions & 13 deletions src/Isolation/Masa.Contrib.Isolation.MultiTenant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ Install-Package Masa.Contrib.Isolation.MultiTenant
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

1. 配置appsettings.json
1. Configure `appsettings.json`
``` appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;",
"Isolations": [
{
"TenantId": "00000000-0000-0000-0000-000000000002",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"TenantId": "00000000-0000-0000-0000-000000000003",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;"
},
"IsolationConnectionStrings": [
{
"TenantId": "00000000-0000-0000-0000-000000000002",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"TenantId": "00000000-0000-0000-0000-000000000003",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
```

Expand Down
26 changes: 13 additions & 13 deletions src/Isolation/Masa.Contrib.Isolation.MultiTenant/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ Install-Package Masa.Contrib.Isolation.MultiTenant
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

1. 配置appsettings.json
1. 配置`appsettings.json`
``` appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;",
"Isolations": [
{
"TenantId": "00000000-0000-0000-0000-000000000002",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"TenantId": "00000000-0000-0000-0000-000000000003",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;"
},
"IsolationConnectionStrings": [
{
"TenantId": "00000000-0000-0000-0000-000000000002",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"TenantId": "00000000-0000-0000-0000-000000000003",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/Isolation/Masa.Contrib.Isolation.UoW.EF/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Install-Package Masa.Contrib.Isolation.MultiTenant // Multi-tenant isolation On-
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

1. 配置appsettings.json
1. Configure `appsettings.json`
``` appsettings.json
{
"ConnectionStrings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Install-Package Masa.Contrib.Isolation.MultiTenant // 多租户隔离 按需引
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

1. 配置appsettings.json
1. 配置`appsettings.json`
``` appsettings.json
{
"ConnectionStrings": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Storage.ObjectStorage.Aliyun;

public abstract class BaseClient
{
protected readonly ICredentialProvider CredentialProvider;
protected readonly AliyunStorageOptions Options;

public BaseClient(ICredentialProvider credentialProvider,
AliyunStorageOptions options)
{
CredentialProvider = credentialProvider;
Options = options;
}

public virtual IOss GetClient()
{
var credential = GetCredential();
return new OssClient(Options.Endpoint, credential.AccessKeyId, credential.AccessKeySecret, credential.SecurityToken);
}

public virtual (string AccessKeyId, string AccessKeySecret, string? SecurityToken) GetCredential()
{
if (!CredentialProvider.SupportSts)
return new(Options.AccessKeyId, Options.AccessKeySecret, null);

var securityToken = CredentialProvider.GetSecurityToken();
return new(securityToken.AccessKeyId, securityToken.AccessKeySecret, securityToken.SessionToken);
}
}
166 changes: 109 additions & 57 deletions src/Storage/Masa.Contrib.Storage.ObjectStorage.Aliyun/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,33 @@

namespace Masa.Contrib.Storage.ObjectStorage.Aliyun;

public class Client : IClient
public class Client : BaseClient, IClient
{
private readonly AliyunStorageOptions _options;
private readonly IMemoryCache _cache;
private readonly bool _supportCallback;
private readonly ILogger<Client>? _logger;

public Client(AliyunStorageOptions options, IMemoryCache cache, ILogger<Client>? logger)
public Client(ICredentialProvider credentialProvider, AliyunStorageOptions options, ILogger<Client>? logger)
: base(credentialProvider, options)
{
_options = options;
_cache = cache;
_supportCallback = !string.IsNullOrEmpty(options.CallbackBody) && !string.IsNullOrEmpty(options.CallbackUrl);
_logger = logger;
}

public Client(ICredentialProvider credentialProvider, IOptionsMonitor<AliyunStorageOptions> options, ILogger<Client>? logger)
: this(credentialProvider, options.CurrentValue, logger)
{
}

/// <summary>
/// Obtain temporary authorization credentials through STS service
/// </summary>
/// <returns></returns>
public TemporaryCredentialsResponse GetSecurityToken()
{
if (!_cache.TryGetValue(_options.TemporaryCredentialsCacheKey, out TemporaryCredentialsResponse? temporaryCredentials))
{
temporaryCredentials = GetTemporaryCredentials(
_options.RegionId,
_options.AccessKeyId,
_options.AccessKeySecret,
_options.RoleArn,
_options.RoleSessionName,
_options.Policy,
_options.DurationSeconds);
SetTemporaryCredentials(temporaryCredentials);
}
return temporaryCredentials!;
if (!CredentialProvider.SupportSts)
throw new ArgumentException($"{nameof(Options.RoleArn)} or {nameof(Options.RoleSessionName)} cannot be empty or null");

return CredentialProvider.GetSecurityToken();
}

/// <summary>
Expand All @@ -44,47 +39,104 @@ public TemporaryCredentialsResponse GetSecurityToken()
/// <returns></returns>
public string GetToken() => throw new NotSupportedException("GetToken is not supported, please use GetSecurityToken");

protected virtual TemporaryCredentialsResponse GetTemporaryCredentials(
string regionId,
string accessKeyId,
string accessKeySecret,
string roleArn,
string roleSessionName,
string policy,
long durationSeconds)
public Task GetObjectAsync(
string bucketName,
string objectName,
Action<Stream> callback,
CancellationToken cancellationToken = default)
{
var client = GetClient();
var result = client.GetObject(bucketName, objectName);
callback.Invoke(result.Content);
return Task.CompletedTask;
}

public Task GetObjectAsync(
string bucketName,
string objectName,
long offset,
long length,
Action<Stream> callback,
CancellationToken cancellationToken = default)
{
if (length < 0 && length != -1)
throw new ArgumentOutOfRangeException(nameof(length), $"{length} should be greater than 0 or -1");

var client = GetClient();
var request = new GetObjectRequest(bucketName, objectName);
request.SetRange(offset, length > 0 ? offset + length : length);
var result = client.GetObject(request);
callback.Invoke(result.Content);
return Task.CompletedTask;
}

public Task PutObjectAsync(
string bucketName,
string objectName,
Stream data,
CancellationToken cancellationToken = default)
{
IClientProfile profile = DefaultProfile.GetProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
var request = new AssumeRoleRequest
{
ContentType = AliyunFormatType.JSON,
RoleArn = roleArn,
RoleSessionName = roleSessionName,
DurationSeconds = durationSeconds
};
if (!string.IsNullOrEmpty(policy))
request.Policy = policy;
var response = client.GetAcsResponse(request);

// if (response.HttpResponse.isSuccess())
// {
return new TemporaryCredentialsResponse( //todo: Get Sts response information is null, waiting for repair: https://github.com/aliyun/aliyun-openapi-net-sdk/pull/401
response.Credentials.AccessKeyId,
response.Credentials.AccessKeySecret,
response.Credentials.SecurityToken,
DateTime.Parse(response.Credentials.Expiration));
// }
//
// string message = $"Aliyun.Client: Failed to obtain temporary credentials, RequestId: {response.RequestId},Status: {response.HttpResponse.Status}, Message: {System.Text.Encoding.Default.GetString(response.HttpResponse.Content)}";
// _logger?.LogWarning(message);
//
// throw new Exception(message);
var client = GetClient();
var objectMetadata = _supportCallback ? BuildCallbackMetadata(Options.CallbackUrl, Options.CallbackBody) : null;
var result = !Options.EnableResumableUpload || Options.BigObjectContentLength > data.Length ?
client.PutObject(bucketName, objectName, data, objectMetadata) :
client.ResumableUploadObject(new UploadObjectRequest(bucketName, objectName, data)
{
PartSize = Options.PartSize,
Metadata = objectMetadata
});
_logger?.LogDebug("----- Upload {ObjectName} from {BucketName} - ({Result})",
objectName,
bucketName,
new UploadObjectResponse(result));
return Task.CompletedTask;
}

protected virtual ObjectMetadata BuildCallbackMetadata(string callbackUrl, string callbackBody)
{
string callbackHeaderBuilder = new CallbackHeaderBuilder(callbackUrl, callbackBody).Build();
var metadata = new ObjectMetadata();
metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder);
return metadata;
}

public Task<bool> ObjectExistsAsync(
string bucketName,
string objectName,
CancellationToken cancellationToken = default)
{
var client = GetClient();
var exist = client.DoesObjectExist(bucketName, objectName);
return Task.FromResult(exist);
}

public async Task DeleteObjectAsync(
string bucketName,
string objectName,
CancellationToken cancellationToken = default)
{
var client = GetClient();
if (await ObjectExistsAsync(bucketName, objectName, cancellationToken) == false)
return;

var result = client.DeleteObject(bucketName, objectName);
_logger?.LogDebug("----- Delete {ObjectName} from {BucketName} - ({Result})",
objectName,
bucketName,
result);
}

protected virtual void SetTemporaryCredentials(TemporaryCredentialsResponse credentials)
public Task DeleteObjectAsync(
string bucketName,
IEnumerable<string> objectNames,
CancellationToken cancellationToken = default)
{
var timespan = (DateTime.UtcNow - credentials.Expiration!.Value).TotalSeconds - 10;
if (timespan >= 0)
_cache.Set(_options.TemporaryCredentialsCacheKey, credentials, TimeSpan.FromSeconds(timespan));
var client = GetClient();
var result = client.DeleteObjects(new DeleteObjectsRequest(bucketName, objectNames.ToList(), Options.Quiet));
_logger?.LogDebug("----- Delete {ObjectNames} from {BucketName} - ({Result})",
objectNames,
bucketName,
result);
return Task.CompletedTask;
}
}
Loading