0

I'm trying to test an upload file with azurite with my code

from datetime import datetime, timedelta
import requests
from azure.storage.blob import BlobSasPermissions, BlobServiceClient, generate_blob_sas

connect_str = "DefaultEndpointsProtocol=http;AccountName=myaccount;AccountKey=;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
container_name = "files"
blob_name = "device.log"
account_key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="


blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blob_client = blob_service_client.get_blob_client(
    container=container_name, blob=blob_name
)

sas_token = generate_blob_sas(
    account_name="devstoreaccount1",
    account_key="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==",
    container_name=container_name,
    blob_name="myblob",
    permission=BlobSasPermissions(create=True),
    expiry=datetime.utcnow() + timedelta(hours=1),
)

upload_url = blob_client.url + "?" + sas_token
print(upload_url)

local_path = "/home/nixos/nimbus/airfriskcomm/data/log/device.log"

with open(local_path, "rb") as file:
    file_contents = file.read()

headers = {
    "x-ms-blob-type": "BlockBlob",
    "Content-Type": "application/octet-stream",
    "Content-Length": str(len(file_contents)),
}

response = requests.put(upload_url, data=file_contents, headers=headers)

print(response.status_code)

but I keep on getting the response status code 403. I guess there is some problem in sas token I'm generating.

1 Answers1

0

When I ran your code by modifying few things like connection string, local path to repro the issue, i got the below error. error message:

PS C:\Users\vijay\Documents\testdir03>  c:; cd 'c:\Users\vijay\Documents\testdir03'; & 'C:\Users\vijay\AppData\Local\Programs\Python\Python311\python.exe' 'c:\Users\vijay\.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '62488' '--' 'C:\Users\vijay\Documents\testdir03\test.py'
http://127.0.0.1:10000/devstoreaccount1/files/device.log?se=2023-03-17T13%3A53%3A23Z&sp=c&sv=2021-12-02&sr=b&sig=u1QWm%2BiSsy%2BtN55eqm67NQtUw9rxMHghWR0KmYRO5YA%3D
403

After I change the blob_name value at line 20, I got the success output. enter image description here

from datetime import datetime, timedelta
import requests
from azure.storage.blob import BlobSasPermissions, BlobServiceClient, generate_blob_sas

connect_str = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
container_name = "files"
blob_name = "device.log"
account_key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="


blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blob_client = blob_service_client.get_blob_client(
    container=container_name, blob=blob_name
)

sas_token = generate_blob_sas(
    account_name="devstoreaccount1",
    account_key="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==",
    container_name=container_name,
    blob_name=blob_name,
    permission=BlobSasPermissions(create=True),
    expiry=datetime.utcnow() + timedelta(hours=1),
)

upload_url = blob_client.url + "?" + sas_token
print(upload_url)

local_path = "device.log"

with open(local_path, "rb") as file:
    file_contents = file.read()

headers = {
    "x-ms-blob-type": "BlockBlob",
    "Content-Type": "application/octet-stream",
    "Content-Length": str(len(file_contents)),
}

response = requests.put(upload_url, data=file_contents, headers=headers)

print(response.status_code)

enter image description here

Refer to this Python sample code to upload the blob to Azurite by using the SAS token | Github.com for more information.

HowAreYou
  • 605
  • 2
  • 6