上传到Azure的Blob存储与共享访问密钥 [英] Upload to Azure Blob Storage with Shared Access Key

查看:524
本文介绍了上传到Azure的Blob存储与共享访问密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPD:这里是我的<一个href=\"http://tech.trailmax.info/2013/07/upload-files-to-azure-blob-storage-with-using-shared-access-keys/\">implemented解决这个问题

我想上传到通过Azure.Storage库的Azure Blob存储(未REST API),并通过共享访问密码验证。

我已经看到了这博客文章,但API有由于后改变,现在我无法得到同样的结果。

下面是我:

  VAR blobClient =新CloudBlobClient(新的URI(blobWith​​Sas.BaseUri),新StorageCredentials(blobWith​​Sas.Sas));
//这里我收到404错误
VAR BLOB = blobClient.GetBlobReferenceFromServer(新的URI(blobWith​​Sas.AbsoluteUri));使用(VAR流=新的FileStream(fullFilePath,FileMode.Open))
{
    blob.UploadFromStream(流);
}

有:

blobWith​​Sas.BaseUri = http://127.0.0.1:10000/devstoreaccount1/a6dc9274-6ce1-4095-be6b-e84d1012cb24 (GUID是容器的名称,已经存在,创造了别的地方。)

blobWith​​Sas.Sas = <$c$c>?sv=2012-02-12&se=2013-06-23T03%3A04%3A53Z&sr=b&sp=w&sig=NaMqgXRMXDFvLAp8LTskgplAKp%2B9LCZzq8WK9Zo35x8%3D (也发布在其他地方在code)

blobWith​​Sas.AbsoluteUri = <$c$c>http://127.0.0.1:10000/devstoreaccount1/a6dc9274-6ce1-4095-be6b-e84d1012cb24/foldername/filename.txt

团块不存在,我要上传新文件,并创建一个blob。我有服务器应用程序持有获取关键Azure存储帐户。服务器会发出SAS的客户和客户端上传文件直接到Azure上。所以,SAS将只有写,其中服务器告诉他们没有阅读和客户端将创建的文件(容器,文件夹名称)

问题出现在 GetBlobReferenceFromServer - 我从Azure存储得到404错误。是的,斑点不存在并且没有参考。因此,考虑CloudBlobClient,我怎么可以将文件上传到一个blob?

P.S。我知道有这些东西REST API。不过,我已经使用 Microsoft.WindowsAzure.Storage 库之前,想尽量避免。REST服务


解决方案

  

问题出现在GetBlobReferenceFromServer - 我得到404错误
  从Azure存储。是的,斑点不存在,也没有
  参考。因此,考虑CloudBlobClient,我怎么可以上传文件到
  BLOB?


对于 GetBlobReferenceFromServer 工作,BLOB必须在Blob存储 present。 阻止斑点页斑点 - 当你知道BLOB存储存在,想找出blob的类型,这是在场景中有用

如果你想从你可以做类似的本地计算机上传文件来创建一个块BLOB:

  VAR一滴=新CloudBlockBlob(新的URI(blobWith​​Sas.AbsoluteUri),新StorageCredentials(blobWith​​Sas.Sas));
使用(VAR流=新的FileStream(fullFilePath,FileMode.Open))
{
    blob.UploadFromStream(流);
}

来到共享访问签名功能,我写了一篇博客文章不久以前这个问题:的<$c$c>http://gauravmantri.com/2013/02/13/revisiting-windows-azure-shared-access-signature/.你可以把它叫做史蒂夫的博客文章:)第2版。我已经展示了同时使用REST API和存储客户端库2.0。与共享访问签名上传的blob的例子

从博客张贴一些code样本:

使用存储客户端库:

  ///&LT;总结&gt;
///上载在SAS许可使用存储客户端库的blob容器中定义一个blob容器中的BLOB。
///&LT; /总结&gt;
///&LT; PARAM NAME =blobContainerSasUri&GT;&LT; /参数&GT;
静态无效UploadBlobWith​​StorageClientLibrarySasPermissionOnBlobContainer(字符串blobContainerSasUri)
{
    CloudBlobContainer blobContainer =新CloudBlobContainer(新的URI(blobContainerSasUri));
    CloudBlockBlob斑点= blobContainer.GetBlockBlobReference(sample.txt的);
    字符串sampleContent =这是示例文本。
    使用(MemoryStream的毫秒=新的MemoryStream(Encoding.UTF8.GetBytes(sampleContent)))
    {
        blob.UploadFromStream(毫秒);
    }
}

使用REST API:

  ///&LT;总结&gt;
///上载在SAS许可使用REST API一个blob容器中定义一个blob容器中的BLOB。
///&LT; /总结&gt;
///&LT; PARAM NAME =blobContainerSasUri&GT;&LT; /参数&GT;
静态无效UploadBlobWith​​RestAPISasPermissionOnBlobContainer(字符串blobContainerSasUri)
{
    字符串blobName =sample.txt的;
    字符串sampleContent =这是示例文本。
    INT CONTENTLENGTH = Encoding.UTF8.GetByteCount(sampleContent);
    字符串的queryString =(新的URI(blobContainerSasUri))查询。
    串blobContainerUri = blobContainerSasUri.Split('?')[0];
    字符串requestUri =的String.Format(CultureInfo.InvariantCulture,{0} / {1} {2},blobContainerUri,blobName,的queryString);
    HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(requestUri);
    request.Method =PUT;
    request.Headers.Add(X-MS-斑点型,BlockBlob);
    request.ContentLength = CONTENTLENGTH;
    使用(流requestStream = request.GetRequestStream())
    {
        requestStream.Write(Encoding.UTF8.GetBytes(sampleContent),0,CONTENTLENGTH);
    }
    使用(HttpWebResponse RESP =(HttpWebResponse)request.GetResponse())
    {    }
}

您也可以找到这个博客帖子有用:<一href=\"http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/\" rel=\"nofollow\"><$c$c>http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/

UPD: Here is my implemented solution to this problem

I'm trying to upload to Azure blob storage via Azure.Storage library (not REST API) and authenticating via Shared Access Key.

I have seen this blog post, but the API has changed since the post and now I can't get the same result.

Here is what I have:

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);
}

Having:

blobWithSas.BaseUri = http://127.0.0.1:10000/devstoreaccount1/a6dc9274-6ce1-4095-be6b-e84d1012cb24 (Guid is name of the container, already exist, created somewhere else.)

blobWithSas.Sas = ?sv=2012-02-12&se=2013-06-23T03%3A04%3A53Z&sr=b&sp=w&sig=NaMqgXRMXDFvLAp8LTskgplAKp%2B9LCZzq8WK9Zo35x8%3D (also issued somewhere else in the code)

blobWithSas.AbsoluteUri = http://127.0.0.1:10000/devstoreaccount1/a6dc9274-6ce1-4095-be6b-e84d1012cb24/foldername/filename.txt

The blob does not exist, I want to upload new file and create a blob. I have "Server" application holding Access Key to Azure Storage Account. Server would issue SAS to clients and clients upload files directly to Azure. So SAS would be only to write, no reading and clients will be creating files where server tells them to (container, folder names)

The problem comes up on GetBlobReferenceFromServer - I get 404 error from Azure Storage. Yes, the blob does not exist and there is no reference. So given CloudBlobClient, how can I upload a file to a blob?

p.s. I realise there is REST API for these things. But I've used Microsoft.WindowsAzure.Storage library before and would like to avoid REST service if possible.

解决方案

The problem comes up on GetBlobReferenceFromServer - I get 404 error from Azure Storage. Yes, the blob does not exist and there is no reference. So given CloudBlobClient, how can I upload a file to a blob?

For GetBlobReferenceFromServer to work, the blob must be present in the blob storage. This is useful in the scenario when you know that the blob exist in storage and would want to find out the type of blob - Block Blob or Page Blob.

If you want to create a block blob by uploading a file from the local computer you could do something like:

    var blob = new CloudBlockBlob(new Uri(blobWithSas.AbsoluteUri), new StorageCredentials(blobWithSas.Sas));
using (var stream = new FileStream(fullFilePath, FileMode.Open))
{
    blob.UploadFromStream(stream);
}

Coming to shared access signature functionality, I wrote a blog post not too long ago about this: http://gauravmantri.com/2013/02/13/revisiting-windows-azure-shared-access-signature/. You can call it version 2 of Steve's blog post:). I've shown examples of uploading blobs with shared access signature using both REST API and Storage Client Library 2.0.

Some code samples from the blog post:

Using 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);
    }
}

Using 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())
    {

    }
}

You may also find this blog post useful: http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/

这篇关于上传到Azure的Blob存储与共享访问密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆