Class GcsUtils

java.lang.Object
google.registry.gcs.GcsUtils
All Implemented Interfaces:
Serializable

public class GcsUtils extends Object implements Serializable
Utilities for working with Google Cloud Storage.

It is Serializable so that it can be used in Beam.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    GcsUtils(com.google.cloud.storage.StorageOptions storageOptions)
     
    GcsUtils(GoogleCredentialsBundle credentialsBundle)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    createFromBytes(com.google.cloud.storage.BlobId blobId, byte[] bytes)
    Creates a GCS file with the given byte contents, overwriting existing files.
    void
    createFromBytes(com.google.cloud.storage.BlobInfo blobInfo, byte[] bytes)
    Creates a GCS file with the given byte contents and metadata, overwriting existing files.
    void
    delete(com.google.cloud.storage.BlobId blobId)
    Delete the given GCS file.
    boolean
     
    boolean
    existsAndNotEmpty(com.google.cloud.storage.BlobId blobId)
    Returns true if a file exists and is non-empty on Google Cloud Storage.
    com.google.cloud.storage.BlobInfo
    getBlobInfo(com.google.cloud.storage.BlobId blobId)
    Returns the BlobInfo of the given GCS file.
    com.google.common.collect.ImmutableMap<String,String>
    getMetadata(com.google.cloud.storage.BlobId blobId)
    Returns the user defined metadata of a GCS file if the file exists, or an empty map.
    int
     
    com.google.common.collect.ImmutableList<String>
    listFolderObjects(String bucketName, String prefix)
    Returns a list of all object names within a bucket for a given prefix.
    openInputStream(com.google.cloud.storage.BlobId blobId)
    Opens a GCS file for reading as an InputStream.
    openOutputStream(com.google.cloud.storage.BlobId blobId)
    Opens a GCS file for writing as an OutputStream, overwriting existing files.
    openOutputStream(com.google.cloud.storage.BlobId blobId, com.google.common.collect.ImmutableMap<String,String> metadata)
    Opens a GCS file for writing as an OutputStream, overwriting existing files and setting the given metadata.
    byte[]
    readBytesFrom(com.google.cloud.storage.BlobId blobId)
    Read the content of the given GCS file and return it in a byte array.
    void
    updateContentType(com.google.cloud.storage.BlobId blobId, String contentType)
    Update file content type on existing GCS file

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • openInputStream

      @CheckReturnValue public InputStream openInputStream(com.google.cloud.storage.BlobId blobId)
      Opens a GCS file for reading as an InputStream.
    • openOutputStream

      @CheckReturnValue public OutputStream openOutputStream(com.google.cloud.storage.BlobId blobId)
      Opens a GCS file for writing as an OutputStream, overwriting existing files.
    • openOutputStream

      @CheckReturnValue public OutputStream openOutputStream(com.google.cloud.storage.BlobId blobId, com.google.common.collect.ImmutableMap<String,String> metadata)
      Opens a GCS file for writing as an OutputStream, overwriting existing files and setting the given metadata.
    • createFromBytes

      public void createFromBytes(com.google.cloud.storage.BlobId blobId, byte[] bytes) throws com.google.cloud.storage.StorageException
      Creates a GCS file with the given byte contents, overwriting existing files.
      Throws:
      com.google.cloud.storage.StorageException
    • createFromBytes

      public void createFromBytes(com.google.cloud.storage.BlobInfo blobInfo, byte[] bytes) throws com.google.cloud.storage.StorageException
      Creates a GCS file with the given byte contents and metadata, overwriting existing files.
      Throws:
      com.google.cloud.storage.StorageException
    • readBytesFrom

      public byte[] readBytesFrom(com.google.cloud.storage.BlobId blobId) throws com.google.cloud.storage.StorageException
      Read the content of the given GCS file and return it in a byte array.
      Throws:
      com.google.cloud.storage.StorageException
    • delete

      public void delete(com.google.cloud.storage.BlobId blobId) throws com.google.cloud.storage.StorageException
      Delete the given GCS file.
      Throws:
      com.google.cloud.storage.StorageException
    • updateContentType

      public void updateContentType(com.google.cloud.storage.BlobId blobId, String contentType) throws com.google.cloud.storage.StorageException
      Update file content type on existing GCS file
      Throws:
      com.google.cloud.storage.StorageException
    • listFolderObjects

      public com.google.common.collect.ImmutableList<String> listFolderObjects(String bucketName, String prefix) throws IOException
      Returns a list of all object names within a bucket for a given prefix.

      Note this also strips the provided prefix from the object, leaving only the object name (i.e. if the bucket hello contains gs://hello/world/myobj.txt and gs://hello/world/a/b.csv, then listFolderObjects("hello", "world/") would return {"myobj.txt", "a/b.csv"}.

      Parameters:
      bucketName - the name of the bucket, with no gs:// namespace or trailing slashes
      prefix - the string prefix all objects in the bucket should start with
      Throws:
      IOException
    • existsAndNotEmpty

      public boolean existsAndNotEmpty(com.google.cloud.storage.BlobId blobId)
      Returns true if a file exists and is non-empty on Google Cloud Storage.
    • getMetadata

      public com.google.common.collect.ImmutableMap<String,String> getMetadata(com.google.cloud.storage.BlobId blobId) throws com.google.cloud.storage.StorageException
      Returns the user defined metadata of a GCS file if the file exists, or an empty map.
      Throws:
      com.google.cloud.storage.StorageException
    • getBlobInfo

      public com.google.cloud.storage.BlobInfo getBlobInfo(com.google.cloud.storage.BlobId blobId) throws com.google.cloud.storage.StorageException
      Returns the BlobInfo of the given GCS file.

      Note that a Blob is returned, but on the BlobInfo part of it is usable.

      Throws:
      com.google.cloud.storage.StorageException
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object