67

I'm trying to connect web and worker role. So i have a page where user can upload video files. Files are large so i can't use the query to send files. That's why i'm trying to upload them into the Blob Storage and then send the url by the query. But i don't know how to get this url.

Can anyone help me?

2
  • What do you mean with: Files are large so i can't use the query to send files? Jun 6, 2013 at 12:37
  • I'm talking about video files Jun 6, 2013 at 12:50

6 Answers 6

98
Answer recommended by Microsoft Azure Collective

Assuming you're uploading the blobs into blob storage using .Net storage client library by creating an instance of CloudBlockBlob, you can get the URL of the blob by reading Uri property of the blob.

static void BlobUrl()
{
    var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
    var cloudBlobClient = account.CreateCloudBlobClient();
    var container = cloudBlobClient.GetContainerReference("container-name");
    var blob = container.GetBlockBlobReference("image.png");
    blob.UploadFromFile("File Path ....");//Upload file....

    var blobUrl = blob.Uri.AbsoluteUri;
}

View example on Pastebin

5
  • 1
    Can you please put some code to get URL for recently uploaded blob (Image file)? Aug 7, 2015 at 12:14
  • 6
    TL;DR: just use cloudBlockBlob.Uri.AbsoluteUri
    – min
    Jul 11, 2016 at 3:49
  • 2
    what about the new library? (v 12 as of now) Jul 30, 2020 at 11:00
  • 1
    @devmiles.com...Get an instance of CloudBlob. It has a Uri property (learn.microsoft.com/en-us/dotnet/api/…) Jul 30, 2020 at 11:04
  • this is part of a depracated package now
    – mikus
    Sep 25, 2022 at 15:46
19

For python users, you can use blob_client.url

Unfortunately, it is not documented in https://learn.microsoft.com

from azure.storage.blob import BlobServiceClient
# get your connection_string - look at docs
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(storage_container_name)
You can call blob_client.url
blob_client = container_client.get_blob_client("myblockblob")
with open("pleasedelete.txt", "rb") as data:
    blob_client.upload_blob(data, blob_type="BlockBlob")
print(blob_client.url)

will return https://pleasedeleteblob.blob.core.windows.net/pleasedelete-blobcontainer/myblockblob

3
  • yeap! it is not documented and I got to know about it through .Net docs! Apr 29, 2021 at 17:11
  • good job! worked for me Mar 6 at 12:25
  • Hey man, this worked great! Would you know how to edit the pdf from the blob without actually downloading it ?
    – newbie101
    Jun 17 at 14:58
9

When you're using updated "Azure Storage Blobs" Package use below code.


BlobClient blobClient = containerClient.GetBlobClient("lg.jpg");

Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", containerClient.Uri);

using FileStream uploadFileStream = File.OpenRead(fileName);

if (uploadFileStream != null) 
    await blobClient.UploadAsync(uploadFileStream, true);

var absoluteUrl= blobClient.Uri.AbsoluteUri;
8

full working Javascript solution as of 2020:

const { BlobServiceClient } = require('@azure/storage-blob')

const AZURE_STORAGE_CONNECTION_STRING = '<connection string>'

async function main() {
  // Create the BlobServiceClient object which will be used to create a container client
  const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

  // Make sure your container was created
  const containerName = 'my-container'

  // Get a reference to the container
  const containerClient = blobServiceClient.getContainerClient(containerName);
  // Create a unique name for the blob
  const blobName = 'quickstart.txt';

  // Get a block blob client
  const blockBlobClient = containerClient.getBlockBlobClient(blobName);

  console.log('\nUploading to Azure storage as blob:\n\t', blobName);

  // Upload data to the blob
  const data = 'Hello, World!';
  await blockBlobClient.upload(data, data.length);
  
  console.log("Blob was uploaded successfully. requestId: ");
  console.log("Blob URL: ", blockBlobClient.url)
}

main().then(() => console.log('Done')).catch((ex) => console.log(ex.message));
2
  • When I try to access the image on the browser I get an error This XML file does not appear to have any style information associated with it. The document tree is shown below.
    – shAkur
    Mar 11, 2021 at 11:32
  • 2
    Thanks for this answer. Still working in 2022. Feb 21, 2022 at 6:05
0

Here is the V12 way. I can't guarantee my variable names are accurate, but the code works.

protected BlobContainerClient AzureBlobContainer
{
  get
  {
    if (!isConfigurationLoaded) { throw new Exception("AzureCloud currently has no configuration loaded"); }
    if (_azureBlobContainer == null)
    {
      if (!string.IsNullOrEmpty(_configuration.StorageEndpointConnection))
      {

        BlobServiceClient blobClient = new BlobServiceClient(_configuration.StorageEndpointConnection);
        BlobContainerClient container = blobClient.GetBlobContainerClient(_configuration.StorageContainer);
        container.CreateIfNotExists();
        _azureBlobContainer = container;
      }
    }
    return _azureBlobContainer;
  }
}

public bool UploadFileToCloudStorage(string fileName, Stream fileStream)
{
  BlobClient cloudFile = AzureBlobContainer.GetBlobClient(fileName);
  cloudFile.DeleteIfExists();
  fileStream.Position = 0;
  cloudFile.Upload(fileStream);
  return true;
}

public BlobClient UploadFileToCloudStorageWithResults(string fileName, Stream fileStream)
{
  BlobClient cloudFile = AzureBlobContainer.GetBlobClient(fileName);
  cloudFile.DeleteIfExists();
  fileStream.Position = 0;
  cloudFile.Upload(fileStream);

  return cloudFile;
}

public Stream DownloadFileStreamFromCloudStorage(string fileName)
{
  BlobClient cloudFile = AzureBlobContainer.GetBlobClient(fileName);
  Stream fileStream = new MemoryStream();
  cloudFile.DownloadTo(fileStream);
  return fileStream;
}
3
  • not async?????? Sep 11, 2022 at 18:39
  • eh. it works. its not perfect.
    – CarComp
    Sep 12, 2022 at 19:31
  • 1
    you're perfect. Sep 15, 2022 at 19:13
-6

Hey I am sorry I was not aware how I have posted the same comment once again in the answer. Please find my correct answer below with detail explanation of how this storage blob work in getting the url from blob.

// Add the connection string on the web.config file for your ease to get on multiple places if required.

<connectionStrings>

<add name="BlobStorageConnection" connectionString="DefaultEndpointsProtocol=https;AccountName=accName;AccountKey=xxxxxxxxxxxxxxxxxx YOU WILL FIND THIS in your AZURE ACCOUNT xxxxxxxxxx==;EndpointSuffix=core.windows.net"/>

This is the string which you can get from the web.config file.

string BlobConnectionString = ConfigurationManager.ConnectionStrings["BlobStorageConnection"].ConnectionString;
     public string GetFileURL()
        {
        //This will create the storage account to get the details of account.
        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(BlobConnectionString);

        //create client
        CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

        //Get a container
        CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("ContainerName");

        //From here we will get the URL of file available in Blob Storage.
        var blob1 = cloudBlobContainer.GetBlockBlobReference(imageName);
        string FileURL=blob1.Uri.AbsoluteUri;
        return FileURL;

    }

Like this way you can get the url of the File if you have the file (or Image) Name.

2
  • 1
    Did both of you find the same answer somewhere else, or did you just copy Gaurav's answer?
    – nivs1978
    Nov 27, 2019 at 6:51
  • @nivs1978 I do not how it got posted. Please find the correct and detailed explanation of getting the url of file from storage blob account.
    – ttr
    Nov 28, 2019 at 16:00

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.