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.
Asked
Active
Viewed 462 times
0
-
Could you please tell me how you create as token? – Jim Xu Jun 14 '21 at 00:43
-
Which programming language you are using? – Stanley Gong Jun 14 '21 at 01:56
-
im creating token from azure portal @ji – sivaranjini Jun 14 '21 at 11:08
-
c# @StanleyGong – sivaranjini Jun 14 '21 at 11:09
-
How's going?Has your issue got resolved? If my post resolves your issue, pls click the checkmark beside my answer – Stanley Gong Jun 17 '21 at 14:36
1 Answers
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:

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