如何 运行 Google gsutil 使用 Python
How to run Google gsutil using Python
安装和配置 Google Cloud SDK gsutil
命令后,只需使用 Windows cmd 输入其名称和参数(-s)即可 运行。
示例如下:
"C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin\gcloud" version
但是如果 运行 使用 Python 子进程,同样的命令会失败。
当子进程的 shell
参数设置为 True 时,ImportError
发生:
import subprocess
cmd = '"C:/Program Files (x86)/Google/Cloud SDK/google-cloud-sdk/bin/gsutil" version'
p = subprocess.Popen(cmd, shell=True)
.....
ImportError: No module named site
将子进程的 shell
参数设置为 False 然后 WindowsError: [Error 2] The system cannot find the file specified
发生:
p = subprocess.Popen(cmd, shell=False)
有没有办法在 Windows 上使用 Python 运行 gsutil
?
请注意,与 Google Cloud Storage 交互的正确且正式的方式是使用 Google Cloud Client Library for Python
而不是 运行 通过 subprocess.Popen
的 gsutil
命令。
如果您不只是设置一些测试,如果没有任何技术限制使这种方式不可行,我建议您从一开始就采用这种方式。
您可以在以下链接中查看相关 Overview and Documentation。
摘自文档的一个小例子如下:
from google.cloud import storage
client = storage.Client()
bucket = client.get_bucket('<your-bucket-name>')
blob = bucket.blob('my-test-file.txt')
blob.upload_from_string('this is test content!')
您可以找到另一个示例 here 使用 google-cloud-python 与数据存储和云存储来管理费用。
安装和配置 Google Cloud SDK gsutil
命令后,只需使用 Windows cmd 输入其名称和参数(-s)即可 运行。
示例如下:
"C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin\gcloud" version
但是如果 运行 使用 Python 子进程,同样的命令会失败。
当子进程的 shell
参数设置为 True 时,ImportError
发生:
import subprocess
cmd = '"C:/Program Files (x86)/Google/Cloud SDK/google-cloud-sdk/bin/gsutil" version'
p = subprocess.Popen(cmd, shell=True)
.....
ImportError: No module named site
将子进程的 shell
参数设置为 False 然后 WindowsError: [Error 2] The system cannot find the file specified
发生:
p = subprocess.Popen(cmd, shell=False)
有没有办法在 Windows 上使用 Python 运行 gsutil
?
请注意,与 Google Cloud Storage 交互的正确且正式的方式是使用 Google Cloud Client Library for Python
而不是 运行 通过 subprocess.Popen
的 gsutil
命令。
如果您不只是设置一些测试,如果没有任何技术限制使这种方式不可行,我建议您从一开始就采用这种方式。
您可以在以下链接中查看相关 Overview and Documentation。 摘自文档的一个小例子如下:
from google.cloud import storage
client = storage.Client()
bucket = client.get_bucket('<your-bucket-name>')
blob = bucket.blob('my-test-file.txt')
blob.upload_from_string('this is test content!')
您可以找到另一个示例 here 使用 google-cloud-python 与数据存储和云存储来管理费用。