使用共享访问密钥上载到Azure Blob存储

本文关键字:Azure Blob 存储 上载 密钥 共享 访问 | 更新日期: 2023-09-27 18:19:28

UPD:这是我实现的这个问题的解决方案

我正在尝试通过Azure.storage库(而不是REST API)上载到Azure blob存储,并通过共享访问密钥进行身份验证。

我看到了这篇博客文章,但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服务。

使用共享访问密钥上载到Azure Blob存储

GetBlobReferenceFromServer出现问题-我得到404错误来自Azure存储。是,blob不存在,也不存在参考因此,给定CloudBlobClient,我如何将文件上传到斑点?

若要使GetBlobReferenceFromServer工作,blob必须存在于blob存储中。当您知道blob存在于存储中并希望找出blob的类型(Block BlobPage 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/的博客文章。你可以称之为史蒂夫博客文章的第二版:)。我展示了使用REST API和Storage Client Library 2.0上传带有共享访问签名的Blob的示例。

博客文章中的一些代码示例:

使用存储客户端库:

/// <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())
    {
    }
}

您可能还发现这篇博客文章很有用:http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/