Skip to content

Commit 68a3d10

Browse files
askmeegsengelke
andauthored
[Cleanup] storage/s3-sdk: Adds docstrings, return value instead of print (#9948)
* initial commit, need to test * typo fix * lint * fix list buckets * remove capsys * fix: docstring style and debug prints --------- Co-authored-by: Charlie Engelke <[email protected]>
1 parent 161448f commit 68a3d10

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

storage/s3-sdk/list_gcs_buckets.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121

2222

2323
def list_gcs_buckets(google_access_key_id, google_access_key_secret):
24-
"""Lists all GCS buckets 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.
24+
"""Lists all Cloud Storage buckets using AWS SDK for Python (boto3)
25+
Positional arguments:
26+
google_access_key_id: hash-based message authentication code (HMAC) access ID
27+
google_access_key_secret: HMAC access secret
28+
29+
Returned value is a list of strings, one for each bucket name.
30+
31+
To use this sample:
32+
1. Create a Cloud Storage HMAC key: https://cloud.google.com/storage/docs/authentication/managing-hmackeys#create
33+
2. Change endpoint_url to a Google Cloud Storage XML API endpoint.
34+
35+
To learn more about HMAC: https://cloud.google.com/storage/docs/authentication/hmackeys#overview
36+
"""
2937
client = boto3.client(
3038
"s3",
3139
region_name="auto",
@@ -37,12 +45,12 @@ def list_gcs_buckets(google_access_key_id, google_access_key_secret):
3745
# Call GCS to list current buckets
3846
response = client.list_buckets()
3947

40-
# Print bucket names
41-
print("Buckets:")
48+
# Return list of bucket names
49+
results = []
4250
for bucket in response["Buckets"]:
43-
print(bucket["Name"])
44-
45-
51+
results.append(bucket["Name"])
52+
print(bucket["Name"]) # Can remove if not needed after development
53+
return results
4654
# [END storage_s3_sdk_list_buckets]
4755

4856

storage/s3-sdk/list_gcs_objects.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@
2121

2222

2323
def list_gcs_objects(google_access_key_id, google_access_key_secret, 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.
24+
"""Lists all Cloud Storage objects using AWS SDK for Python (boto3)
25+
Positional arguments:
26+
google_access_key_id: hash-based message authentication code (HMAC) access ID
27+
google_access_key_secret: HMAC access secret
28+
bucket_name: name of Cloud Storage bucket
2929
30+
Returned value is a list of strings, one for each object in the bucket.
31+
32+
To use this sample:
33+
1. Create a Cloud Storage HMAC key: https://cloud.google.com/storage/docs/authentication/managing-hmackeys#create
34+
2. Change endpoint_url to a Google Cloud Storage XML API endpoint.
35+
36+
To learn more about HMAC: https://cloud.google.com/storage/docs/authentication/hmackeys#overview
37+
"""
3038
client = boto3.client(
3139
"s3",
3240
region_name="auto",
@@ -38,12 +46,12 @@ def list_gcs_objects(google_access_key_id, google_access_key_secret, bucket_name
3846
# Call GCS to list objects in bucket_name
3947
response = client.list_objects(Bucket=bucket_name)
4048

41-
# Print object names
42-
print("Objects:")
49+
# Return list of object names in bucket
50+
results = []
4351
for blob in response["Contents"]:
44-
print(blob["Key"])
45-
46-
52+
results.append(blob["Key"])
53+
print(blob["Key"]) # Can remove if not needed after development
54+
return results
4755
# [END storage_s3_sdk_list_objects]
4856

4957

storage/s3-sdk/noxfile_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def get_service_account_email():
3535
TEST_CONFIG_OVERRIDE = {
3636
# A dictionary you want to inject into your test. Don't put any
3737
# secrets here. These values will override predefined values.
38-
'envs': {
39-
'HMAC_KEY_TEST_SERVICE_ACCOUNT': get_service_account_email(),
38+
"envs": {
39+
"HMAC_KEY_TEST_SERVICE_ACCOUNT": get_service_account_email(),
4040
# Some tests can not use multiple projects because of several reasons:
4141
# 1. The new projects is enforced to have the
4242
# 'constraints/iam.disableServiceAccountKeyCreation' policy.
4343
# 2. The new projects buckets need to have universal permission model.
4444
# For those tests, we'll use the original project.
45-
'MAIN_GOOGLE_CLOUD_PROJECT': 'python-docs-samples-tests'
45+
"MAIN_GOOGLE_CLOUD_PROJECT": "python-docs-samples-tests",
4646
},
4747
}

storage/s3-sdk/s3_sdk_test.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,21 @@ def test_blob(test_bucket):
6868
# Retry request because the created key may not be fully propagated for up
6969
# to 15s.
7070
@backoff.on_exception(backoff.constant, ClientError, interval=1, max_time=15)
71-
def test_list_buckets(capsys, hmac_fixture, test_bucket):
72-
list_gcs_buckets.list_gcs_buckets(
71+
def test_list_buckets(hmac_fixture, test_bucket):
72+
results = list_gcs_buckets.list_gcs_buckets(
7373
google_access_key_id=hmac_fixture[0].access_id,
7474
google_access_key_secret=hmac_fixture[1],
7575
)
76-
out, _ = capsys.readouterr()
77-
assert "Buckets:" in out
78-
assert test_bucket.name in out
76+
assert test_bucket.name in results
7977

8078

8179
# Retry request because the created key may not be fully propagated for up
8280
# to 15s.
8381
@backoff.on_exception(backoff.constant, ClientError, interval=1, max_time=15)
84-
def test_list_blobs(capsys, hmac_fixture, test_bucket, test_blob):
85-
list_gcs_objects.list_gcs_objects(
82+
def test_list_blobs(hmac_fixture, test_bucket, test_blob):
83+
result = list_gcs_objects.list_gcs_objects(
8684
google_access_key_id=hmac_fixture[0].access_id,
8785
google_access_key_secret=hmac_fixture[1],
8886
bucket_name=test_bucket.name,
8987
)
90-
out, _ = capsys.readouterr()
91-
assert "Objects:" in out
92-
assert test_blob.name in out
88+
assert test_blob.name in result

0 commit comments

Comments
 (0)