Skip to content

Commit 2439d01

Browse files
author
Aaron Gabriel Neyer
authored
samples: Add GCS fileio samples (#645)
* Add GCS file-like io samples * now with pandas! * change pandas version * add to readme * requests from andrew * lint * canonical command line args * Update storage_fileio_pandas.py * Update storage_fileio_write_read.py
1 parent af9c9dc commit 2439d01

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

samples/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ for instructions on setting up credentials for applications.
8686
* [Enable Requester Pays](#enable-requester-pays)
8787
* [Enable Uniform Bucket Level Access](#enable-uniform-bucket-level-access)
8888
* [Enable Versioning](#enable-versioning)
89+
* [FileIO Write-Read] (#fileio-write-read)
90+
* [FileIO Pandas] (#fileio-pandas)
8991
* [Generate Encryption Key](#generate-encryption-key)
9092
* [Generate Signed Post Policy V4](#generate-signed-post-policy-v4)
9193
* [Generate Signed Url V2](#generate-signed-url-v2)
@@ -486,6 +488,22 @@ View the [source code](https://github.com/googleapis/python-storage/blob/main/sa
486488
487489
`python storage_enable_versioning.py`
488490
491+
-----
492+
### FileIO Write-Read
493+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-storage&page=editor&open_in_editor=samples/snippets/storage_fileio_write_read.py,samples/README.md)
494+
495+
View the [source code](https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_fileio_write_read.py). To run this sample:
496+
497+
498+
`python storage_fileio_write_read.py <BUCKET_NAME> <BLOB_NAME>`
499+
-----
500+
### FileIO Pandas
501+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-storage&page=editor&open_in_editor=samples/snippets/storage_fileio_pandas.py,samples/README.md)
502+
503+
View the [source code](https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_fileio_pandas.py). To run this sample:
504+
505+
506+
`python storage_fileio_pandas.py <BUCKET_NAME> <BLOB_NAME>`
489507
-----
490508
### Generate Encryption Key
491509
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-storage&page=editor&open_in_editor=samples/snippets/storage_generate_encryption_key.py,samples/README.md)

samples/snippets/fileio_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2021 Google LLC
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 uuid
16+
17+
import storage_fileio_pandas
18+
import storage_fileio_write_read
19+
20+
21+
def test_fileio_write_read(bucket, capsys):
22+
blob_name = "test-fileio-{}".format(uuid.uuid4())
23+
storage_fileio_write_read.write_read(bucket.name, blob_name)
24+
out, _ = capsys.readouterr()
25+
assert "Hello world" in out
26+
27+
28+
def test_fileio_pandas(bucket, capsys):
29+
blob_name = "test-fileio-{}".format(uuid.uuid4())
30+
storage_fileio_pandas.pandas_write(bucket.name, blob_name)
31+
out, _ = capsys.readouterr()
32+
assert f"Wrote csv with pandas with name {blob_name} from bucket {bucket.name}." in out
33+
storage_fileio_pandas.pandas_read(bucket.name, blob_name)
34+
out, _ = capsys.readouterr()
35+
assert f"Read csv with pandas with name {blob_name} from bucket {bucket.name}." in out

samples/snippets/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
google-cloud-pubsub==2.8.0
22
google-cloud-storage==1.42.3
3+
pandas==1.1.5
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google Inc. All Rights Reserved.
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 sys
18+
19+
"""Sample that creates and consumes a GCS blob using pandas with file-like IO
20+
"""
21+
22+
# [START storage_fileio_pandas_write]
23+
24+
25+
def pandas_write(bucket_name, blob_name):
26+
"""Use pandas to interact with GCS using file-like IO"""
27+
# The ID of your GCS bucket
28+
# bucket_name = "your-bucket-name"
29+
30+
# The ID of your new GCS object
31+
# blob_name = "storage-object-name"
32+
33+
from google.cloud import storage
34+
import pandas as pd
35+
36+
storage_client = storage.Client()
37+
bucket = storage_client.bucket(bucket_name)
38+
blob = bucket.blob(blob_name)
39+
40+
with blob.open("w") as f:
41+
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
42+
f.write(df.to_csv(index=False))
43+
44+
print(f"Wrote csv with pandas with name {blob_name} from bucket {bucket.name}.")
45+
46+
47+
# [END storage_fileio_pandas_write]
48+
49+
50+
# [START storage_fileio_pandas_read]
51+
52+
53+
def pandas_read(bucket_name, blob_name):
54+
"""Use pandas to interact with GCS using file-like IO"""
55+
# The ID of your GCS bucket
56+
# bucket_name = "your-bucket-name"
57+
58+
# The ID of your new GCS object
59+
# blob_name = "storage-object-name"
60+
61+
from google.cloud import storage
62+
import pandas as pd
63+
64+
storage_client = storage.Client()
65+
bucket = storage_client.bucket(bucket_name)
66+
blob = bucket.blob(blob_name)
67+
68+
with blob.open("r") as f:
69+
pd.read_csv(f)
70+
71+
print(f"Read csv with pandas with name {blob_name} from bucket {bucket.name}.")
72+
73+
74+
# [END storage_fileio_pandas_read]
75+
76+
77+
if __name__ == "__main__":
78+
pandas_write(
79+
bucket_name=sys.argv[1],
80+
blob_name=sys.argv[2]
81+
)
82+
83+
pandas_read(
84+
bucket_name=sys.argv[1],
85+
blob_name=sys.argv[2]
86+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google Inc. All Rights Reserved.
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 sys
18+
19+
"""Sample that writes and read a blob in GCS using file-like IO
20+
"""
21+
22+
# [START storage_fileio_write_read]
23+
from google.cloud import storage
24+
25+
26+
def write_read(bucket_name, blob_name):
27+
"""Write and read a blob from GCS using file-like IO"""
28+
# The ID of your GCS bucket
29+
# bucket_name = "your-bucket-name"
30+
31+
# The ID of your new GCS object
32+
# blob_name = "storage-object-name"
33+
34+
storage_client = storage.Client()
35+
bucket = storage_client.bucket(bucket_name)
36+
blob = bucket.blob(blob_name)
37+
38+
# Mode can be specified as wb/rb for bytes mode.
39+
# See: https://docs.python.org/3/library/io.html
40+
with blob.open("w") as f:
41+
f.write("Hello world")
42+
43+
with blob.open("r") as f:
44+
print(f.read())
45+
46+
47+
# [END storage_fileio_write_read]
48+
49+
if __name__ == "__main__":
50+
write_read(
51+
bucket_name=sys.argv[1],
52+
blob_name=sys.argv[2]
53+
)

0 commit comments

Comments
 (0)