Google Cloud API客户端库
项目描述
Python风格的Google Cloud Platform服务客户端。
google-cloud
警告: 此软件包已移动。它在PyPI上重命名为google-cloud。将不会为gcloud发布新版本。
概述
此客户端支持以下Google Cloud Platform服务
Google Cloud 数据存储
Google Cloud 存储
Google Cloud 发布/订阅
Google 大数据查询
Google Cloud 资源管理器
Google Stackdriver日志
如果您需要其他Google API的支持,请查看Google APIs Python客户端库。
快速入门
$ pip install --upgrade gcloud
示例应用程序
getting-started-python - 一个示例和教程,演示如何使用Cloud Datastore、Cloud Storage和Cloud Pub/Sub构建完整的Web应用程序,并将其部署到Google App Engine或Google Compute Engine。
gcloud-python-expenses-demo - 使用Cloud Datastore和Cloud Storage的样本费用演示
身份验证
使用gcloud-python,我们试图使身份验证尽可能简单。查看我们的文档中的身份验证部分了解更多信息。您也可能发现所有gcloud-*库共享的身份验证文档很有帮助。
Google Cloud Datastore
Google Cloud Datastore (Datastore API文档) 是一个完全管理的、无模式的数据库,用于存储非关系型数据。Cloud Datastore会自动根据用户进行扩展,并支持ACID事务、读写的高可用性、读和祖先查询的强一致性以及所有其他查询的最终一致性。
查看gcloud-python API datastore文档,了解如何使用此客户端库与Cloud Datastore交互。
查看官方Google Cloud Datastore文档,了解更多关于如何为您的项目激活Cloud Datastore的详细信息。
from gcloud import datastore
# Create, populate and persist an entity
entity = datastore.Entity(key=datastore.Key('EntityKind'))
entity.update({
'foo': u'bar',
'baz': 1337,
'qux': False,
})
# Then query for entities
query = datastore.Query(kind='EntityKind')
for result in query.fetch():
print result
Google Cloud Storage
Google Cloud Storage (Storage API文档) 允许您在具有非常高的可靠性、性能和可用性的Google基础设施上存储数据,并且可以用于通过直接下载将大型数据对象分发给用户。
查看gcloud-python API storage文档,了解如何使用此客户端库连接到Cloud Storage。
您需要创建一个Google Cloud Storage存储桶才能使用此客户端库。按照官方Google Cloud Storage文档的说明创建存储桶。
from gcloud import storage
client = storage.Client()
bucket = client.get_bucket('bucket-id-here')
# Then do other things...
blob = bucket.get_blob('remote/path/to/file.txt')
print blob.download_as_string()
blob.upload_from_string('New contents!')
blob2 = bucket.blob('remote/path/storage.txt')
blob2.upload_from_filename(filename='/local/path.txt')
Google Cloud Pub/Sub
Google Cloud Pub/Sub (Pub/Sub API文档) 是一种设计用于在应用程序之间提供可靠、多对多、异步消息传递的服务。发布者应用程序可以向主题发送消息,其他应用程序可以订阅该主题以接收消息。通过解耦发送者和接收者,Google Cloud Pub/Sub允许开发者在不同编写的应用程序之间进行通信。
查看gcloud-python API Pub/Sub文档,了解如何使用此客户端库连接到Cloud Pub/Sub。
要开始使用此API,您需要创建
from gcloud import pubsub
client = pubsub.Client()
topic = client.topic('topic_name')
topic.create()
topic.publish('this is the message_payload',
attr1='value1', attr2='value2')
Google BigQuery
没有适当的硬件和基础设施,查询大量数据集可能会耗费时间和金钱。Google BigQuery (BigQuery API文档) 通过利用Google基础设施的处理能力,允许针对追加表执行超快、类似SQL的查询,从而解决这个问题。
此包仍在实施中,但几乎完成了!
从CSV加载数据
import csv
from gcloud import bigquery
from gcloud.bigquery import SchemaField
client = bigquery.Client()
dataset = client.dataset('dataset_name')
dataset.create() # API request
SCHEMA = [
SchemaField('full_name', 'STRING', mode='required'),
SchemaField('age', 'INTEGER', mode='required'),
]
table = dataset.table('table_name', SCHEMA)
table.create()
with open('csv_file', 'rb') as readable:
table.upload_from_file(
readable, source_format='CSV', skip_leading_rows=1)
执行同步查询
# Perform a synchronous query.
QUERY = (
'SELECT name FROM [bigquery-public-data:usa_names.usa_1910_2013] '
'WHERE state = "TX"')
query = client.run_sync_query('%s LIMIT 100' % QUERY)
query.timeout_ms = TIMEOUT_MS
query.run()
for row in query.rows:
print row
查看gcloud-python API BigQuery文档,了解如何使用此客户端库连接到BigQuery。
Google Cloud Resource Manager
云资源管理器API(资源管理器API)提供了您可以使用的方法来以编程方式管理Google Cloud Platform中的项目。(资源管理器API文档)
请参阅 gcloud-python API的资源管理器文档,了解如何使用此客户端库管理项目。
Google Stackdriver 日志
Stackdriver 日志API(日志API文档)允许您存储、搜索、分析、监控和警报来自Google Cloud Platform的日志数据和事件。
from gcloud import logging
client = logging.Client()
logger = client.logger('log_name')
logger.log_text("A simple entry") # API call
获取条目的示例
entries, token = logger.list_entries()
for entry in entries:
print entry.payload
请参阅 gcloud-python API的日志文档,了解如何使用此客户端库连接到Stackdriver 日志。
贡献
欢迎并向此库做出贡献,并强烈鼓励。
有关如何开始的更多信息,请参阅贡献文档。
许可证
Apache 2.0 - 请参阅许可协议以获取更多信息。
项目详情
gcloud-0.18.3.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0af2dec59fce20561752f86e42d981c6a255e306a6c5e5d1fa3d358a8857e4fb |
|
MD5 | 66bda7eac1489782ad87284ac330d98a |
|
BLAKE2b-256 | 11abd0cee58db2d8445c26e6f5db25d9b1f1aa14a3ab30eea8ce77ae808d10ef |