Package com.google.cloud.spring.storage
Class GoogleStorageResource
java.lang.Object
com.google.cloud.spring.storage.GoogleStorageResource
- All Implemented Interfaces:
org.springframework.core.io.InputStreamSource
,org.springframework.core.io.Resource
,org.springframework.core.io.WritableResource
public class GoogleStorageResource
extends Object
implements org.springframework.core.io.WritableResource
Implements
WritableResource
for reading and writing objects in Google Cloud Storage
(GCS). An instance of this class represents a handle to a bucket or a blob.-
Constructor Summary
ConstructorDescriptionGoogleStorageResource
(com.google.cloud.storage.Storage storage, GoogleStorageLocation googleStorageLocation, boolean autoCreateFiles) Constructs the resource representation of a bucket or a blob (file) in Google Cloud Storage.GoogleStorageResource
(com.google.cloud.storage.Storage storage, String locationUri) Constructor that defaults autoCreateFiles to true.GoogleStorageResource
(com.google.cloud.storage.Storage storage, String locationUri, boolean autoCreateFiles) Constructs the resource representation of a bucket or a blob (file) in Google Cloud Storage. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks for the existence of theBucket
associated with the resource.long
com.google.cloud.storage.Blob
Creates the blob that thisGoogleStorageResource
represents in Google Cloud Storage.com.google.cloud.storage.Blob
createBlob
(byte[] contents) Creates the blob that thisGoogleStorageResource
represents in Google Cloud Storage and fills it with provided content.com.google.cloud.storage.Bucket
Creates the bucket that this resource references in Google Cloud Storage.createRelative
(String relativePath) Creates aGoogleStorageResource
handle that is relative to this one.createSignedUrl
(TimeUnit timeUnit, long timePeriods, com.google.cloud.storage.Storage.SignUrlOption... options) Creates a signed URL to an object, if it exists.boolean
exists()
com.google.cloud.storage.Blob
getBlob()
Gets the underlying storage object in Google Cloud Storage.com.google.cloud.storage.Bucket
Returns theBucket
associated with the resource.getFile()
Returns the output stream for a Google Cloud Storage file.getURI()
getURL()
boolean
boolean
isBucket()
boolean
isOpen()
boolean
boolean
long
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.core.io.Resource
getContentAsByteArray, getContentAsString, isFile, readableChannel
Methods inherited from interface org.springframework.core.io.WritableResource
writableChannel
-
Constructor Details
-
GoogleStorageResource
public GoogleStorageResource(com.google.cloud.storage.Storage storage, String locationUri, boolean autoCreateFiles) Constructs the resource representation of a bucket or a blob (file) in Google Cloud Storage.- Parameters:
storage
- the Google Cloud Storage clientlocationUri
- the URI of the bucket or blob, e.g., gs://your-bucket/ or gs://your-bucket/your-file-nameautoCreateFiles
- determines the auto-creation of the file in Google Cloud Storage if an operation that depends on its existence is triggered (e.g., getting the output stream of a file)- Throws:
IllegalArgumentException
- if the location URI is invalid
-
GoogleStorageResource
Constructor that defaults autoCreateFiles to true.- Parameters:
locationUri
- the cloud storage addressstorage
- the storage client- See Also:
-
GoogleStorageResource
public GoogleStorageResource(com.google.cloud.storage.Storage storage, GoogleStorageLocation googleStorageLocation, boolean autoCreateFiles) Constructs the resource representation of a bucket or a blob (file) in Google Cloud Storage.- Parameters:
storage
- the Google Cloud Storage clientgoogleStorageLocation
- theGoogleStorageLocation
of the resource.autoCreateFiles
- determines the auto-creation of the file in Google Cloud Storage if an operation that depends on its existence is triggered (e.g., getting the output stream of a file)- Throws:
IllegalArgumentException
- if the location is an invalid Google Storage location- Since:
- 1.2
-
-
Method Details
-
isAutoCreateFiles
public boolean isAutoCreateFiles() -
exists
public boolean exists()- Specified by:
exists
in interfaceorg.springframework.core.io.Resource
- Returns:
- Returns true if the bucket or object exists.
- Throws:
com.google.cloud.storage.StorageException
- if an issue occurs getting the Bucket or Blob.
-
isReadable
public boolean isReadable()- Specified by:
isReadable
in interfaceorg.springframework.core.io.Resource
-
isOpen
public boolean isOpen()- Specified by:
isOpen
in interfaceorg.springframework.core.io.Resource
-
getURL
- Specified by:
getURL
in interfaceorg.springframework.core.io.Resource
- Returns:
- the self-link for this GCS resource. Note that this is not a URL to download the contents of the file.
- Throws:
IOException
-
getURI
- Specified by:
getURI
in interfaceorg.springframework.core.io.Resource
-
getBlob
public com.google.cloud.storage.Blob getBlob()Gets the underlying storage object in Google Cloud Storage.- Returns:
- the storage object, will be null if it does not exist in Google Cloud Storage.
- Throws:
com.google.cloud.storage.StorageException
- if an issue occurs getting the BlobIllegalStateException
- if the resource reference is to a bucket, and not a blob.
-
createSignedUrl
public URL createSignedUrl(TimeUnit timeUnit, long timePeriods, com.google.cloud.storage.Storage.SignUrlOption... options) Creates a signed URL to an object, if it exists. This method will fail if this storage resource was not created using service account credentials.- Parameters:
timeUnit
- the time unit used to determine how long the URL is valid.timePeriods
- the number of periods to determine how long the URL is valid.options
- specifies additional options for signing URLs- Returns:
- the URL if the object exists, and null if it does not.
- Throws:
IllegalStateException
- if the resource reference is to a bucket, and not a blob.com.google.cloud.storage.StorageException
- if there are errors in accessing Google Storage
-
createBlob
public com.google.cloud.storage.Blob createBlob()Creates the blob that thisGoogleStorageResource
represents in Google Cloud Storage.- Returns:
- the created blob object
- Throws:
com.google.cloud.storage.StorageException
- if any errors during blob creation arise, such as if the blob already existsIllegalStateException
- if the resource reference is to a bucket, and not a blob.
-
createBlob
public com.google.cloud.storage.Blob createBlob(byte[] contents) Creates the blob that thisGoogleStorageResource
represents in Google Cloud Storage and fills it with provided content.- Parameters:
contents
- the initial file contents to write- Returns:
- the created blob object
- Throws:
com.google.cloud.storage.StorageException
- if any errors during blob creation arise, such as if the blob already existsIllegalStateException
- if the resource reference is to a bucket, and not a blob.- Since:
- 1.2.2
-
createBucket
public com.google.cloud.storage.Bucket createBucket()Creates the bucket that this resource references in Google Cloud Storage.- Returns:
- the
Bucket
object for the bucket - Throws:
com.google.cloud.storage.StorageException
- if any errors during bucket creation arise, such as if the bucket already exists
-
getBucket
public com.google.cloud.storage.Bucket getBucket()Returns theBucket
associated with the resource.- Returns:
- the bucket if it exists, or null otherwise
-
bucketExists
public boolean bucketExists()Checks for the existence of theBucket
associated with the resource.- Returns:
- true if the bucket exists
-
getFile
- Specified by:
getFile
in interfaceorg.springframework.core.io.Resource
-
contentLength
- Specified by:
contentLength
in interfaceorg.springframework.core.io.Resource
- Throws:
IOException
-
lastModified
- Specified by:
lastModified
in interfaceorg.springframework.core.io.Resource
- Throws:
IOException
-
createRelative
Creates aGoogleStorageResource
handle that is relative to this one. It inheritsautoCreateFiles
from this object. Note that it does not actually create the blob.Note that this method does not actually create the blob.
- Specified by:
createRelative
in interfaceorg.springframework.core.io.Resource
- Parameters:
relativePath
- the URL to a Google Cloud Storage file- Returns:
- the
GoogleStorageResource
handle for the relative path - Throws:
com.google.cloud.storage.StorageException
- if an issue occurs creating the relative GoogleStorageResource
-
getFilename
- Specified by:
getFilename
in interfaceorg.springframework.core.io.Resource
-
getDescription
- Specified by:
getDescription
in interfaceorg.springframework.core.io.Resource
-
getInputStream
- Specified by:
getInputStream
in interfaceorg.springframework.core.io.InputStreamSource
- Throws:
IOException
-
isWritable
public boolean isWritable()- Specified by:
isWritable
in interfaceorg.springframework.core.io.WritableResource
-
getOutputStream
Returns the output stream for a Google Cloud Storage file.- Specified by:
getOutputStream
in interfaceorg.springframework.core.io.WritableResource
- Returns:
- the object's output stream or
null
if the object doesn't exist and cannot be created - Throws:
IOException
- if an issue occurs getting the OutputStream
-
getBlobName
- Returns:
- the blob name of the Google Storage Resource; null if the resource is a bucket
-
isBucket
public boolean isBucket()- Returns:
- true if the resource is a bucket; false otherwise
-
getBucketName
- Returns:
- the bucket name of the Google Storage Resource
-
getGoogleStorageLocation
- Returns:
- the
GoogleStorageLocation
describing the location of the resource in GCS - Since:
- 1.2
-