简体   繁体   中英

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.

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);
}

在此处输入图片说明 在此处输入图片说明

For more details, please refer to the sample .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM