10000 storage: Add Boto3 List Objects in GCS (#2041) · diophung/python-docs-samples@2d4ab38 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d4ab38

Browse files
authored
storage: Add Boto3 List Objects in GCS (GoogleCloudPlatform#2041)
* Add S3 SDK sample to list objects * Fix test and method names * Fix typo in helpful information * Fixing lint issues * Remove workaround * Remove debug code from generate_signed_urls.py * update bucket to blob
1 parent 8fba904 commit 2d4ab38

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

storage/s3-sdk/README.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ To run this sample:
7979
8080
8181
82+
List GCS Objects using S3 SDK
83+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
84+
85+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
86+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=storage/s3-sdk/list_gcs_objects.py,storage/s3-sdk/README.rst
87+
88+
89+
90+
91+
To run this sample:
92+
93+
.. code-block:: bash
94+
95+
$ python list_gcs_objects.py
96+
97+
usage: list_gcs_objects.py [-h]
98+
google_access_key_id google_access_key_secret
99+
bucket_name
100+
101+
positional arguments:
102+
google_access_key_id Your Cloud Storage HMAC Access Key ID.
103+
google_access_key_secret
104+
Your Cloud Storage HMAC Access Key Secret.
105+
bucket_name Your Cloud Storage bucket name
106+
107+
optional arguments:
108+
-h, --help show this help message and exit
109+
110+
111+
82112
83113
84114
.. _Google Cloud SDK: https://cloud.google.com/sdk/

storage/s3-sdk/README.rst.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ samples:
1717
- name: List GCS Buckets using S3 SDK
1818
file: list_gcs_buckets.py
1919
show_help: true
20+
- name: List GCS Objects using S3 SDK
21+
file: list_gcs_objects.py
22+
show_help: true
2023

2124
cloud_client_library: false
2225

storage/s3-sdk/list_gcs_objects.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 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+
17+
import argparse
18+
# [START storage_s3_sdk_list_objects]
19+
import boto3
20+
21+
22+
def list_gcs_objects(google_access_key_id, google_access_key_secret,
23+
bucket_name):
24+
"""Lists GCS objects using boto3 SDK"""
25+
# Create a new client and do the following:
26+
# 1. Change the endpoint URL to use the
27+
# Google Cloud Storage XML API endpoint.
28+
# 2. Use Cloud Storage HMAC Credentials.
29+
30+
client = boto3.client("s3", region_name="auto",
31+
endpoint_url="https://storage.googleapis.com",
32+
aws_access_key_id=google_access_key_id,
33+
aws_secret_access_key=google_access_key_secret)
34+
35+
# Call GCS to list objects in bucket_name
36+
response = client.list_objects(Bucket=bucket_name)
37+
38+
# Print object names
39+
print("Objects:")
40+
for blob in response["Contents"]:
41+
print(blob["Key"])
42+
# [END storage_s3_sdk_list_objects]
43+
44+
45+
if __name__ == "__main__":
46+
parser = argparse.ArgumentParser(
47+
description=__doc__,
48+
formatter_class=argparse.RawDescriptionHelpFormatter)
49+
parser.add_argument("google_access_key_id",
50+
help="Your Cloud Storage HMAC Access Key ID.")
51+
parser.add_argument("google_access_key_secret",
52+
help="Your Cloud Storage HMAC Access Key Secret.")
53+
parser.add_argument('bucket_name',
54+
help="Your Cloud Storage bucket name")
55+
56+
args = parser.parse_args()
57+
58+
list_gcs_objects(google_access_key_id=args.google_access_key_id,
59+
google_access_key_secret=args.google_access_key_secret,
60+
bucket_name=args.bucket_name)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019 Google, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import list_gcs_objects
18+
19+
BUCKET = os.environ["GOOGLE_CLOUD_PROJECT_S3_SDK"]
20+
KEY_ID = os.environ["STORAGE_HMAC_ACCESS_KEY_ID"]
21+
SECRET_KEY = os.environ["STORAGE_HMAC_ACCESS_SECRET_KEY"]
22+
23+
24+
def test_list_blobs(capsys):
25+
list_gcs_objects.list_gcs_objects(google_access_key_id=KEY_ID,
26+
google_access_key_secret=SECRET_KEY,
27+
bucket_name=BUCKET)
28+
out, _ = capsys.readouterr()
29+
assert "Objects:" in out

0 commit comments

Comments
 (0)
0