Pure.Krome
Pure.Krome

Reputation: 87047

How to use an Azure SAS when trying to copy an Azure File to an Azure Blob

I have a private Azure Blob. I'm trying to copy Azure File(s) over to the blob. These are both in the same Storage account.

When I try to copy the Azure file to the Blob, I'm getting a 404.

e.g. : await destinationBlob.StartCopyAsync(sourceCloudFile);

Assumption: This is because I need to provide a Shared Access Signature on the Azure File.

So, I'm not sure how to use the SAS to copy the File to the Blob.

var policy = new Microsoft.WindowsAzure.Storage.File.SharedAccessFilePolicy()
{
    Permissions = Microsoft.WindowsAzure.Storage.File.SharedAccessFilePermissions.List,
    SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.AddDays(1))
};
var sasToken = source.CloudFile.GetSharedAccessSignature(policy);

destinationBlob.Properties.ContentType = "image/jpg";

Ok - I have the token .. now what?

Upvotes: 1

Views: 846

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136366

Please change the permission in your SharedAccessFilePolicy. In order to copy a file, the permission should be Read and not List.

var policy = new Microsoft.WindowsAzure.Storage.File.SharedAccessFilePolicy()
{
    Permissions = Microsoft.WindowsAzure.Storage.File.SharedAccessFilePermissions.Read,
    SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.AddDays(1)
};

Once you get the SAS, you would simply create a SAS URL for the file and call StartCopy on your CloudBlockBlob object.

var sasToken = source.CloudFile.GetSharedAccessSignature(policy);
var fileSasUri = new Uri(string.Format("{0}{1}", source.CloudFile.Uri.AbsoluteUri, sasToken));
var copyId = await destinationBlob.StartCopyAsync(fileSasUri);

To set the content type of the destination blob, after the copy has been completed you can do something like:

destinationBlob.Properties.ContentType = "image/jpg";
await destinationBlob.SetPropertiesAsync();

Upvotes: 2

Related Questions