|
@@ -20,6 +20,8 @@ using TEAMModelOS.SDK.Context.Configuration;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Microsoft.Azure.Cosmos.Linq;
|
|
|
using System.Reflection.Metadata;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
|
+using Jaeger.Util;
|
|
|
|
|
|
namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
|
|
|
{
|
|
@@ -586,7 +588,7 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
|
|
|
// When the start time for the SAS is omitted, the start time is assumed to be the time when the storage service receives the request.
|
|
|
// Omitting the start time for a SAS that is effective immediately helps to avoid clock skew.
|
|
|
SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
|
|
|
- SharedAccessExpiryTime = DateTime.UtcNow.AddHours(2),
|
|
|
+ SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
|
|
|
Permissions = SharedAccessBlobPermissions.Read
|
|
|
};
|
|
|
|
|
@@ -606,32 +608,101 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
|
|
|
// Return the URI string for the container, including the SAS token.
|
|
|
return (blobContainer.Uri.ToString(), sasContainerToken);
|
|
|
}
|
|
|
-
|
|
|
- public async Task<object> GetContainerSasUri(BlobSas blobSas) {
|
|
|
- string sasContainerToken;
|
|
|
-
|
|
|
- blobContainer = await CreateContainer(blobSas.name);
|
|
|
+ public async Task<KeyValuePair<string, object>> GetContainerSasUri(BlobSas blobSas, bool isRead)
|
|
|
+ {
|
|
|
+ CloudBlobContainer blobContainer;
|
|
|
+ if (blobSas.role == "system")
|
|
|
+ {
|
|
|
+ blobContainer = await CreateContainer(_options.Container);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ blobContainer = await CreateContainer(blobSas.name.ToLower().Replace("#", ""));
|
|
|
+ }
|
|
|
// If no stored policy is specified, create a new access policy and define its constraints.
|
|
|
-
|
|
|
+
|
|
|
// Note that the SharedAccessBlobPolicy class is used both to define the parameters of an ad hoc SAS, and
|
|
|
// to construct a shared access policy that is saved to the container's shared access policies.
|
|
|
+ DateTimeOffset dateTime = DateTime.UtcNow.AddHours(1);
|
|
|
+ long time = dateTime.ToUnixTimeMilliseconds();
|
|
|
SharedAccessBlobPolicy adHocPolicy = new SharedAccessBlobPolicy()
|
|
|
- {
|
|
|
- // When the start time for the SAS is omitted, the start time is assumed to be the time when the storage service receives the request.
|
|
|
- // Omitting the start time for a SAS that is effective immediately helps to avoid clock skew.
|
|
|
- SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
|
|
|
- SharedAccessExpiryTime = DateTime.UtcNow.AddHours(2),
|
|
|
- Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Read
|
|
|
- };
|
|
|
+ {
|
|
|
+ // When the start time for the SAS is omitted, the start time is assumed to be the time when the storage service receives the request.
|
|
|
+ // Omitting the start time for a SAS that is effective immediately helps to avoid clock skew.
|
|
|
+ SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
|
|
|
+ SharedAccessExpiryTime = dateTime,
|
|
|
+ Permissions = SharedAccessBlobPermissions.Read
|
|
|
+ };
|
|
|
// Generate the shared access signature on the container, setting the constraints directly on the signature.
|
|
|
- sasContainerToken = blobContainer.GetSharedAccessSignature(adHocPolicy, null);
|
|
|
-
|
|
|
-
|
|
|
+ string sasContainerToken = blobContainer.GetSharedAccessSignature(adHocPolicy, null);
|
|
|
+ KeyValuePair<string, object> dict = new KeyValuePair<string, object>( blobContainer.Name, new { url = blobContainer.Uri, sas = sasContainerToken, timeout = time } );
|
|
|
+ return dict;
|
|
|
+ }
|
|
|
+ public async Task<Dictionary<string,object>> GetBlobSasUri(BlobSas blobSas,bool isRead) {
|
|
|
+ string sasBlobToken;
|
|
|
|
|
|
- // Return the URI string for the container, including the SAS token.
|
|
|
+ CloudBlobContainer blobContainer;
|
|
|
+ if (blobSas.role == "system")
|
|
|
+ {
|
|
|
+ blobContainer = await CreateContainer(_options.Container);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ blobContainer = await CreateContainer(blobSas.name.ToLower().Replace("#",""));
|
|
|
+ }
|
|
|
+ // Create a new access policy and define its constraints.
|
|
|
+ // Note that the SharedAccessBlobPolicy class is used both to define the parameters of an ad hoc SAS, and
|
|
|
+ // to construct a shared access policy that is saved to the container's shared access policies.
|
|
|
+ DateTimeOffset dateTime = DateTime.UtcNow.AddHours(1);
|
|
|
+ SharedAccessBlobPolicy adHocSAS = null;
|
|
|
+ if (isRead)
|
|
|
+ {
|
|
|
+ adHocSAS = new SharedAccessBlobPolicy()
|
|
|
+ {
|
|
|
+ // When the start time for the SAS is omitted, the start time is assumed to be the time when the storage service receives the request.
|
|
|
+ // Omitting the start time for a SAS that is effective immediately helps to avoid clock skew.
|
|
|
+ SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5),
|
|
|
+ SharedAccessExpiryTime = dateTime,
|
|
|
+ Permissions = SharedAccessBlobPermissions.Read
|
|
|
+ };
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ adHocSAS = new SharedAccessBlobPolicy()
|
|
|
+ {
|
|
|
+ // When the start time for the SAS is omitted, the start time is assumed to be the time when the storage service receives the request.
|
|
|
+ // Omitting the start time for a SAS that is effective immediately helps to avoid clock skew.
|
|
|
+ SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5),
|
|
|
+ SharedAccessExpiryTime = dateTime,
|
|
|
+ Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Read
|
|
|
+ };
|
|
|
|
|
|
- return (blobContainer.Uri.Scheme + "://" + blobContainer.Uri.Host.ToString(), blobContainer.Name, sasContainerToken);
|
|
|
+ }
|
|
|
+ Dictionary<string, object> dict = new Dictionary<string, object>();
|
|
|
+ long time = dateTime.ToUnixTimeMilliseconds();
|
|
|
+ List<string> folders = BaseConfigModel.Configuration.GetSection("Azure:Blob:" + blobSas.role).Get<List<string>>();
|
|
|
+ if (folders.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (string floder in folders) {
|
|
|
+ string cates = floder;
|
|
|
+ if (blobSas.role == "student") {
|
|
|
+ if (string.IsNullOrEmpty(blobSas.code)) {
|
|
|
+ throw new BizException("请设置学生编码!", ResponseCode.PARAMS_ERROR);
|
|
|
+ }
|
|
|
+ cates = floder.Replace("{studentId}", blobSas.code);
|
|
|
+ }
|
|
|
+ // Get a reference to a blob within the container.
|
|
|
+ // Note that the blob may not exist yet, but a SAS can still be created for it.
|
|
|
+ CloudBlockBlob blob = blobContainer.GetBlockBlobReference(cates);
|
|
|
+ // Generate the shared access signature on the blob, setting the constraints directly on the signature.
|
|
|
+ sasBlobToken = blob.GetSharedAccessSignature(adHocSAS);
|
|
|
+ dict.Add(cates, new { url=blob.Uri,sas=sasBlobToken , timeout = time });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dict;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 若要为 blob 创建服务 SAS,请调用 CloudBlob.GetSharedAccessSignature 方法。
|
|
|
///下面的代码示例在 blob 上创建 SAS。 如果提供现有存储访问策略的名称,则该策略与 SAS 关联。 如果未提供存储访问策略,则代码会在 Blob 上创建一个临时 SAS。
|