Google Cloud Storage
Google Cloud Storage (GCS) 文档加载器允许您从存储桶加载文档。
Maven 依赖
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-document-loader-google-cloud-storage</artifactId>
<version>1.0.0-beta3</version>
</dependency>
API
GoogleCloudStorageDocumentLoader
身份验证
身份验证应该为您透明地处理:
- 如果您的应用程序在 Google Cloud Platform 上运行(Cloud Run、App Engine、Compute Engine 等)
- 当在本地机器上运行时,如果您已通过 Google 的
gcloud
SDK 进行身份验证
您只需创建一个指定项目 ID 的加载器:
GoogleCloudStorageDocumentLoader gcsLoader = GoogleCloudStorageDocumentLoader.builder()
.project(System.getenv("GCP_PROJECT_ID"))
.build();
否则,如果您已下载服务账号密钥并导出指向它的环境变量,可以指定 Credentials
:
GoogleCloudStorageDocumentLoader gcsLoader = GoogleCloudStorageDocumentLoader.builder()
.project(System.getenv("GCP_PROJECT_ID"))
.credentials(GoogleCredentials.fromStream(new FileInputStream(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"))))
.build();
了解更多关于凭证的信息。
访问公共存储桶时,您不需要进行身份验证。
示例
从 GCS 存储桶加载单个文件
GoogleCloudStorageDocumentLoader gcsLoader = GoogleCloudStorageDocumentLoader.builder()
.project(System.getenv("GCP_PROJECT_ID"))
.build();
Document document = gcsLoader.loadDocument("BUCKET_NAME", "FILE_NAME.txt", new TextDocumentParser());
从 GCS 存储桶加载所有文件
GoogleCloudStorageDocumentLoader gcsLoader = GoogleCloudStorageDocumentLoader.builder()
.project(System.getenv("GCP_PROJECT_ID"))
.build();
List<Document> documents = gcsLoader.loadDocuments("BUCKET_NAME", new TextDocumentParser());
使用通配符模式从 GCS 存储桶加载所有文件
GoogleCloudStorageDocumentLoader gcsLoader = GoogleCloudStorageDocumentLoader.builder()
.project(System.getenv("GCP_PROJECT_ID"))
.build();
List<Document> documents = gcsLoader.loadDocuments("BUCKET_NAME", "*.txt", new TextDocumentParser());
有关更多代码示例,请查看集成测试类: