Skip to content

Google Cloud Storage file management abstraction

The Google Cloud Storage file management abstraction refers to the FileManageService interface and its implementation, GcsFileManageServiceImpl. This layer provides a simplified API for the application to interact with [[Google Cloud Storage]] (GCS), handling operations such as file creation, retrieval, directory listing, and deletion without directly exposing the underlying storage client details^[001-todo-code.md].

Core Operations

The abstraction exposes several key methods for managing blobs and directories within the GCS bucket^[001-todo-code.md]:

  • createBlob: Uploads file content. It supports uploading MultipartFile objects or raw byte[] arrays to a specific path defined by the bucket name and file path^[001-todo-code.md].
  • getDocument: Retrieves the content of a specific file as a byte[] array using its full path^[001-todo-code.md].
  • getDirectoryOfFileNames: Lists all files within a specific directory prefix. It iterates through the storage blob list and returns a sorted list of file names^[001-todo-code.md].
  • deleteDirectory: Removes all files within a specified directory. It lists the blobs matching the directory prefix and deletes them in bulk^[001-todo-code.md].

Path Generation

The service is responsible for constructing logical storage paths for different types of content^[001-todo-code.md].

For reports, it uses a prefix structure defined by ReportDocumentType (e.g., /doc/report/csv/ or /doc/report/pdf/)^[001-todo-code.md]. It determines the full path by combining a global prefix folder (from GcpUtil), the document type path, and a unique identifier (e.g., ID-temp/ for temporary files)^[001-todo-code.md].

For media uploads like images and videos, the path generation logic includes a hash component to prevent collision. It generates a folder path using an MD5 hash of the bucket name and platform code, and a filename derived from an MD5 hash of the file content^[001-todo-code.md].

Implementation Details

The implementation relies on the Google Cloud Java Client's Storage interface^[001-todo-code.md].

  • Blob Creation: Uses BlobId and BlobInfo to initiate the upload of byte content^[001-todo-code.md].
  • Blob Reading: Uses storage.readAllBytes(blobId) to fetch file content^[001-todo-code.md].
  • Listing: Uses storage.list with BlobListOption.prefix to simulate directory operations, as GCS is a flat object store^[001-todo-code.md].
  • Deletion: Accumulates BlobId objects from the list operation and performs a batch delete^[001-todo-code.md].

Sources

^[001-todo-code.md]