Skip to content

Commit 09d6a5d

Browse files
committed
Create storage-sample.py
1 parent 632a962 commit 09d6a5d

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

python-example/storage-sample.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2013 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# [START all]
17+
"""Command-line skeleton application for Cloud Storage API.
18+
Usage:
19+
$ python storage-sample.py
20+
21+
You can also get help on all the command-line flags the program understands
22+
by running:
23+
24+
$ python storage-sample.py --help
25+
26+
"""
27+
28+
import argparse
29+
import httplib2
30+
import os
31+
import sys
32+
import json
33+
34+
from apiclient import discovery
35+
from oauth2client import file
36+
from oauth2client import client
37+
from oauth2client import tools
38+
39+
# Define sample variables.
40+
_BUCKET_NAME = '[[INSERT_YOUR_BUCKET_NAME_HERE]]'
41+
_API_VERSION = 'v1beta2'
42+
43+
# Parser for command-line arguments.
44+
parser = argparse.ArgumentParser(
45+
description=__doc__,
46+
formatter_class=argparse.RawDescriptionHelpFormatter,
47+
parents=[tools.argparser])
48+
49+
50+
# CLIENT_SECRETS is name of a file containing the OAuth 2.0 information for this
51+
# application, including client_id and client_secret. You can see the Client ID
52+
# and Client secret on the APIs page in the Cloud Console:
53+
# <https://cloud.google.com/console#/project/673076789876/apiui>
54+
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
55+
56+
# Set up a Flow object to be used for authentication.
57+
# Add one or more of the following scopes. PLEASE ONLY ADD THE SCOPES YOU
58+
# NEED. For more information on using scopes please see
59+
# <https://developers.google.com/+/best-practices>.
60+
FLOW = client.flow_from_clientsecrets(CLIENT_SECRETS,
61+
scope=[
62+
'https://www.googleapis.com/auth/devstorage.full_control',
63+
'https://www.googleapis.com/auth/devstorage.read_only',
64+
'https://www.googleapis.com/auth/devstorage.read_write',
65+
],
66+
message=tools.message_if_missing(CLIENT_SECRETS))
67+
68+
69+
def main(argv):
70+
# Parse the command-line flags.
71+
flags = parser.parse_args(argv[1:])
72+
73+
# If the credentials don't exist or are invalid run through the native client
74+
# flow. The Storage object will ensure that if successful the good
75+
# credentials will get written back to the file.
76+
storage = file.Storage('sample.dat')
77+
credentials = storage.get()
78+
if credentials is None or credentials.invalid:
79+
credentials = tools.run_flow(FLOW, storage, flags)
80+
81+
# Create an httplib2.Http object to handle our HTTP requests and authorize it
82+
# with our good Credentials.
83+
http = httplib2.Http()
84+
http = credentials.authorize(http)
85+
86+
# Construct the service object for the interacting with the Cloud Storage API.
87+
service = discovery.build('storage', _API_VERSION, http=http)
88+
89+
try:
90+
req = service.buckets().get(bucket=_BUCKET_NAME)
91+
resp = req.execute()
92+
print json.dumps(resp, indent=2)
93+
94+
fields_to_return = 'nextPageToken,items(bucket,name,metadata(my-key))'
95+
req = service.objects().list(bucket=_BUCKET_NAME)
96+
# If you have too many items to list in one request, list_next() will
97+
# automatically handle paging with the pageToken.
98+
while req is not None:
99+
resp = req.execute()
100+
print json.dumps(resp, indent=2)
101+
req = service.objects().list_next(req, resp)
102+
103+
except client.AccessTokenRefreshError:
104+
print ("The credentials have been revoked or expired, please re-run"
105+
"the application to re-authorize")
106+
107+
if __name__ == '__main__':
108+
main(sys.argv)
109+
# [END all]

0 commit comments

Comments
 (0)