Google云端存储 – 如何从Python 3上传文件?
作者:互联网
如何从Python 3将文件上传到Google Cloud Storage?最终Python 2,如果它不可能从Python 3.
我看了看,但还没有找到真正有效的解决方案.我尝试了boto,但是当我尝试通过gsutil config -e生成必要的.boto文件时,它一直说我需要通过gcloud auth login配置身份验证.但是,我已经多次完成了后者,没有它帮助.
解决方法:
使用标准的gcloud库,它支持Python 2和Python 3.
将文件上载到云存储的示例
from gcloud import storage
from oauth2client.service_account import ServiceAccountCredentials
import os
credentials_dict = {
'type': 'service_account',
'client_id': os.environ['BACKUP_CLIENT_ID'],
'client_email': os.environ['BACKUP_CLIENT_EMAIL'],
'private_key_id': os.environ['BACKUP_PRIVATE_KEY_ID'],
'private_key': os.environ['BACKUP_PRIVATE_KEY'],
}
credentials = ServiceAccountCredentials.from_json_keyfile_dict(
credentials_dict
)
client = storage.Client(credentials=credentials, project='myproject')
bucket = client.get_bucket('mybucket')
blob = bucket.blob('myfile')
blob.upload_from_filename('myfile')
标签:python,python-3-x,google-cloud-storage,google-cloud-platform,boto 来源: https://codeday.me/bug/20190930/1835283.html