Hello community,
I am trying to run a Python file from the "Events" tab and I setup the Python Path in environment variable for an Anaconda installation.
I am following this link to run my Python code and there might be an error in my Path because when I run the workflow, the Python CMD doesn't appear like the CMD does when you execute a batch file.
The objective of this Python code is to download a set of PDF files from GCS to a local folder and the workflow uploads these files and other files in that folder to Sharepoint.
Can anyone help me with the best way to configure this Events tab to run this Python code?
When we are running the code using "Events", do we use the Virtual Environment that is setup by Alteryx (C:\Program Files\Alteryx\bin\Miniconda3\envs\DesignerBaseTools_vEnv) or the Base Virtual Environment that comes with our Python installation as I want to make sure where I should install my packages before I can run the code.
I already tested this code in a separate venv I have created separate from the base and ran the code and it downloads all the PDF files that are available in the folder on GCS, so the code is working as expected.
import os
import datetime
from google.cloud import storage
from google.oauth2 import service_account
# Set the path to the GCP key file
keyPath = r"path\to\gcp\key\file\here"
# Authenticate the credentials using the key file
credentialsForGCP = service_account.Credentials.from_service_account_file(
keyPath, scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
# Set the name of the bucket and folder containing PDF files to be copied to the local folder
bucketName = 'bucket-name-here'
folderName = 'folder/name/here'
# Set the local path where the files will be saved
# localPath = r'C:\Users\ssripat3\Documents\Python Scripts\PDF_Files'
localPath = r'path\to\local\folder\here'
# Establish a connection to the GCP account using the key file and authenticate the credentials
storageClient = storage.Client(credentials=credentialsForGCP)
# Fetch the bucket details from the GCP account
bucket = storageClient.bucket(bucketName)
# Get the list of blobs in the specified folder with the PDF file extension
blobs = bucket.list_blobs(prefix=folderName)
pdfBlobs = [blob for blob in blobs if blob.name.endswith('.pdf')]
# Copy the PDF files to the local folder with the current date and time appended to the filename
for blob in pdfBlobs:
# Get the base filename without the extension
baseFilename = os.path.splitext(os.path.basename(blob.name))[0]
# Get the current date and time in the desired format
currentTime = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
# Construct the new filename with the current date and time appended to the base filename
pdfFileNameWithDateTime = f'{baseFilename}_{currentTime}.pdf'
# Copy the file to the local folder with the new filename
blob.download_to_filename(os.path.join(localPath, pdfFileNameWithDateTime))Alteryx Version 2021.4