Class GoogleCloudStorageProvider
java.lang.Object
com.broadleafcommerce.asset.storage.provider.service.GoogleCloudStorageProvider
- All Implemented Interfaces:
StorageProvider
The default implementation of the
StorageProvider
for the Google Cloud storage.- Author:
- Dima Myroniuk (dmyroniuk)
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionGoogleCloudStorageProvider
(InternalAssetProperties internalAssetProperties, com.google.cloud.storage.Storage storage, jakarta.activation.FileTypeMap mimeFileTypeMap, com.broadleafcommerce.common.extension.TypeFactory typeFactory) -
Method Summary
Modifier and TypeMethodDescriptionaddResourceFromStream
(@NonNull InputStream resource, @NonNull String fileName) Adds a new resource to the configured storage provider using the providedInputStream
and filename.addResourcesFromFiles
(@NonNull Map<String, File> fileNamesToResources) addResourcesFromStreams
(@NonNull Map<String, InputStream> fileNamesToResources) Adds new resources to the configured storage provider using the providedMap
ofInputStreams
and filenames.protected void
assertAllPathsNotNull
(Iterable<String> paths) protected void
assertPathNotNull
(String path) protected StorageLocationMapping
buildStorageLocationMapping
(String url, String actualResourcePath) protected StorageLocationMapping
createNewMappingRecord
(String url, String actualResourcePath) Similar tocreateNewMappingRecords(Map)
, but only for one record.protected Map<String,
StorageLocationMapping> createNewMappingRecords
(Map<String, String> actualResourcePathsByOriginalLocation) Establishes newStorageLocationMapping
records inStorageLocationMappingService
for the given values.protected void
deleteMappingRecords
(Collection<String> originalLocations) void
deleteResource
(@NonNull String path) Deletes the resource in the configured storage provider at the given path string if it exists.void
deleteResources
(@NonNull Iterable<String> paths) Deletes the resources in the configured storage provider at the given path strings if they exist.protected <R> R
doWithoutPolicyValidation
(Supplier<R> returningWork) Executes the givenreturningWork
with security policy validations disabled.Queries againstStorageLocationMappingService
to find the actual storage locations from the mapping records, if available in the system.protected String
Similar togenerateActualStorageLocations(Collection)
, but only for one URL.Used during a 'resource add' operation to determine where new assets should actually be stored.protected String
protected com.google.cloud.storage.BlobId
Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.3, not used by the framework anymore.protected String
protected String
protected InternalAssetProperties
protected int
protected jakarta.activation.FileTypeMap
protected ResourceWithMetadata
getResourceWithMetadata
(@NonNull com.google.cloud.storage.Blob blob) protected com.google.cloud.storage.Storage
protected String
protected com.broadleafcommerce.common.extension.TypeFactory
boolean
isWhitelistedMimeType
(@NonNull String mimeType) Determines whether the given MIME type string is supported by this provider.protected String
prependBucketPathPrefix
(String resourcePath) readStreamableResourcesWithMetadata
(@NonNull Iterable<String> paths) Retrieves theresources
in the configured storage provider at the given path strings if they exist.readStreamableResourceWithMetadata
(@NonNull String path) Retrieves theresource
in the configured storage provider at the given path string if it exists.protected com.google.cloud.storage.BlobId
Similar toresolveExistingActualStorageLocations(Collection)
, but only for one URL.Used during resource read/delete operations to figure out what the actual storage locations are for some given URLs.void
setStorageLocationMappingSvc
(StorageLocationMappingService<StorageLocationMapping> storageLocationMappingSvc) protected com.google.cloud.storage.BlobId
protected String
validateAndCleanFilePath
(String fileName) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.broadleafcommerce.asset.storage.provider.service.StorageProvider
moveResourceFromDirectory, moveResourceFromDirectoryAndGetPath, moveResourcesFromDirectory, moveResourcesFromDirectoryAndGetPaths, readResource, readResources, readStreamableResource, readStreamableResources
-
Field Details
-
DEFAULT_STORAGE_LOCATION_MAPPING_PROVIDER_TYPE
-
-
Constructor Details
-
GoogleCloudStorageProvider
public GoogleCloudStorageProvider(InternalAssetProperties internalAssetProperties, com.google.cloud.storage.Storage storage, jakarta.activation.FileTypeMap mimeFileTypeMap, com.broadleafcommerce.common.extension.TypeFactory typeFactory)
-
-
Method Details
-
isWhitelistedMimeType
Description copied from interface:StorageProvider
Determines whether the given MIME type string is supported by this provider. Files with MIME types not supported, cannot be stored using this provider. The whitelist can be configured using a system property of the formbroadleaf.asset.internal.storageProvider.mimeTypeWhitelist
.If the whitelist is empty, then all types are considered whitelisted.
- Specified by:
isWhitelistedMimeType
in interfaceStorageProvider
- Parameters:
mimeType
- the mime type to check against the provider's whitelist- Returns:
- whether the given MIME type string is supported by this provider.
- See Also:
-
readStreamableResourceWithMetadata
public Optional<ResourceWithMetadata> readStreamableResourceWithMetadata(@NonNull @NonNull String path) Description copied from interface:StorageProvider
Retrieves theresource
in the configured storage provider at the given path string if it exists.- Specified by:
readStreamableResourceWithMetadata
in interfaceStorageProvider
- Parameters:
path
- fully qualified path to the resource's location in the storage provider- Returns:
- Either the
resource
wrapped in anOptional
if it exists or elseOptional.empty()
.
-
getResourceWithMetadata
protected ResourceWithMetadata getResourceWithMetadata(@NonNull @NonNull com.google.cloud.storage.Blob blob) -
readStreamableResourcesWithMetadata
public Iterable<ResourceWithMetadata> readStreamableResourcesWithMetadata(@NonNull @NonNull Iterable<String> paths) Description copied from interface:StorageProvider
Retrieves theresources
in the configured storage provider at the given path strings if they exist.- Specified by:
readStreamableResourcesWithMetadata
in interfaceStorageProvider
- Parameters:
paths
- fully qualified path to the resources' locations in the storage provider- Returns:
Iterable
ofresources
that were found
-
addResourceFromStream
public String addResourceFromStream(@NonNull @NonNull InputStream resource, @NonNull @NonNull String fileName) Description copied from interface:StorageProvider
Adds a new resource to the configured storage provider using the providedInputStream
and filename. Then, it returns the fully qualified path of the newly stored resource.- Specified by:
addResourceFromStream
in interfaceStorageProvider
- Parameters:
resource
-resource
to add to the configured storage providerfileName
- the name of the resource, potentially including path segments.- Returns:
- the fully qualified path to the added resource
-
deleteMappingRecords
-
createNewMappingRecords
protected Map<String,StorageLocationMapping> createNewMappingRecords(Map<String, String> actualResourcePathsByOriginalLocation) Establishes newStorageLocationMapping
records inStorageLocationMappingService
for the given values.- Parameters:
actualResourcePathsByOriginalLocation
- a map from original location values to the actual resource paths for them- Returns:
- the created
StorageLocationMapping
records mapped by the original location
-
createNewMappingRecord
@Nullable protected StorageLocationMapping createNewMappingRecord(String url, String actualResourcePath) Similar tocreateNewMappingRecords(Map)
, but only for one record.- Parameters:
url
- the URL the caller provided to this storage provider to associate with some binary data. This is the value callers will give to the storage provider to store/read/delete the resource. This is expected to already be globally unique.actualResourcePath
- the actual storage location/path which this provider is using to actually store the data for theurl
- Returns:
- the created
StorageLocationMapping
if successful,null
otherwise
-
buildStorageLocationMapping
-
addResourcesFromStreams
public BulkAddResourcesResponse addResourcesFromStreams(@NonNull @NonNull Map<String, InputStream> fileNamesToResources) Description copied from interface:StorageProvider
Adds new resources to the configured storage provider using the provided
Map
ofInputStreams
and filenames. Then, it returns aBulkAddResourcesResponse
containing the information about which files succeeded and which files failed.- Specified by:
addResourcesFromStreams
in interfaceStorageProvider
- Parameters:
fileNamesToResources
- a map of filenames toresources
that should be copied to the storage provider. The filenames can include path segments.- Returns:
- a
BulkAddResourcesResponse
containing the details of which files did and didn't succeed
-
addResourcesFromFiles
public BulkAddResourcesResponse addResourcesFromFiles(@NonNull @NonNull Map<String, File> fileNamesToResources) Description copied from interface:StorageProvider
Adds new resources to the configured storage provider using the providedMap
ofFiles
and filenames. Then, it returns aBulkAddResourcesResponse
containing the information about which files succeeded and which files failed.- Specified by:
addResourcesFromFiles
in interfaceStorageProvider
- Parameters:
fileNamesToResources
- a map of filenames toresources
that should be copied to the storage provider. The filenames can include path segments.- Returns:
- a
BulkAddResourcesResponse
containing the details of which files did and didn't succeed
-
deleteResource
Description copied from interface:StorageProvider
Deletes the resource in the configured storage provider at the given path string if it exists.- Specified by:
deleteResource
in interfaceStorageProvider
- Parameters:
path
- fully qualified path to the resource's location in the storage provider
-
deleteResources
Description copied from interface:StorageProvider
Deletes the resources in the configured storage provider at the given path strings if they exist. This method will attempt to remove all the resources it can successfully before throwing aBulkStorageException
containing all the failures and their causes.- Specified by:
deleteResources
in interfaceStorageProvider
- Parameters:
paths
- fully qualified paths to the resources' locations in the storage provider
-
getBlobId
@Deprecated(since="2.0.3", forRemoval=true) protected com.google.cloud.storage.BlobId getBlobId(String fileName) Deprecated, for removal: This API element is subject to removal in a future version.since 2.0.3, not used by the framework anymore. UsegenerateActualStorageLocations(Collection)
orresolveExistingActualStorageLocations(Collection)
instead.- Parameters:
fileName
- the given path to return the blob ID for- Returns:
- the BlobId that can be used for the given path
-
toBlobId
- Parameters:
gcsObjectName
- theBlobId.getName()
to use- Returns:
- the
BlobId
for the given name in theInternalAssetProperties.StorageProvider.GoogleCloudStorage.getBucketName()
-
getGCSProperties
-
prependBucketPathPrefix
-
validateAndCleanFilePath
-
resolveExistingActualStorageLocations
protected Map<String,com.google.cloud.storage.BlobId> resolveExistingActualStorageLocations(Collection<String> urls) Used during resource read/delete operations to figure out what the actual storage locations are for some given URLs.- Parameters:
urls
- the URLs the caller provided to this storage provider to associate with some binary data. This is the value callers will give to the storage provider to store/read/delete the resources. These are expected to already be globally unique.- Returns:
- a map from the originally provided URL to the actual storage location for it
- Since:
- 2.0.3
- See Also:
-
findActualStorageLocationsFromMappings
Queries againstStorageLocationMappingService
to find the actual storage locations from the mapping records, if available in the system.- Parameters:
urls
- the URLs the caller provided to this storage provider to associate with some binary data. This is the value callers will give to the storage provider to store/read/delete the resources. These are expected to already be globally unique.- Returns:
- a map from each provided URL to the found actual storage location. If a URL did not have a mapping found, it will be omitted in the map.
- Since:
- 2.0.3
-
getStorageLocationMappingProviderType
- Returns:
- the value used for
StorageLocationMapping.getStorageProviderType()
-
doWithoutPolicyValidation
Executes the givenreturningWork
with security policy validations disabled.- Type Parameters:
R
- the return type of the work- Parameters:
returningWork
- the work to execute- Returns:
- the result of executing the work
-
resolveExistingActualStorageLocation
Similar toresolveExistingActualStorageLocations(Collection)
, but only for one URL.- Parameters:
url
- the URL the caller provided to this storage provider to associate with some binary data. This is the value callers will give to the storage provider to store/read/delete the resource. This is expected to already be globally unique.- Returns:
- the actual storage location/path which this provider is using to actually store the
data for the
url
- Since:
- 2.0.3
- See Also:
-
getHashedResourcePath
-
generateActualStorageLocations
Used during a 'resource add' operation to determine where new assets should actually be stored.
Note - as of 2.0.3, all new resources will be stored in paths determined by this method.
- Parameters:
urls
- the URLs the caller provided to this storage provider to associate with some binary data. This is the value callers will give to the storage provider to store/read/delete the resources. These are expected to already be globally unique.- Returns:
- a map from each provided URL to a new unique location/path which this provider will
use to actually store data for that URL. The mappings in this result should be stored
via
StorageLocationMappingService
. - Since:
- 2.0.3
- See Also:
-
generateActualStorageLocation
Similar togenerateActualStorageLocations(Collection)
, but only for one URL.- Parameters:
url
- the URL the caller provided to this storage provider to associate with some binary data. This is the value callers will give to the storage provider to store/read/delete the resource. This is expected to already be globally unique.- Returns:
- a new unique location/path which this provider will use to actually store data for
the provided URL. The mapping from the input URL to this result should be stored via
StorageLocationMappingService
. - Since:
- 2.0.3
- See Also:
-
generateGloballyUniqueFilename
- Parameters:
url
- the URL the caller provided to this storage provider to associate with some binary data. This is the value callers will give to the storage provider to store/read/delete the resource. This is expected to already be globally unique.- Returns:
- a globally unique filename this provider should use when actually storing the data. Uniqueness is very important, as it ensures even similarly named assets will have their own unique path.
-
getFileExtensionWithDot
-
getMaxGeneratedDirectoryDepth
protected int getMaxGeneratedDirectoryDepth() -
assertPathNotNull
-
assertAllPathsNotNull
-
getInternalAssetProperties
-
getStorage
protected com.google.cloud.storage.Storage getStorage() -
getMimeFileTypeMap
protected jakarta.activation.FileTypeMap getMimeFileTypeMap() -
getTypeFactory
protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory() -
setStorageLocationMappingSvc
@Autowired public void setStorageLocationMappingSvc(StorageLocationMappingService<StorageLocationMapping> storageLocationMappingSvc) -
getStorageLocationMappingSvc
-