Showing posts with label Azure Blob Storage. Show all posts
Showing posts with label Azure Blob Storage. Show all posts

Programmatically upload a file to Azure Blob Storage (ADLS) using C# .Net

Today we will learn on how to programmatically upload a file to Azure Blob Storage (ADLS) using C# .Net
Steps:
  • Create a test file called "sample-blob" and write "Test" inside the file.
  • Open visual studio and create a new console application
  • Install Azure storage blob client library for .Net using Nuget:
    • dotnet add package Azure.Storage.Blobs
  • Create a C# class for uploading the file to blog
    • Import the Azure.Storage.Blobs library
    • Create a reference to the container where you want the file to be uploaded
    • Get a reference to the blob (file name)
    • call the blog.upload(local_file_path) function by the path to the file that you want to upload to Azure blob
    • Now, run the script.
    • Once the script finishes execution, go to Azure blob and check if the file has been uploaded or not
    • Open the file to see if the file contains "test" word in it or not.
                  
    • You are done





AzCopy - copy files from on-premise location to Azure Blog Storage (ADLS)

Today we will learn on how to copy files from on-premise to Azure Blob Storage (ADLS) using AzCopy


Pre-Requisite:
  • Download AzCopy utility from here  (microsoft site)
  • Install the AzCopy.exe in the machine that you'll be using to run the copy commands
  • Open the Azcopy command line utility and start writing the commands
Steps to deploy AzCopy Script to copy Images:
  1. Authenticate a user identity:
    1. Use the script in CMD to authenticate your identity.


    • Replace the <tenant-id> placeholder with the tenant ID of the organization to which the storage account belongs. To find the tenant ID, select the Azure Active Directory > Properties > Directory ID in the Azure portal.

    • Eg.: azcopy login --tenant-id = 5dasda9fa7da79da7-abec-4505-81ea6-8csda2


Once you enter the command "azcopy login --tenant-id = <tenant-id>, it will return a code and URL of a website.
  • Open the website
  • Provide the code
  • Click Next button
  • A Sign-in window will appear.
  • Sign-in using your azure account credentials
  • After you've successfully signed in, you can close the browser window and begin using AzCopy
To login using other means (service principle or User assigned identity), get the appropriate command from the below link:
      
2. To copy files from On-prem location to ADLS, use the below command:
  • azcopy sync " <Select-folder-path>" "https:// <storage-account-name>/ <container-name> <SAS Token>" --recursive=true 
Note: Please use double quotation for file path if you’re using Windows machine. 
  • AzCopy sync will start a non-stop copy process where AzCopy will keep on copying the files unless manually terminated.
  • If you just want to just trigger the process one time, use the copy parameter instead of sync.
Copy single file:
  • azcopy cp "/path/to/file.txt" "https://[account].blob.core.windows.net/[container]/[path/to/blob]?[SAS]"
Copy entire directory:
  • azcopy cp "/path/to/dir" "https://[account].blob.core.windows.net/[container]/[path/to/directory]?[SAS]" --recursive=true
To get the SAS token:
  • Go to your storage account from Azure Portal
  • Click Shared Access Signature 
  • Copy the SAS token