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