在Python中调用gsutil出现语法错误,求GCS文件下载Python脚本
Hey there, let's sort out that "Invalid Syntax" error you're getting when calling gsutil from Python, and get you up and running with a working script.
常见错误原因
Most often, this syntax error happens because you're not properly splitting the gsutil command into individual arguments when using subprocess. If you pass the entire command as a single string (like 'gsutil cp gs://my-bucket/file.txt ./local.txt'), the shell might misparse spaces or special characters in paths, triggering the error.
正确使用subprocess调用gsutil的脚本
Here's a fixed version that splits the command into a list of arguments—this is the safe way to call command-line tools from Python:
import subprocess def download_gcs_file(bucket_name, gcs_file_path, local_save_path): # Split the gsutil command into separate arguments to avoid syntax issues gsutil_command = [ 'gsutil', 'cp', f'gs://{bucket_name}/{gcs_file_path}', local_save_path ] try: # Execute the command, capture output, and check for errors result = subprocess.run(gsutil_command, check=True, capture_output=True, text=True) print(f"Download succeeded! Output: {result.stdout}") except subprocess.CalledProcessError as e: print(f"Download failed. Error message: {e.stderr}") raise # Example usage if __name__ == "__main__": YOUR_BUCKET = "your-gcs-bucket-name" GCS_FILE = "data/reports/2024_q1.pdf" LOCAL_PATH = "./downloads/q1_report.pdf" download_gcs_file(YOUR_BUCKET, GCS_FILE, LOCAL_PATH)
更推荐的方案:使用官方Google Cloud Storage Python SDK
Calling gsutil via subprocess is a quick fix, but for production Python code, the official google-cloud-storage library is far more reliable—it's Pythonic, avoids shell syntax issues, and gives you better control over errors and operations.
First, install the library:
pip install google-cloud-storage
Then use this script:
from google.cloud import storage def download_with_sdk(bucket_name, gcs_file_path, local_save_path): # Initialize the storage client storage_client = storage.Client() # Get the target bucket bucket = storage_client.bucket(bucket_name) # Get the file blob from GCS blob = bucket.blob(gcs_file_path) # Download the blob to your local filesystem blob.download_to_filename(local_save_path) print(f"File successfully downloaded to: {local_save_path}") # Example usage if __name__ == "__main__": YOUR_BUCKET = "your-gcs-bucket-name" GCS_FILE = "data/reports/2024_q1.pdf" LOCAL_PATH = "./downloads/q1_report.pdf" download_with_sdk(YOUR_BUCKET, GCS_FILE, LOCAL_PATH)
Note: Make sure your Python environment has the correct GCP credentials set up (either via gcloud auth application-default login or service account keys) for both methods to work.
内容的提问来源于stack exchange,提问作者Senthil Sekar

