Making the initial upload

Blob storage can be approached using scripts in a virtual machine. There are several examples and templates available for a multitude of programming languages (e.g., .NET, Java, Python). Below we provide a step-by-step description of how to make your initial data upload using Python code.

Setting up

Configure your storage connection string

On your local computer, create a text file that contains the Connection String and upload it to the workspace:

  1. Sign in to the Azure portal.

  2. Go to your storage account (dws###acronymdata).

  3. In the left hand panel, go to Access Key (under Settings).

  4. Copy the connection string belonging to key1 (see screenshot below).

  5. Save the string in a .txt file and upload to your workspace through the DRE portal.

In your virtual machine, find the connection key .txt file and run the following code in a command window:

Windows: cmd

  • setx AZURE_STORAGE_CONNECTION_STRING "<yourconnectionstring>"

Linux: Bash

  • export AZURE_STORAGE_CONNECTION_STRING="<yourconnectionstring>"

Now make sure to permanently remove the connection key .txt file by selecting it in your file explorer and pressing shift+del (this bypasses the bin).

Restart any open program that will be using Blob.

All further instructions below take place in the VM, unless stated otherwise

Install the Azure Blob storage client library for Python package

  • Search for and right-click Anaconda Prompt, choose run as administrator

  • Run the following commands:

    • conda install -c conda-forge azure-storage-blob

    • conda install -c conda-forge azure-mgmt-storage

Upload one file

  • Open Jupyter notebooks

  • Create new folder data

  • Create new notebook blob-initial-upload


Tip: You can run the cells below one by one and inspect the outcome to learn more about their functionality.

Set up the framework

import os, datetime

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__

try:

print("Azure Blob storage v" + __version__ + " - Python quickstart sample")

# Quick start code goes here

except Exception as ex:

print('Exception:')

print(ex)

Get the connection string

# Retrieve the connection string for use with the application. The storage

# connection string is stored in an environment variable on the machine

# running the application called AZURE_STORAGE_CONNECTION_STRING. If the environment variable is

# created after the application is launched in a console or with Visual Studio,

# the shell or application needs to be closed and reloaded to take the

# environment variable into account.

connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')

Upload initial blob to your container

In the code below, replace <container_name> with the name of your blob container.

# Create the BlobServiceClient object which will be used to create a container client

blob_service_client = BlobServiceClient.from_connection_string(connect_str)


# Your container

container_name = "<container_name>"


# Create a file in local data directory to upload and download

local_path = "./data"

local_file_name = "initial_upload_" + datetime.datetime.now().strftime("%Y-%m-%d_%H%M") + ".txt"

upload_file_path = os.path.join(local_path, local_file_name)


# Write some text to the file

file = open(upload_file_path, 'w')

file.write("This file was created on " + datetime.datetime.now() + " to initiate blob container " + container_name)

file.close()


# Create a blob client using the local file name as the name for the blob

blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)


print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)


# Upload the created file

with open(upload_file_path, "rb") as data:

blob_client.upload_blob(data)

List the blobs in your container

container_client = blob_service_client.get_container_client(container_name)

print("\nListing blobs...")


# List the blobs in the container

try:

for blob in container_client.list_blobs():

print("\t" + blob.name)

except ResourceNotFoundError:

print("Container not found.")

Next steps

Your blob is now also visible in your container in the DRE portal:

To see Blob storage sample apps, continue to:

Azure Blob storage SDK v12 Python samples

To learn more, see the Azure Storage client libraries for Python.

For tutorials, samples, quickstarts, and other documentation, visit Azure for Python Developers.