gpt4 book ai didi

c# - 使用共享访问 key 上传到 Azure Blob 存储

转载 作者:可可西里 更新时间:2023-11-01 08:08:49 24 4
gpt4 key购买 nike

UPD:这是我的 implemented solution to this problem

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

我看过这个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 存储帐户的访问 key 。服务器将向客户端发出SAS,客户端将文件直接上传到Azure。因此,SAS 只能写入,不能读取,客户端将在服务器告诉它们的位置创建文件(容器、文件夹名称)

GetBlobReferenceFromServer 上出现问题 - 我从 Azure 存储收到 404 错误。是的,该 blob 不存在并且没有引用。那么给定 CloudBlobClient,我如何将文件上传到 blob?

附:我意识到有 REST API 可以处理这些事情。但我之前使用过 Microsoft.WindowsAzure.Storage 库,并且希望尽可能避免使用 REST 服务。

最佳答案

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?

要使 GetBlobReferenceFromServer 正常工作,Blob 必须存在于 Blob 存储中。当您知道 blob 存在于存储中并且想要找出 blob 的类型 - Block BlobPage Blob 时,这非常有用。

如果您想通过从本地计算机上传文件来创建 block 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/ 。您可以将其称为 Steve 博客文章的第二版:)。我展示了使用 REST API 和存储客户端库 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/

关于c# - 使用共享访问 key 上传到 Azure Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17257377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com