How to move a blob data to Snowflake thru Python

1k views Asked by At

I am trying to move the data from ADLS blob to Snowflake table.

I am able to do the same with UI.

Steps followed for UI :

enter image description here

Generated the following SAS token :

sp=rl&st=2021-06-01T05:45:37Z&se=2021-06-01T13:45:37Z&spr=https&sv=2020-02-10&sr=c&sig=rYYY4o%2YY3jj%2XXXXXAB%2Bo8ygrtyAVCnPOxomlOc%3D

Able to load the table with the above token in Snowflake Web UI :

copy into FIRST_LEVEL.MOVIES
  from 'azure://adlsedmadifpoc.blob.core.windows.net/airflow-dif/raw-area/'
  credentials=(azure_sas_token='sp=rl&st=2021-06-01T05:45:37Z&se=2021-06-01T13:45:37Z&spr=https&sv=2020-02-10&sr=c&sig=rYYY4o%2YY3jj%2XXXXXAB%2Bo8ygrtyAVCnPOxomlOc%3D')
   FORCE = TRUE file_format = (TYPE = CSV);

I am trying to do the same with Python :

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

def generate_sas_token(file_name):

    sas = generate_blob_sas(account_name="xxxx",
account_key="p5V2GELxxxxQ4tVgLdj9inKwwYWlAnYpKtGHAg==", container_name="airflow-dif",blob_name=file_name,permission=BlobSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=2))
    print (sas)
    return sas

sas = generate_sas_token("raw-area/moviesDB.csv")

# Connectio string

conn = snowflake.connector.connect(user='xx',password='xx@123',account='xx.southeast-asia.azure',database='xx')

# Create cursor

cur = conn.cursor()
cur.execute(
            f"copy into FIRST_LEVEL.MOVIES FROM  'azure://xxx.blob.core.windows.net/airflow-dif/raw-area/moviesDB.csv'   credentials=(azure_sas_token='{sas}')  file_format = (TYPE = CSV) ;")
cur.execute(f" Commit  ;")
# Execute SQL statement
cur.close()
conn.close()

SAS token generated in the code :

se=2021-06-01T07%3A42%3A11Z&sp=rt&sv=2020-06-12&sr=b&sig=ZhZMPSI%yyyyAPTqqE0%3D

I am unable to use List permission while generating sas token thru python.

I am facing the below error :

    cursor=cursor,
snowflake.connector.errors.ProgrammingError: 091003 (22000): Failure using stage area. Cause: [Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. (Status Code: 403; Error Code: AuthenticationFailed)]

I might have list of csv files in future in that folder.

Any help appreciated. Thanks.

1

There are 1 answers

0
usr_lal123 On

The following code worked :

from azure.storage.blob import generate_container_sas, ContainerSasPermissions
from datetime import datetime,timedelta
import snowflake.connector

def get_sas_token():
    container_sas_token = generate_container_sas(
        account_name = 'XX',
        account_key = 'p5V2GEL3AqGuPMMYXXXQ4tVgLdj9inKwwYWlAnYpKtGHAg==',
        container_name = 'airflow-dif',
        permission=ContainerSasPermissions(read=True,list=True),
        expiry=datetime.utcnow() + timedelta(hours=1)
    )
    print (container_sas_token)
    return container_sas_token

sas = get_sas_token()

# Connectio string

conn = snowflake.connector.connect(user='XX',password='XX@123',account='XX.southeast-asia.azure',database='XX')

# Create cursor

cur = conn.cursor()
cur.execute(
            f"copy into FIRST_LEVEL.MOVIES FROM  'azure://XX.blob.core.windows.net/airflow-dif/raw-area/'   credentials=(azure_sas_token='{sas}')  FORCE = TRUE file_format = (TYPE = CSV) ;")

    
print (cur.fetchone())
cur.execute(f" Commit  ;")

# Execute SQL statement

cur.close()
conn.close()

Thank you Gaurav for your inputs.