Python Client for Google Cloud Storage
Project description
This is a shared codebase for gcloud-aio-storage and gcloud-rest-storage
Installation
$ pip install --upgrade gcloud-{aio,rest}-storage
Usage
To upload a file, you might do something like the following:
import aiohttp
from gcloud.aio.storage import Storage
async with aiohttp.ClientSession() as session:
client = Storage(session=session)
with open('/path/to/my/file', mode='r') as f:
status = await client.upload('my-bucket-name',
'path/to/gcs/folder',
f.read())
print(status)
Note that there are multiple ways to accomplish the above, ie,. by making use of the Bucket and Blob convenience classes if that better fits your use-case.
Of course, the major benefit of using an async library is being able to parallelize operations like this. Since gcloud-aio-storage is fully asyncio-compatible, you can use any of the builtin asyncio method to perform more complicated operations:
my_files = {
'/local/path/to/file.1': 'path/in/gcs.1',
'/local/path/to/file.2': 'path/in/gcs.2',
'/local/path/to/file.3': 'different/gcs/path/filename.3',
}
async with Storage() as client:
# Prepare all our upload data
uploads = []
for local_name, gcs_name in my_files.items():
with open(local_name, mode='r') as f:
uploads.append((gcs_name, f.read()))
# Simultaneously upload all files
await asyncio.gather(*[client.upload('my-bucket-name', path, file_)
for path, file_ in uploads])
You can also refer smoke test for more info and examples.
Note that you can also let gcloud-aio-storage do its own session management, so long as you give us a hint when to close that session:
async with Storage() as client:
# closes the client.session on leaving the context manager
# OR
client = Storage()
# do stuff
await client.close() # close the session explicitly
File Encodings
In some cases, aiohttp needs to transform the objects returned from GCS into strings, eg. for debug logging and other such issues. The built-in await response.text() operation relies on chardet for guessing the character encoding in any cases where it can not be determined based on the file metadata.
Unfortunately, this operation can be extremely slow, especially in cases where you might be working with particularly large files. If you notice odd latency issues when reading your results, you may want to set your character encoding more explicitly within GCS, eg. by ensuring you set the contentType of the relevant objects to something suffixed with ; charset=utf-8. For example, in the case of contentType='application/x-netcdf' files exhibiting latency, you could instead set contentType='application/x-netcdf; charset=utf-8. See #172 for more info!
Emulators
For testing purposes, you may want to use gcloud-aio-storage along with a local GCS emulator. Setting the $STORAGE_EMULATOR_HOST environment variable to the address of your emulator should be enough to do the trick.
For example, using fsouza/fake-gcs-server, you can do:
docker run -d -p 4443:4443 -v $PWD/my-sample-data:/data fsouza/fake-gcs-server
export STORAGE_EMULATOR_HOST='0.0.0.0:4443'
Any gcloud-aio-storage requests made with that environment variable set will query fake-gcs-server instead of the official GCS API.
Note that some emulation systems require disabling SSL – if you’re using a custom http session, you may need to disable SSL verification.
Contributing
Please see our contributing guide.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gcloud-aio-storage-5.5.2.tar.gz
.
File metadata
- Download URL: gcloud-aio-storage-5.5.2.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ec2ebc64e339b13a77b2ee325a0b8d29072985bd6495d7610ac88c12014cf01 |
|
MD5 | 0e269cf84716b2bda508406d47cc4c9d |
|
BLAKE2b-256 | eb38433f6e2b58c06e19245e3ee4e60779c832a01a5623ea3f8756acbf4b2948 |
File details
Details for the file gcloud_aio_storage-5.5.2-py2.py3-none-any.whl
.
File metadata
- Download URL: gcloud_aio_storage-5.5.2-py2.py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1ceb51ecc5e91e645f1cc2cdcc78f504497b9a10e2782bf58c9e5178a7a9754 |
|
MD5 | 293368a108a32a7e58ea6758096c8992 |
|
BLAKE2b-256 | ea662d7824439c807cc11369d1633ac96d55e6ec458aa2a228cf01fffa076d66 |