0

I have an application to upload files to azure blob storage. I have initially used connection string to upload and download file from blob storage. Now, I need to use sas token for upload and download files from blob, but i'm getting "The remote server returned an error: (404) Not Found." to download the files which i have uploaded using connection string.

1 Answers1

0

Just try the simple console app below to generate an SAS token and upload/download blobs:

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

Result:

enter image description here

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16