Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

c# - 如何使用最新的Azure SDK .NET API v12在Blob上获取共享访问签名?(How to get a Shared Access Signature on a Blob using the latest Azure SDK .NET API v12?)

I used to be able to create a shared access signature on a Blob using the v11 Azure SDK API, like this:

(我曾经能够使用v11 Azure SDK API在Blob上创建共享访问签名,如下所示:)

var containerName = "mycontainer";
var blobName = "myblob";

CloudStorageAccount storageAccount 
 = CloudStorageAccount.Parse(<StorageConnectionString>);

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer container = blobClient.GetContainerReference(containerName);


SharedAccessBlobPermissions permission = SharedAccessBlobPermissions.Read;

TimeSpan clockSkew = TimeSpan.FromMinutes(15d);
TimeSpan accessDuration = TimeSpan.FromMinutes(15d);

var blobSAS = new SharedAccessBlobPolicy
{
    SharedAccessStartTime = DateTime.UtcNow.Subtract(clockSkew),
    SharedAccessExpiryTime = DateTime.UtcNow.Add(accessDuration) + clockSkew,
    Permissions = permissions
};

CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

string sasBlobToken = blob.GetSharedAccessSignature(blobSAS);

...

I want to use the latest v12 .NET API which seems to replace CloudBlobClient by BlobServiceClient , CloudBlobContainer by BlobContainerClient and CloudBlockBlob by BlobClient .

(我想用最新的V12 .NET API,这似乎取代CloudBlobClientBlobServiceClientCloudBlobContainer通过BlobContainerClientCloudBlockBlob通过BlobClient 。)

However the method GetSharedAccessSignature that is available on a CloudBlockBlob instance is not available on a BlobClient instance.

(然而该方法GetSharedAccessSignature上可用的一个CloudBlockBlob实例不可在BlobClient实例。)

Question

()

How to get a shared access signature from a BlobClient instance using the latest Azure SDK .NET API v12?

(如何使用最新的Azure SDK .NET API v12从BlobClient实例获取共享访问签名?)

  ask by Kzrystof translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do as

(你可以做)

SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy
{
    // Expiration is in 12 hours.
    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(12),
    Permissions = permissions
};

and pass the values

(并传递值)

 // Generate the shared access signature on the container, setting the constraints directly on the signature
 CloudBlobContainer container = blobClient.GetContainerReference(containerName);
 string sasContainerToken = container.GetSharedAccessSignature(sasConstraints);

With V12, You can do

(使用V12,您可以)

 StorageSharedKeyCredential Credential = new StorageSharedKeyCredential(accountName, accountKey);
  // Use the key to get the SAS token.
 string sasToken = accountSasBuilder.ToSasQueryParameters(Credential).ToString();

SAMPLE CODE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...