我尝试使用WindowsAzure.Storage 7.0.2预览从Xamarin.Forms PCL应用上传图像的流到Azure Blob。出于某种原因,StorageCredentials无法识别SAS令牌的AccountName。
var credentials = new StorageCredentials("https://<ACCOUNT-NAME>.blob.core.windows.net/...");
CloudStorageAccount Account = new CloudStorageAccount(credentials, true);
然后像这样上传:
public async Task<string> WriteFile(string containerName, string fileName, System.IO.Stream stream, string contentType = "")
{
var container = GetBlobClient().GetContainerReference(containerName);
var fileBase = container.GetBlockBlobReference(fileName);
await fileBase.UploadFromStreamAsync(stream);
if (!string.IsNullOrEmpty(contentType))
{
fileBase.Properties.ContentType = contentType;
await fileBase.SetPropertiesAsync();
}
return fileBase.Uri.ToString();
}
如何解决我的问题?是否有更好的上传到Azure存储的解决方案? 谢谢!
答案 0 :(得分:0)
接收SAS令牌的StorageCredentials构造函数只需要SAS令牌的查询部分,而不是完整的URI字符串。如果您拥有资源的完整URI(包括SAS令牌),则最常见的事情是直接使用该对象的构造函数。例如,如果您有URI:
string myBlobUri = @"https://myaccount.blob.core.windows.net/sascontainer/sasblob.txt?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D";
你可以这样创建一个blob对象:
CloudBlockBlob fileBase = new CloudBlockBlob(new Uri(myBlobUri));
如果您确实想要出于某种原因使用StorageCredentials和CloudStorageAccount类(例如,您可能有一个AccountSAS),可以采用以下方法:
string sasToken = @"sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D";
StorageCredentials credentials = new StorageCredentials(sasToken);
CloudStorageAccount account = new CloudStorageAccount(credentials, "myaccount", "core.windows.net", true)