73

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? Commented Jun 6, 2013 at 12:37
  • I'm talking about video files Commented Jun 6, 2013 at 12:50

6 Answers 6

105
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

Sign up to request clarification or add additional context in comments.

5 Comments

Can you please put some code to get URL for recently uploaded blob (Image file)?
TL;DR: just use cloudBlockBlob.Uri.AbsoluteUri
what about the new library? (v 12 as of now)
@devmiles.com...Get an instance of CloudBlob. It has a Uri property (learn.microsoft.com/en-us/dotnet/api/…)
this is part of a depracated package now
21

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

4 Comments

yeap! it is not documented and I got to know about it through .Net docs!
good job! worked for me
Hey man, this worked great! Would you know how to edit the pdf from the blob without actually downloading it ?
such url is not publicly accessible, when trying to open this url, I am getting error "Public access is not permitted on this storage account"
13

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;

1 Comment

this works as of 2024, with the new library.
10

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

1 Comment

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.
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 Comments

not async??????
eh. it works. its not perfect.
you're perfect.
-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 Comments

Did both of you find the same answer somewhere else, or did you just copy Gaurav's answer?
@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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.