使用SAS令牌连接到Azure CDN文件夹

ut6juiuv  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(147)

我有AzureBlobService.cs:

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

namespace AzureBlob.API.Service
{
    public class AzureBlobService
    {
        BlobServiceClient _blobClient;
        BlobContainerClient _containerClient;
        string azureConnectionString = "<Primary Connection String>";
        public AzureBlobService()
        {
            _blobClient = new BlobServiceClient(azureConnectionString);
            _containerClient = _blobClient.GetBlobContainerClient("apiimages");
        }

        public async Task<List<Azure.Response<BlobContentInfo>>> UploadFiles(List<IFormFile> files)
        {

            var azureResponse = new List<Azure.Response<BlobContentInfo>>();
            foreach (var file in files)
            {
                string fileName = file.FileName;
                using (var memoryStream = new MemoryStream())
                {
                    file.CopyTo(memoryStream);
                    memoryStream.Position = 0;
                    var client = await _containerClient.UploadBlobAsync(fileName, memoryStream, default);
                    azureResponse.Add(client);
                }
            };

            return azureResponse;
        }

        public async Task<List<BlobItem>> GetUploadedBlobs()
        {
            var items = new List<BlobItem>();
            var uploadedFiles = _containerClient.GetBlobsAsync();
            await foreach (BlobItem file in uploadedFiles)
            {
                items.Add(file);
            }

            return items;
        }
    }
}

如何更改此代码以使用SAS令牌连接到Azure CDN文件夹?

5uzkadbs

5uzkadbs1#

如何更改此代码,以便使用SAS令牌连接到Azure CDN文件夹?
要使用**SAS token**而不是连接字符串,您可以遵循本文档,它将帮助您连接存储帐户与SAS令牌。
您可以尝试下面的代码使用SAS令牌连接存储帐户以上传文件和检索上传的blob。

代码:

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

namespace AzureBlob.API.Service
{
    public class AzureBlobService
    {
        BlobServiceClient _blobClient;
        BlobContainerClient _containerClient;
        public AzureBlobService()
        {
            var sasToken = "<Your SAS-token without ?>";
            String blobUri ="https://<yourcdnendpoint>.azureedge.net";
            _blobClient = new BlobServiceClient(new Uri($"{blobUri}?{sasToken}"), null);
            _containerClient = _blobClient.GetBlobContainerClient("apiimages");
        }

        public async Task<List<Azure.Response<BlobContentInfo>>> UploadFiles(List<IFormFile> files)
        {

            var azureResponse = new List<Azure.Response<BlobContentInfo>>();
            foreach (var file in files)
            {
                string fileName = file.FileName;
                using (var memoryStream = new MemoryStream())
                {
                    file.CopyTo(memoryStream);
                    memoryStream.Position = 0;
                    var client = await _containerClient.UploadBlobAsync(fileName, memoryStream, default);
                    azureResponse.Add(client);
                }
            };

            return azureResponse;
        }

        public async Task<List<BlobItem>> GetUploadedBlobs()
        {
            var items = new List<BlobItem>();
            var uploadedFiles = _containerClient.GetBlobsAsync();
            await foreach (BlobItem file in uploadedFiles)
            {
                items.Add(file);
            }

            return items;
        }
    }
}

相关问题