public class GoogleStorageResource extends Object implements org.springframework.core.io.WritableResource
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 and Description |
---|
GoogleStorageResource(Storage storage,
GoogleStorageLocation googleStorageLocation,
boolean autoCreateFiles)
Constructs the resource representation of a bucket or a blob (file) in Google Cloud
Storage.
|
GoogleStorageResource(Storage storage,
String locationUri)
Constructor that defaults autoCreateFiles to true.
|
GoogleStorageResource(Storage storage,
String locationUri,
boolean autoCreateFiles)
Constructs the resource representation of a bucket or a blob (file) in Google Cloud
Storage.
|
Modifier and Type | Method and Description |
---|---|
boolean |
bucketExists()
Checks for the existence of the
Bucket associated with the resource. |
long |
contentLength() |
Blob |
createBlob()
Creates the blob that this
GoogleStorageResource represents in Google Cloud
Storage. |
Blob |
createBlob(byte[] contents)
Creates the blob that this
GoogleStorageResource represents in Google Cloud
Storage and fills it with provided content. |
Bucket |
createBucket()
Creates the bucket that this resource references in Google Cloud Storage.
|
GoogleStorageResource |
createRelative(String relativePath)
Creates a
GoogleStorageResource handle that is relative to this one. |
URL |
createSignedUrl(TimeUnit timeUnit,
long timePeriods,
Storage.SignUrlOption... options)
Creates a signed URL to an object, if it exists.
|
boolean |
exists() |
Blob |
getBlob()
Gets the underlying storage object in Google Cloud Storage.
|
String |
getBlobName() |
Bucket |
getBucket()
Returns the
Bucket associated with the resource. |
String |
getBucketName() |
String |
getDescription() |
File |
getFile() |
String |
getFilename() |
GoogleStorageLocation |
getGoogleStorageLocation() |
InputStream |
getInputStream() |
OutputStream |
getOutputStream()
Returns the output stream for a Google Cloud Storage file.
|
URI |
getURI() |
URL |
getURL() |
boolean |
isAutoCreateFiles() |
boolean |
isBucket() |
boolean |
isOpen() |
boolean |
isReadable() |
boolean |
isWritable() |
long |
lastModified() |
public GoogleStorageResource(Storage storage, String locationUri, boolean autoCreateFiles)
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)IllegalArgumentException
- if the location URI is invalidpublic GoogleStorageResource(Storage storage, String locationUri)
locationUri
- the cloud storage addressstorage
- the storage clientGoogleStorageResource(Storage, String, boolean)
public GoogleStorageResource(Storage storage, GoogleStorageLocation googleStorageLocation, boolean autoCreateFiles)
storage
- the Google Cloud Storage clientgoogleStorageLocation
- the GoogleStorageLocation
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)IllegalArgumentException
- if the location is an invalid Google Storage locationpublic boolean isAutoCreateFiles()
public boolean exists()
exists
in interface org.springframework.core.io.Resource
StorageException
- if an issue occurs getting the Bucket or Blob.public boolean isReadable()
isReadable
in interface org.springframework.core.io.Resource
public boolean isOpen()
isOpen
in interface org.springframework.core.io.Resource
@NonNull public URL getURL() throws IOException
getURL
in interface org.springframework.core.io.Resource
IOException
@NonNull public URI getURI()
getURI
in interface org.springframework.core.io.Resource
public Blob getBlob()
StorageException
- if an issue occurs getting the BlobIllegalStateException
- if the resource reference is to a bucket, and not a blob.public URL createSignedUrl(TimeUnit timeUnit, long timePeriods, Storage.SignUrlOption... options)
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 URLsIllegalStateException
- if the resource reference is to a bucket, and not a blob.StorageException
- if there are errors in accessing Google Storagepublic Blob createBlob()
GoogleStorageResource
represents in 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.public Blob createBlob(byte[] contents)
GoogleStorageResource
represents in Google Cloud
Storage and fills it with provided content.contents
- the initial file contents to writeStorageException
- 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.public Bucket createBucket()
Bucket
object for the bucketStorageException
- if any errors during bucket creation arise,
such as if the bucket already existspublic Bucket getBucket()
Bucket
associated with the resource.public boolean bucketExists()
Bucket
associated with the resource.@NonNull public File getFile()
getFile
in interface org.springframework.core.io.Resource
public long contentLength() throws IOException
contentLength
in interface org.springframework.core.io.Resource
IOException
public long lastModified() throws IOException
lastModified
in interface org.springframework.core.io.Resource
IOException
@NonNull public GoogleStorageResource createRelative(String relativePath)
GoogleStorageResource
handle that is relative to this one. It inherits autoCreateFiles
from this object. Note that it does not actually create the blob.
Note that this method does not actually create the blob.
createRelative
in interface org.springframework.core.io.Resource
relativePath
- the URL to a Google Cloud Storage fileGoogleStorageResource
handle for the relative pathStorageException
- if an issue occurs creating the relative GoogleStorageResourcepublic String getFilename()
getFilename
in interface org.springframework.core.io.Resource
@NonNull public String getDescription()
getDescription
in interface org.springframework.core.io.Resource
@NonNull public InputStream getInputStream() throws IOException
getInputStream
in interface org.springframework.core.io.InputStreamSource
IOException
public boolean isWritable()
isWritable
in interface org.springframework.core.io.WritableResource
@NonNull public OutputStream getOutputStream() throws IOException
getOutputStream
in interface org.springframework.core.io.WritableResource
null
if the object doesn't exist and cannot be
createdIOException
- if an issue occurs getting the OutputStreampublic String getBlobName()
public boolean isBucket()
public String getBucketName()
public GoogleStorageLocation getGoogleStorageLocation()
GoogleStorageLocation
describing the location of the resource in GCSCopyright © 2021. All rights reserved.