问题
I would like to access a file located in azure file shares to copy it into blob storage. It works when I create Shared Access Signature by using the Microsoft Azure Storage Explorer by right-clicking on the file and going through the process and then hardcode generated value in the code. But I cannot generate this value using this C# code.
var sharedAccessFilePolicy = new SharedAccessFilePolicy()
{
Permissions = SharedAccessFilePermissions.Read,
SharedAccessStartTime = DateTime.Now,
SharedAccessExpiryTime = DateTime.Now.AddDays(1)
};
This code also generates an SAS but it does not work anyways. any idea? BTW I'm using Microsoft.Azure.Storage NuGet
sv=2019-02-02, sr=f, sig=****, se=****, sp=r"
st=2019-11-06T10****, se=2019-11-07T10**1**, sp=rl, sv=2018-03-28, sr=f, sig=****
the first one is generated by the code and the second one is the one that is comming from Microsoft Azure Storage Explorer
回答1:
The below code I have test, it could generate the file sas token and copy the file to blob.
static async System.Threading.Tasks.Task Main(string[] args)
{
var accountName = "accountname";
var accountKey = "your account key";
var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
var fileClient = account.CreateCloudFileClient();
var share = fileClient.GetShareReference("windows");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
var file = rootDir.GetFileReference("test.json");
var sasToken = file.GetSharedAccessSignature(new Microsoft.WindowsAzure.Storage.File.SharedAccessFilePolicy()
{
Permissions = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.Write,
SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.AddDays(1))
});
var uri = file.StorageUri.PrimaryUri + sasToken;
Uri fileuri = new Uri(uri);
Console.WriteLine(uri);
var blobclient = account.CreateCloudBlobClient();
var containerClient =blobclient.GetContainerReference("test");
var blob=containerClient.GetBlockBlobReference("test.json");
await blob.StartCopyAsync(fileuri);
}
Hope this could help you, if you still have other problem please feel free to let me know.
来源:https://stackoverflow.com/questions/58731067/create-a-share-account-signature-in-c-sharp