python - python 中的 gsutil cp 通过 os.system

标签 python google-cloud-platform cp gsutil os.system

我想将本地文件复制到谷歌云存储桶。我可以在命令行中使用 gsutil cp 来做到这一点。但我想在 python 中执行此操作,因此要在 python 中运行类似的命令,我使用以下行:

os.system('gsutil cp '+ '/MY-LOCAL-FILE-NAME' +' gs://My-Bucket-Name')

它将文件发送到存储桶,但它是一个空文件!!! 我也试过 python subprocess 但同样的问题。 我认为问题是字符串连接。当我使用整个字符串时它可以工作并将文件发送到存储桶但是当我在“subprocess”或“os.system”中连接子字符串时,它只发送一个空文件,这太奇怪了,对此有什么想法吗???

最佳答案

从 Gcloud github 你有 snippets你感兴趣的地方:

def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
     storage_client = storage.Client()
     bucket = storage_client.get_bucket(bucket_name)
     blob = bucket.blob(destination_blob_name)

     blob.upload_from_filename(source_file_name)

print('File {} uploaded to {}.'.format(
    source_file_name,
    destination_blob_name))

关于python - python 中的 gsutil cp 通过 os.system,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51509373/

相关文章:

bash - Makefile 错误 - 寻找匹配 `"时意外的 EOF '

python - 过滤掉包含非字母数字字符的行

python - 在 Mac 上,PYTHONPATH 应设置为什么才能使用 "python manage.py run_command"?

python - 使用 Python SDK 的 Vertex AI 模型版本

google-app-engine - 关于获取datastore的Cursor行为的问题

linux - cp:找不到命令

Android 在 OSX 10.8.5 上构建错误 : cp: the -R and -r options may not be specified together

python - 如何使用 openpyxl 将 excel 中的所有单元格格式化为单一样式?

python - 属性错误: type object 'Product' has no attribute '_meta'

sql-server - Google Composer - 如何在环境中安装 Microsoft SQL Server ODBC 驱动程序