1

I try to upload a simple file to azure blob storage with the below code. When I run first-time "mu_blob" created but samplesource.txt didn't upload. When run second-time I received this error and the file didn't upload.

ErrorCode:BlobAlreadyExists
Error:None

from azure.storage.blob import BlobClient

blob = BlobClient.from_connection_string(conn_str="*****", container_name="test", blob_name="my_blob")

with open("./SampleSource.txt", "rb") as data:
    blob.upload_blob(data)

enter image description here

6
  • upload_blob has a parameter if you want to overwrite or not Commented Feb 20, 2021 at 12:13
  • @ChristianSloper My problem is the SampleSource.txt didn't upload on the blob. Commented Feb 20, 2021 at 13:18
  • note that samplesource is not uploaeded.. it uploads to a blob called "my_blob" Commented Feb 20, 2021 at 13:40
  • @ChristianSloper I know but it's empty. I added a screenshot. Commented Feb 20, 2021 at 13:45
  • add overwrite=True, and blob_type='BlockBlob' ? Commented Feb 20, 2021 at 14:05

1 Answer 1

3

This is probably because you are creating blob with different name than SampleSource.txt. Check out below code sample to understand uploading of blob better:

# Create a local directory to hold blob data
local_path = "./data"
os.mkdir(local_path)

# Create a file in the local data directory to upload and download
local_file_name = str(uuid.uuid4()) + ".txt"
upload_file_path = os.path.join(local_path, local_file_name)

# Write text to the file
file = open(upload_file_path, 'w')
file.write("Hello, World!")
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)

I would further suggest you to go through Quickstart: Manage blobs with Python v12 SDK.

Sign up to request clarification or add additional context in comments.

1 Comment

I had this issue and my solution as to use "from_connection_string" vs. other connection methods. The person who submitted the original question should check that their connection string is correct and also check that it's from the correct storage account.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.