我有一个将文件上传到 azure blob 存储的应用程序。我最初使用连接字符串从 blob 存储上传和下载文件。现在,我需要使用 sas 令牌从 blob 上传和下载文件,但我收到“远程服务器返回错误:(404)未找到”。下载我使用连接字符串上传的文件。
问问题
118 次
1 回答
0
只需尝试下面的简单控制台应用程序即可生成 SAS 令牌并上传/下载 Blob:
using System;
using Azure.Storage.Blobs;
using Azure.Storage.Sas;
namespace blobSasTest
{
class Program
{
static void Main(string[] args)
{
var connstr = "<storang account connection string>"; ;
var containerName = "<container name>";
var blobName = "test.txt"; //blob name, just 4 test here.
var destPath = "d:/temp/"; //temp path, just 4 test here.
//get sas token to upload and download
var sasURI = getSasToken4UploadAndDownload(connstr , containerName, blobName);
var blobClient = new BlobClient(sasURI);
//upload
blobClient.Upload(destPath + "test.txt",overwrite:true);
//download
blobClient.DownloadTo(destPath + "test2.txt");
}
public static Uri getSasToken4UploadAndDownload(string connstr , string container , string blob) {
var blobClient = new BlobContainerClient(connstr, container).GetBlobClient(blob);
var blobSasBuilder = new BlobSasBuilder
{
BlobContainerName = blobClient.BlobContainerName,
BlobName = blobClient.Name
};
//1 hour to expire
blobSasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(60);
//granting read,create,write permission to download and upload
blobSasBuilder.SetPermissions(BlobSasPermissions.Read | BlobSasPermissions.Write | BlobSasPermissions.Create);
var sas = blobClient.GenerateSasUri(blobSasBuilder);
return sas;
}
}
}
结果:
于 2021-06-15T01:51:37.483 回答