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
ConstructorsConstructorDescriptionGoogleStorageResource(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 TypeMethodDescriptionbooleanChecks for the existence of theBucketassociated with the resource.longcom.google.cloud.storage.BlobCreates the blob that thisGoogleStorageResourcerepresents in Google Cloud Storage.com.google.cloud.storage.BlobcreateBlob(byte[] contents) Creates the blob that thisGoogleStorageResourcerepresents in Google Cloud Storage and fills it with provided content.com.google.cloud.storage.BucketCreates the bucket that this resource references in Google Cloud Storage.createRelative(String relativePath) Creates aGoogleStorageResourcehandle 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.booleanexists()com.google.cloud.storage.BlobgetBlob()Gets the underlying storage object in Google Cloud Storage.com.google.cloud.storage.BucketReturns theBucketassociated with the resource.getFile()Returns the output stream for a Google Cloud Storage file.getURI()getURL()booleanbooleanisBucket()booleanisOpen()booleanbooleanlongMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.core.io.Resource
getContentAsByteArray, getContentAsString, isFile, readableChannelMethods 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- theGoogleStorageLocationof 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:
existsin 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:
isReadablein interfaceorg.springframework.core.io.Resource
-
isOpen
public boolean isOpen()- Specified by:
isOpenin interfaceorg.springframework.core.io.Resource
-
getURL
- Specified by:
getURLin 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:
getURIin 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 thisGoogleStorageResourcerepresents 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 thisGoogleStorageResourcerepresents 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
Bucketobject 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 theBucketassociated with the resource.- Returns:
- the bucket if it exists, or null otherwise
-
bucketExists
public boolean bucketExists()Checks for the existence of theBucketassociated with the resource.- Returns:
- true if the bucket exists
-
getFile
- Specified by:
getFilein interfaceorg.springframework.core.io.Resource
-
contentLength
- Specified by:
contentLengthin interfaceorg.springframework.core.io.Resource- Throws:
IOException
-
lastModified
- Specified by:
lastModifiedin interfaceorg.springframework.core.io.Resource- Throws:
IOException
-
createRelative
Creates aGoogleStorageResourcehandle that is relative to this one. It inheritsautoCreateFilesfrom this object. Note that it does not actually create the blob.Note that this method does not actually create the blob.
- Specified by:
createRelativein interfaceorg.springframework.core.io.Resource- Parameters:
relativePath- the URL to a Google Cloud Storage file- Returns:
- the
GoogleStorageResourcehandle for the relative path - Throws:
com.google.cloud.storage.StorageException- if an issue occurs creating the relative GoogleStorageResource
-
getFilename
- Specified by:
getFilenamein interfaceorg.springframework.core.io.Resource
-
getDescription
- Specified by:
getDescriptionin interfaceorg.springframework.core.io.Resource
-
getInputStream
- Specified by:
getInputStreamin interfaceorg.springframework.core.io.InputStreamSource- Throws:
IOException
-
isWritable
public boolean isWritable()- Specified by:
isWritablein interfaceorg.springframework.core.io.WritableResource
-
getOutputStream
Returns the output stream for a Google Cloud Storage file.- Specified by:
getOutputStreamin interfaceorg.springframework.core.io.WritableResource- Returns:
- the object's output stream or
nullif 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
GoogleStorageLocationdescribing the location of the resource in GCS - Since:
- 1.2
-