8000 Merge pull request #23 from jonparrott/master · theacodes/python-docs-samples@3886025 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3886025

Browse files
author
Jeff Mendoza
committed
Merge pull request GoogleCloudPlatform#23 from jonparrott/master
Merging in storage samples.
2 parents 4e4a1d8 + c985ed0 commit 3886025

File tree

7 files changed

+293
-0
lines changed

7 files changed

+293
-0
lines changed

storage/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Python Samples for Google Cloud Storage
2+
3+
Two samples:
4+
5+
1. ``list_objects.py`` lists objects in a bucket.
6+
2. ``compose_objects.py`` composes objects together to create another.
7+
8+
See the docstring for each sample for usage, or run the sample for the help text.
9+
10+
### Setup
11+
12+
Before running the samples, you'll need the Google Cloud SDK in order to setup authentication.
13+
14+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app).
15+
2. Setup the gcloud tool.
16+
17+
```
18+
gcloud components update app
19+
gcloud auth login
20+
gcloud config set project <your-app-id>
21+
```
22+
23+
You will also need to install the dependencies using [pip](https://pypi.python.org/pypi/pip):
24+
25+
```
26+
pip install -r requirements.txt
27+
```

storage/__init__.py

Whitespace-only changes.

storage/compose_objects.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 sample application for composing objects using the Cloud
18+
Storage API.
19+
20+
Before running, authenticate with the Google Cloud SDK by running:
21+
$ gcloud auth login
22+
23+
Create a least two sample files:
24+
$ echo "File 1" > file1.txt
25+
$ echo "File 2" > file2.txt
26+
27+
Example invocation:
28+
$ python compose_objects.py my-bucket destination.txt file1.txt file2.txt
29+
30+
Usage:
31+
$ python compose_objects.py <your-bucket> <destination-file-name> \
32+
<source-1> [... <source-n>]
33+
34+
You can also get help on all the command-line flags the program understands
35+
by running:
36+
$ python compose-sample.py --help
37+
38+
"""
39+
40+
import argparse
41+
import sys
42+
import json
43+
44+
from apiclient import discovery
45+
from oauth2client.client import GoogleCredentials
46+
47+
# Parser for command-line arguments.
48+
parser = argparse.ArgumentParser(
49+
description=__doc__,
50+
formatter_class=argparse.RawDescriptionHelpFormatter)
51+
parser.add_argument('bucket')
52+
parser.add_argument('destination', help='Destination file name')
53+
parser.add_argument('sources', nargs='+', help='Source files to compose')
54+
55+
56+
def main(argv):
57+
# Parse the command-line flags.
58+
args = parser.parse_args(argv[1:])
59+
60+
# Get the application default credentials. When running locally, these are
61+
# available after running `gcloud auth login`. When running on compute
62+
# engine, these are available from the environment.
63+
credentials = GoogleCredentials.get_application_default()
64+
65+
# Construct the service object for the interacting with the Cloud Storage
66+
# API.
67+
service = discovery.build('storage', 'v1', credentials=credentials)
68+
69+
# Upload the source files.
70+
for filename in args.sources:
71+
req = service.objects().insert(
72+
media_body=filename,
73+
name=filename,
74+
bucket=args.bucket)
75+
resp = req.execute()
76+
print '> Uploaded source file %s' % filename
77+
print json.dumps(resp, indent=2)
78+
79+
# Construct a request to compose the source files into the destination.
80+
compose_req_body = {
81+
'sourceObjects': [{'name': filename} for filename in args.sources],
82+
'destination': {
83+
'contentType': 'text/plain', # required
84+
}
85+
}
86+
req = service.objects().compose(
87+
destinationBucket=args.bucket,
88+
destinationObject=args.destination,
89+
body=compose_req_body)
90+
resp = req.execute()
91+
print '> Composed files into %s' % args.destination
92+
print json.dumps(resp, indent=2)
93+
94+
# Download and print the composed object.
95+
req = service.objects().get_media(
96+
bucket=args.bucket,
97+
object=args.destination)
98+
99+
res = req.execute()
100+
print '> Composed file contents:'
101+
print res
102+
103+
104+
if __name__ == '__main__':
105+
main(sys.argv)
106+
# [END all]

storage/list_objects.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 sample application for listing all objects
18+
in a bucket using the Cloud Storage API.
19+
20+
Before running, authenticate with the Google Cloud SDK by running:
21+
$ gcloud auth login
22+
23+
Usage:
24+
$ python list_objects.py <your-bucket>
25+
26+
You can also get help on all the command-line flags the program understands
27+
by running:
28+
$ python list_objects.py --help
29+
30+
"""
31+
32+
import argparse
33+
import sys
34+
import json
35+
36+
from apiclient import discovery
37+
from oauth2client.client import GoogleCredentials
38+
39+
40+
# Parser for command-line arguments.
41+
parser = argparse.ArgumentParser(
42+
description=__doc__,
43+
formatter_class=argparse.RawDescriptionHelpFormatter)
44+
parser.add_argument('bucket')
45+
46+
47+
def main(argv):
48+
# Parse the command-line flags.
49+
args = parser.parse_args(argv[1:])
50+
51+
# Get the application default credentials. When running locally, these are
52+
# available after running `gcloud auth login`. When running on compute
53+
# engine, these are available from the environment.
54+
credentials = GoogleCredentials.get_application_default()
55+
56+
# Construct the service object for interacting with the Cloud Storage API.
57+
service = discovery.build('storage', 'v1', credentials=credentials)
58+
59+
# Make a request to buckets.get to retrieve information about the bucket.
60+
req = service.buckets().get(bucket=args.bucket)
61+
resp = req.execute()
62+
print json.dumps(resp, indent=2)
63+
64+
# Create a request to objects.list to retrieve a list of objects.
65+
fields_to_return = \
66+
'nextPageToken,items(name,size,contentType,metadata(my-key))'
67+
req = service.objects().list(bucket=args.bucket, fields=fields_to_return)
68+
69+
# If you have too many items to list in one request, list_next() will
70+
# automatically handle paging with the pageToken.
71+
while req is not None:
72+
resp = req.execute()
73+
print json.dumps(resp, indent=2)
74+
req = service.objects().list_next(req, resp)
75+
76+
if __name__ == '__main__':
77+
main(sys.argv)
78+
# [END all]

storage/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-api-python-client>=1.4.0

storage/tests/test_list_objects.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
from storage.list_objects import main
15+
from tests import CloudBaseTest
16+
17+
18+
class TestListObjects(CloudBaseTest):
19+
def test_main(self):
20+
args = [
21+
'ignored_command_name',
22+
self.constants['bucketName']
23+
]
24+
self.assertNotRaises(main(args))

tests/__init__.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
"""
15+
Common testing utilities between samples
16+
"""
17+
18+
import json
19+
import os
20+
import unittest
21+
22+
23+
BUCKET_NAME_ENV = 'TEST_BUCKET_NAME'
24+
PROJECT_ID_ENV = 'TEST_PROJECT_ID'
25+
RESOURCE_PATH = os.path.join(os.getcwd(), 'resources')
26+
27+
28+
class CloudBaseTest(unittest.TestCase):
29+
30+
def setUp(self):
31+
# A hack to prevent get_application_default from going GAE route.
32+
self._server_software_org = os.environ.get('SERVER_SOFTWARE')
33+
os.environ['SERVER_SOFTWARE'] = ''
34+
35+
# Constants from environment
36+
test_bucket_name = os.environ.get(BUCKET_NAME_ENV, '')
37+
test_project_id = os.environ.get(PROJECT_ID_ENV, '')
38+
if not test_project_id or not test_bucket_name:
39+
raise Exception('You need to define an env var "%s" and "%s" to '
40+
'run the test.'
41+
% (PROJECT_ID_ENV, BUCKET_NAME_ENV))
42+
43+
# Constants from resources/constants.json
44+
with open(
45+
os.path.join(RESOURCE_PATH, 'constants.json'),
46+
'r') as constants_file:
47+
48+
self.constants = json.load(constants_file)
49+
self.constants['projectId'] = test_project_id
50+
self.constants['bucketName'] = test_bucket_name
51+
self.constants['cloudStorageInputURI'] = (
52+
self.constants['cloudStorageInputURI'] % test_bucket_name)
53+
self.constants['cloudStorageOutputURI'] = (
54+
self.constants['cloudStorageOutputURI'] % test_bucket_name)
55+
56+
def tearDown(self):
57+
os.environ['SERVER_SOFTWARE'] = self._server_software_org

0 commit comments

Comments
 (0)
0