UPD:这是我的implemented solution to this problem
我正在尝试通过Azure.Storage库(不是REST API)上传到Azure blob存储,并通过共享访问密钥进行身份验证。
我见过这个blog post,但是自帖子以来API已经发生了变化,现在我无法得到相同的结果。
这就是我所拥有的:
var blobClient = new CloudBlobClient(new Uri(blobWithSas.BaseUri), new StorageCredentials(blobWithSas.Sas));
// here I receive 404 error
var blob = blobClient.GetBlobReferenceFromServer(new Uri(blobWithSas.AbsoluteUri));
using (var stream = new FileStream(fullFilePath, FileMode.Open))
{
blob.UploadFromStream(stream);
}
拥有:
blobWithSas.BaseUri
= http://127.0.0.1:10000/devstoreaccount1/a6dc9274-6ce1-4095-be6b-e84d1012cb24
(Guid是容器的名称,已经存在,在其他地方创建。)
blobWithSas.Sas
= ?sv=2012-02-12&se=2013-06-23T03%3A04%3A53Z&sr=b&sp=w&sig=NaMqgXRMXDFvLAp8LTskgplAKp%2B9LCZzq8WK9Zo35x8%3D
(也在代码中的其他位置发布)
blobWithSas.AbsoluteUri
= http://127.0.0.1:10000/devstoreaccount1/a6dc9274-6ce1-4095-be6b-e84d1012cb24/foldername/filename.txt
blob不存在,我想上传新文件并创建一个blob。我有“服务器”应用程序持有Azure存储帐户的访问密钥。服务器将向客户端发出SAS,客户端将文件直接上载到Azure。所以SAS只会写,没有阅读,客户端会创建服务器告诉他们的文件(容器,文件夹名称)
问题出现在GetBlobReferenceFromServer
- 我从Azure存储中收到404错误。是的,blob不存在且没有引用。因此,给定CloudBlobClient,如何将文件上传到blob?
P.S。我意识到这些东西有REST API。但我之前使用过Microsoft.WindowsAzure.Storage
库,如果可能的话,我希望避免使用REST服务。
答案 0 :(得分:14)
问题出现在GetBlobReferenceFromServer上 - 我收到404错误 来自Azure存储。是的,blob不存在而且没有 参考。因此,给定CloudBlobClient,我如何将文件上传到 团块?
要使GetBlobReferenceFromServer
生效,blob必须存在于Blob存储中。当您知道blob存在于存储中并且想要找出blob的类型 - Block Blob
或Page Blob
时,这非常有用。
如果要通过从本地计算机上传文件来创建块blob,可以执行以下操作:
var blob = new CloudBlockBlob(new Uri(blobWithSas.AbsoluteUri), new StorageCredentials(blobWithSas.Sas));
using (var stream = new FileStream(fullFilePath, FileMode.Open))
{
blob.UploadFromStream(stream);
}
为了实现共享访问签名功能,我不久前写了一篇博客文章:http://gauravmantri.com/2013/02/13/revisiting-windows-azure-shared-access-signature/
。您可以将其称为史蒂夫博客文章的第2版:)。我已经展示了使用REST API和Storage Client Library 2.0上传带有共享访问签名的Blob的示例。
博客文章中的一些代码示例:
使用Storage Client Library:
/// <summary>
/// Uploads a blob in a blob container where SAS permission is defined on a blob container using storage client library.
/// </summary>
/// <param name="blobContainerSasUri"></param>
static void UploadBlobWithStorageClientLibrarySasPermissionOnBlobContainer(string blobContainerSasUri)
{
CloudBlobContainer blobContainer = new CloudBlobContainer(new Uri(blobContainerSasUri));
CloudBlockBlob blob = blobContainer.GetBlockBlobReference("sample.txt");
string sampleContent = "This is sample text.";
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sampleContent)))
{
blob.UploadFromStream(ms);
}
}
使用REST API:
/// <summary>
/// Uploads a blob in a blob container where SAS permission is defined on a blob container using REST API.
/// </summary>
/// <param name="blobContainerSasUri"></param>
static void UploadBlobWithRestAPISasPermissionOnBlobContainer(string blobContainerSasUri)
{
string blobName = "sample.txt";
string sampleContent = "This is sample text.";
int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
string queryString = (new Uri(blobContainerSasUri)).Query;
string blobContainerUri = blobContainerSasUri.Split('?')[0];
string requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/{1}{2}", blobContainerUri, blobName, queryString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "PUT";
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.ContentLength = contentLength;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
}
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
}
}