簡體   English   中英

使用 Azure 服務自動運行 Python 代碼

[英]Automating running Python code using Azure services

大家好 Stackoverflow,

我寫了兩個 python 腳本。 一個腳本用於獲取本地文件並將它們發送到 GCS(谷歌雲存儲)。 另一個相反 - 從 GCS 中獲取上傳的文件並保存在本地。

我想使用 Azure 使流程自動化。

你會推薦使用什么? Azure Function App,Azure Logic App 或其他服務?

* 我現在正在嘗試使用 Logic App。 我使用 pyinstaller 制作了 .exe 文件,並在邏輯應用程序中尋找將運行我的程序(.exe 文件)的連接器。 我在邏輯應用程序中有觸發器 - “添加或修改文件時”,但現在我在選擇下一步(連接器)時堆疊。

親切的問候,安娜

根據要求添加代碼:

from google.cloud import storage
import os
import glob
import json

    # Finding path to config file that is called "gcs_config.json" in directory C:/
    def find_config(name, path):
        for root, dirs, files in os.walk(path):
            if name in files:
                return os.path.join(root, name)
    
    def upload_files(config_file):
        # Reading 3 Parameters for upload from JSON file
        with open(config_file, "r") as file:
            contents = json.loads(file.read())
            print(contents)
    
        # Setting up login credentials
        os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = contents['login_credentials']
        # The ID of GCS bucket
        bucket_name = contents['bucket_name']
        # Setting path to files
        LOCAL_PATH = contents['folder_from']
    
        for source_file_name in glob.glob(LOCAL_PATH + '/**'):
    
        # For multiple files upload
        # Setting destination folder according to file name 
            if os.path.isfile(source_file_name):
                partitioned_file_name = os.path.split(source_file_name)[-1].partition("-")
                file_type_name = partitioned_file_name[0]
    
                # Setting folder where files will be uploaded
                destination_blob_name = file_type_name + "/" + os.path.split(source_file_name)[-1]
    
                # Setting up required variables for GCS 
                storage_client = storage.Client()
                bucket = storage_client.bucket(bucket_name)
                blob = bucket.blob(destination_blob_name)
    
                # Running upload and printing confirmation message
                blob.upload_from_filename(source_file_name)
                print("File from {} uploaded to {} in bucket {}.".format(
                    source_file_name, destination_blob_name, bucket_name
                ))
    
    config_file = find_config("gcs_config.json", "C:/")
    
    upload_files(config_file)

配置 json:

{
"login_credentials": "C:/Users/AS/Downloads/bright-velocity-___-53840b2f9bb4.json",
"bucket_name": "staging.bright-velocity-___.appspot.com",
"folder_from": "C:/Users/AS/Documents/Test2/",
"folder_for_downloaded_files": "C:/Users/AnnaShepilova/Documents/DownloadedFromGCS2/",

"given_date": "",
"given_prefix": ["Customer", "Account"] }

目前, Logic Apps中沒有用於與Google Cloud Services交互的內置連接器 但是,您可以使用Google Cloud Storage在您的邏輯應用程序Function 應用程序中提供 REST API。

在此處輸入圖像描述

但我的建議是你可以使用Azure Function來做這些事情。 因為azure Function可以更靈活的寫自己的流程來做任務。

請參閱Azure function 中運行您的 .exe 文件 如果您使用的是Local EXECloud Environment exe

請參閱此處了解更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM