0

的GCP python文檔具有以下功能的腳本:我創建了我的腳本的參數解析函數,它在多個參數(通過Python客戶端庫上傳多個文件到谷歌雲存儲

def upload_pyspark_file(project_id, bucket_name, filename, file): 
     """Uploads the PySpark file in this directory to the configured 
     input bucket.""" 
     print('Uploading pyspark file to GCS') 
     client = storage.Client(project=project_id) 
     bucket = client.get_bucket(bucket_name) 
     blob = bucket.blob(filename) 
     blob.upload_from_file(file) 

文件名)上傳到GCS存儲桶。我試圖調整上述函數來解析這些多個參數並上傳這些文件,但我不確定如何繼續。我的疑惑是上面的'文件名'和'文件'變量。我如何根據特定目的調整功能?

回答

1

我不認爲你仍然在尋找這樣的東西?

from google.cloud import storage 
import os 

files = os.listdir('data-files') 
client = storage.Client.from_service_account_json('cred.json') 
bucket = client.get_bucket('xxxxxx') 


def upload_pyspark_file(filename, file): 
    # """Uploads the PySpark file in this directory to the configured 
    # input bucket.""" 
    # print('Uploading pyspark file to GCS') 
    # client = storage.Client(project=project_id) 
    # bucket = client.get_bucket(bucket_name) 
    print('Uploading from ', file, 'to', filename) 
    blob = bucket.blob(filename) 
    blob.upload_from_file(file) 


for f in files: 
    upload_pyspark_file(f, "data-files\\{0}".format(f)) 

filefilename之間的區別是,你可能已經猜到了,file是源文件和filename是目標文件。

相關問題