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?
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
For python users, you can use blob_client.url
Unfortunately it is not documented in docs.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
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));
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;