Stasy Concelgoger
Stasy Concelgoger

Reputation: 746

how to get blob-URL after file upload in azure

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?

Upvotes: 72

Views: 115588

Answers (6)

gpaw
gpaw

Reputation: 349

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

Upvotes: 21

Duc Trung Mai
Duc Trung Mai

Reputation: 2608

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

Upvotes: 10

logeshpalani31
logeshpalani31

Reputation: 1638

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;

Upvotes: 13

CarComp
CarComp

Reputation: 2026

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

Upvotes: 0

ttr
ttr

Reputation: 9

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.

Upvotes: -6

Gaurav Mantri
Gaurav Mantri

Reputation: 136356

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

Upvotes: 104

Related Questions