cechode
cechode

Reputation: 1022

Shared Access moving from windowsazure.storage to Azure.Storage.Files

For the life of me, can not get how to set a shared access file policy in the new sdk. after uploading using the ShareFileClient i need to generate a url for another unauthenticated client to download. with the old sdk i did something like

        var sasConstraints = new SharedAccessFilePolicy() { 
            SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5) , 
            SharedAccessExpiryTime = DateTime.UtcNow.AddDays(10) , 
            Permissions = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.List };
        return myCloudFile.Uri + myCloudFile.GetSharedAccessSignature(sasConstraints);

Any and all pointers to accomplish this in the new sdk is greatly appreciated.

Upvotes: 0

Views: 718

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

According to my understanding, you want to a sas token for Azure File share with new Azure file sahre sdk. Regarding how to create it, please refer to the following code

  1. Install package
dotnet add package Azure.Storage.Files.Shares
  1. Code
# create sas token
 string accountname = "<account name>";
 string key = "<storage ket>";
 var creds = new StorageSharedKeyCredential(accountname, key);
var builder = new ShareSasBuilder
 {
    ShareName = "file share name",
    Protocol = SasProtocol.None,
    StartsOn = DateTimeOffset.UtcNow.AddHours(-1),
    ExpiresOn = DateTimeOffset.UtcNow.AddHours(+1)              
};
    builder.SetPermissions(ShareSasPermissions.All);
    var sas = builder.ToSasQueryParameters(creds).ToString();

// test (Upload file)
// Get a reference to a share 
ShareClient share = new ShareClient(connectionString, shareName);

// Get a reference to a directory and create it
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
directory.Create();

// Get a reference to a file and upload it
ShareFileClient file = directory.GetFileClient(fileName);
using (FileStream stream = File.OpenRead(localFilePath))
{
     await file.CreateAsync(stream.Length);
     var response =await file.UploadRangeAsync(
                    ShareFileRangeWriteType.Update,
                    new HttpRange(0, stream.Length),
                    stream);

     Console.WriteLine(response.GetRawResponse().Status);
}

enter image description here enter image description here

For more details, please refer to the sample.

Upvotes: 1

Related Questions