Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file from URL to Azure BLOB

I have a file at a remote url such as http://www.site.com/docs/doc1.xls and i would like to copy that file onto my BLOB storage account.

I am aware and know of uploading files to a BLOB storage but wasn't sure how this can be done for a file from remote URL.

like image 465
user1144596 Avatar asked Dec 06 '13 00:12

user1144596


People also ask

How do I access Azure Blob storage via URL?

By default, the URL for accessing the Blob service in a storage account is https://<your account name>. blob.core.windows.net. You can map your own domain or subdomain to the Blob service for your storage account so that users can reach it using the custom domain or subdomain.

What URL format can blobs be accessed from Azure?

Users or client applications can access objects in Blob storage via HTTP/HTTPS, from anywhere in the world.


1 Answers

Try looking at CloudBlockBlob.StartCopyFromBlob that takes a URI if you are using the .NET client library.

string accountName = "accountname";
string accountKey = "key";
string newFileName = "newfile2.png";
string destinationContainer = "destinationcontainer";
string sourceUrl = "http://www.site.com/docs/doc1.xls";

CloudStorageAccount csa = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudBlobClient blobClient = csa.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference(destinationContainer);
blobContainer.CreateIfNotExists();
var newBlockBlob = blobContainer.GetBlockBlobReference(newFileName);
newBlockBlob.StartCopyFromBlob(new Uri(sourceUrl), null, null, null);

Gaurav posted about this when it first came out. Handy, and his post shows how to watch for completion since the operation is Async.

like image 64
MikeWo Avatar answered Sep 19 '22 02:09

MikeWo