Package google.registry.gcs
Class GcsUtils
java.lang.Object
google.registry.gcs.GcsUtils
- All Implemented Interfaces:
Serializable
Utilities for working with Google Cloud Storage.
It is Serializable
so that it can be used in Beam.
- See Also:
-
Constructor Summary
ConstructorDescriptionGcsUtils
(com.google.cloud.storage.StorageOptions storageOptions) GcsUtils
(GoogleCredentialsBundle credentialsBundle) -
Method Summary
Modifier and TypeMethodDescriptionvoid
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) Returnstrue
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 theBlobInfo
of the given GCS file.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
hashCode()
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 anInputStream
.openOutputStream
(com.google.cloud.storage.BlobId blobId) Opens a GCS file for writing as anOutputStream
, 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 anOutputStream
, 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
-
Constructor Details
-
GcsUtils
-
GcsUtils
public GcsUtils(com.google.cloud.storage.StorageOptions storageOptions)
-
-
Method Details
-
openInputStream
Opens a GCS file for reading as anInputStream
. -
openOutputStream
Opens a GCS file for writing as anOutputStream
, 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 anOutputStream
, 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 slashesprefix
- the string prefix all objects in the bucket should start with- Throws:
IOException
-
existsAndNotEmpty
public boolean existsAndNotEmpty(com.google.cloud.storage.BlobId blobId) Returnstrue
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 theBlobInfo
of the given GCS file.Note that a
Blob
is returned, but on theBlobInfo
part of it is usable.- Throws:
com.google.cloud.storage.StorageException
-
equals
-
hashCode
public int hashCode()
-