Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a Single File to Blob Storage Azure

Tags:

c#

azure

How can I to Upload a file with C# ? I need to upload a file from a dialogWindow.

like image 415
user2476304 Avatar asked Sep 03 '13 16:09

user2476304


People also ask

How do I transfer files to BLOB storage?

To create a blob container in a storage account, right-click the Blob Containers node in that account, select Create Blob Container, and enter a name. To upload data to a container, select the target container and click the Upload button.

How do I send data to Azure Blob Storage?

Upload contents of a folder to Data Box Blob storage To get your account key, in the Azure portal, go to your storage account. Go to Settings > Access keys, select a key, and paste it into the AzCopy command. If the specified destination container does not exist, AzCopy creates it and uploads the file into it.

Which command upload files to a storage blob?

You can upload files and directories to Blob storage by using the AzCopy v10 command-line utility.

Can you store files in Azure Blob Storage?

Azure Blob storage is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data, such as text or binary data. Blob storage is ideal for: Serving images or documents directly to a browser.


2 Answers

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;    

// Retrieve storage account from connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("StorageKey");

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
}

see here about needed SDK and references

i think it's what you need

like image 154
Luis Miguel Avatar answered Oct 16 '22 11:10

Luis Miguel


Since WindowsAzure.Storage is legacy. With Microsoft.Azure.Storage.* we can use the following code to upload

  static async Task CreateBlob()
    {
        
        BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);
        
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
        
        BlobClient blobClient = containerClient.GetBlobClient(filename);

        
        using FileStream uploadFileStream = File.OpenRead(filepath);
        
        await blobClient.UploadAsync(uploadFileStream, true);
        uploadFileStream.Close();
    }
like image 6
rajat Avatar answered Oct 16 '22 12:10

rajat