GCS File or Directory Loader
This loader parses any file stored on Google Cloud Storage (GCS), or the entire Bucket (with an optional prefix filter) if no particular file is specified. It now supports more advanced operations through the implementation of ResourcesReaderMixin and FileSystemReaderMixin.
Features
- Parse single files or entire buckets from GCS
- List resources in GCS buckets
- Retrieve detailed information about GCS objects
- Load specific resources from GCS
- Read file content directly
- Supports various authentication methods
- Comprehensive logging for easier debugging
- Robust error handling for improved reliability
Authentication
When initializing GCSReader, you may pass in your GCP Service Account Key in several ways:
- As a file path (
service_account_key_path) - As a JSON string (
service_account_key_json) - As a dictionary (
service_account_key)
If no credentials are provided, the loader will attempt to use default credentials.
Usage
To use this loader, you need to pass in the name of your GCS Bucket. You can then either parse a single file by passing its key, or parse multiple files using a prefix.
from llama_index.readers.gcs import GCSReader
import logging
# Set up logging (optional, but recommended)
logging.basicConfig(level=logging.INFO)
# Initialize the reader
reader = GCSReader(
bucket="scrabble-dictionary",
key="dictionary.txt", # Optional: specify a single file
# prefix="subdirectory/", # Optional: specify a prefix to filter files
service_account_key_json="[SERVICE_ACCOUNT_KEY_JSON]",
)
# Load data
documents = reader.load_data()
# List resources in the bucket
resources = reader.list_resources()
# Get information about a specific resource
resource_info = reader.get_resource_info("dictionary.txt")
# Load a specific resource
specific_doc = reader.load_resource("dictionary.txt")
# Read file content directly
file_content = reader.read_file_content("dictionary.txt")
print(f"Loaded {len(documents)} documents")
print(f"Found {len(resources)} resources")
print(f"Resource info: {resource_info}")
print(f"Specific document: {specific_doc}")
print(f"File content length: {len(file_content)} bytes")
Note: If the file is nested in a subdirectory, the key should contain that, e.g., subdirectory/input.txt.
Advanced Usage
All files are parsed with SimpleDirectoryReader. You may specify a custom file_extractor, relying on any of the loaders in the LlamaIndex library (or your own)!
from llama_index.readers.gcs import GCSReader
from llama_index.readers.mongodb import SimpleMongoReader
reader = GCSReader(
bucket="my-bucket",
file_extractor={
".mongo": SimpleMongoReader(),
# Add more custom extractors as needed
},
)
Error Handling
The GCSReader now includes comprehensive error handling. You can catch exceptions to handle specific error cases:
from google.auth.exceptions import DefaultCredentialsError
try:
reader = GCSReader(bucket="your-bucket-name")
documents = reader.load_data()
except DefaultCredentialsError:
print("Authentication failed. Please check your credentials.")
except Exception as e:
print(f"An error occurred: {str(e)}")
Logging
To get insights into the GCSReader's operations, configure logging in your application:
import logging
logging.basicConfig(level=logging.INFO)
This loader is designed to be used as a way to load data into LlamaIndex. For more advanced usage, including custom file extractors, metadata extraction, and working with specific file types, please refer to the LlamaIndex documentation.
Metadata
Release files for llama-index-readers-gcs 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llama_index_readers_gcs-0.7.0.tar.gz | 6.9 kB | Without provenance | Details for llama_index_readers_gcs-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llama_index_readers_gcs-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for llama_index_readers_gcs-0.7.0-py3-none-any.whl |
Total release size: 13.4 kB
Release files / llama_index_readers_gcs-0.7.0.tar.gz
| Download URL | llama_index_readers_gcs-0.7.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
58d63c407b7d58f98c4611a2b4d7a9a419ab60a0c825606c7f16a53e098aea97
|
|
BLAKE2b-256 checksum How to use checksums |
420f9a1ae3bdf59b9b68bae64d1178361fb265115c86e78df50e763256beec44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / llama_index_readers_gcs-0.7.0-py3-none-any.whl
| Download URL | llama_index_readers_gcs-0.7.0-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3eafe302f8659059bc5b6bc99daad4a2320553392b277650d237df6bfb4d56ee
|
|
BLAKE2b-256 checksum How to use checksums |
29ff528dd0260bdf72dd89cf047802a8e97b8f5485a55459c277a2ec4533fbde
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for llama-index-readers-gcs 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.