I get the same error. This code was used to work before I upgraded to 2.0:
var sharedAccessPolicy = new SharedAccessBlobPolicy { SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-10), SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(30), Permissions = SharedAccessBlobPermissions.Read }; var sharedAccessSignature = _blockblob.GetSharedAccessSignature(sharedAccessPolicy); return _blockblob.Uri.AbsoluteUri + sharedAccessSignature;
I get uri:
http://127.0.0.1:10000/devstoreaccount1/original/c04d2a1c-980b-42c5-b76e-b71185f027b6.jpg?sv=2012-02-12&st=2012-11-20T08%3A30%3A24Z&se=2012-11-20T09%3A10%3A24Z&sr=b&sp=r&sig=9%2BVg6mSGqyrfr5rPlNJ6GSv%2BHN3J9k%2FWFRLYmx3xCvQ%3D
UPDATE RESOLVED:
In my code above, I have _blockBlob. It has been installed in constuctor with
var blobClient = account.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(containerName); CloudBlockBlob _blockblob = container.GetBlockBlobReference(fileName);
Change last line (as suggested by clausndk) to
ICloudBlob _test = container.GetBlobReferenceFromServer(fileName);
solves the problem, since calling GetSharedAccessSignature on _test results in a different (valid) signature.
After looking at the source code for Azure storage and using the debugger in my application, I found the cause of the problem. In my code, I have a container name with a trailing slash (original /). This is not a problem only when it comes to GetSharedAccessSignature. Here, an additional slash is placed in canonicalName (one slash is added to the code giving double slashes), and this invalidates the signature. The reason that GetBlobReferenceFromServer works is because it requests the servers (via the REST API) for blob, and as a result, CloudBlockBlob has a slash.
In my code, I removed the trailing slash, but Sandrino Di Mattia's solution for using .Trim ('/') in the container name also works. I think this is preferable to using GetBlobReferenceFromServer, as this will cause an additional server call.
We hope that the implementation of GetCanonicalName in CloudBlockBlobBase will be changed to handle trailing slashes in the future (I created the GitHub problem for this), but at the moment this "workaround" works.