public class Storage extends Object
The object storage is organized in two layers. The virtual layer is completely stored in the database and contains all metadata of buckets and objects.
Everytime an object is created or updated, a physical object is created and stored using the PhysicalStorageEngine
of the bucket. Therefore a physical object key which is stored in the virtual objects
always contains the same data. If the data changes, the physical key changes but the virtual key remains the same.
Using this approach, the generated URLs can be cached without having the worry about this in the data model.
For database entities referencing virtual objects a StoredObjectRef
can be used, which takes care of
referential integrity (deletes the object, if the entity is deleted etc.)
Modifier and Type | Field and Description |
---|---|
static String |
FRAMEWORK_STORAGE
Names the framework which must be enabled to activate the storage feature.
|
static sirius.kernel.health.Log |
LOG
Logger used by the storage facility.
|
Constructor and Description |
---|
Storage() |
Modifier and Type | Method and Description |
---|---|
String |
calculateMd5(File file)
Computes a base64 representation of the md5 sum of the given file.
|
protected void |
clearCacheForVirtualObject(VirtualObject virtualObject)
Clears the cache for a given
VirtualObject . |
StoredObject |
createTemporaryObject(Tenant tenant,
String bucketName,
String reference,
String path)
Creates a new temporary object to be used in a
StoredObjectRef . |
protected String |
createURL(DownloadBuilder downloadBuilder)
Creates a download URL for a fully populated builder.
|
void |
delete(StoredObject object)
Deletes the given object and alls of its versions.
|
protected void |
deletePhysicalObject(String bucket,
String physicalKey)
Deletes the physical object in the given bucket.
|
protected void |
deleteReferencedObjects(String reference,
String excludedObjectKey)
Deletes all automatically created objects except the given one.
|
protected void |
deliverPhysicalFile(sirius.web.http.WebContext ctx,
String bucket,
String physicalKey,
String fileExtension)
Delivers a pyhsical file or object.
|
Optional<StoredObject> |
findByKey(Tenant tenant,
String bucket,
String key)
Tries to find the object with the given id, for the given tenant and bucket.
|
Optional<StoredObject> |
findByPath(Tenant tenant,
String bucketName,
String path)
Tries to find the object with the given path, for the given tenant and bucket.
|
StoredObject |
findOrCreateObjectByPath(Tenant tenant,
String bucketName,
String path)
Tries to find the object with the given path, for the given tenant and bucket.
|
Optional<BucketInfo> |
getBucket(String bucket)
Returns the bucket infos for the bucket with the given name.
|
Collection<BucketInfo> |
getBuckets()
Returns a list of all known buckets known to the system.
|
InputStream |
getData(StoredObject file)
Retrieves the actual data stored by the given object.
|
protected PhysicalStorageEngine |
getStorageEngine(String bucketName) |
void |
list(BucketInfo bucket,
Tenant tenant,
Function<StoredObject,Boolean> iterator)
Lists all objects within the given bucket for the given tenant.
|
protected void |
markAsUsed(String reference,
String objectKey)
If an object is created using
createTemporaryObject(Tenant, String, String, String) it is marked
as an temporary upload up until the referencing entity is saved. |
static String |
normalizePath(String path)
Normalizes the given path to be used in
VirtualObject.PATH |
DownloadBuilder |
prepareDownload(String bucket,
String objectKey)
Creates a builder to construct a download URL for an object.
|
protected DownloadBuilder |
prepareDownload(VirtualObject object)
Creates a builder for download URLs based on a
VirtualObject which might avoid a lookup. |
OutputStream |
updateFile(StoredObject file)
Creates a new output stream which updates the contents of the given file.
|
void |
updateFile(StoredObject file,
File data,
String filename)
Updates the given file using the given data.
|
void |
updateFile(StoredObject file,
InputStream data,
String filename,
String md5,
long size)
Updates the given file based on the given stream.
|
protected boolean |
verifyHash(String key,
String hash)
Verifies the authentication hash for the given key.
|
public static final sirius.kernel.health.Log LOG
public static final String FRAMEWORK_STORAGE
public Collection<BucketInfo> getBuckets()
public Optional<BucketInfo> getBucket(String bucket)
bucket
- the name of the bucket to fetch infos forprotected PhysicalStorageEngine getStorageEngine(String bucketName)
public Optional<StoredObject> findByKey(@Nullable Tenant tenant, String bucket, String key)
Uses a cache for the virtual objects
. When fetching an object from the cache, the buckets
and tennants are compared to ensure integrity. Only non-temporary objects are cached.
tenant
- the tenant to filter onbucket
- the bucket to search inkey
- the globally unique id to search byprotected void clearCacheForVirtualObject(VirtualObject virtualObject)
VirtualObject
.virtualObject
- the virtual objectpublic void list(@Nullable BucketInfo bucket, @Nullable Tenant tenant, Function<StoredObject,Boolean> iterator)
bucket
- the bucket to search intenant
- the tenant to filter oniterator
- the iterator to process the objectspublic static String normalizePath(String path)
VirtualObject.PATH
path
- the path to cleanuppublic Optional<StoredObject> findByPath(Tenant tenant, String bucketName, String path)
tenant
- the tenant to filter onbucketName
- the bucket to search inpath
- the path used to lookup the objectpublic StoredObject findOrCreateObjectByPath(Tenant tenant, String bucketName, String path)
tenant
- the tenant to filter onbucketName
- the bucket to search inpath
- the path used to lookup the objectpublic StoredObject createTemporaryObject(Tenant tenant, String bucketName, @Nullable String reference, @Nullable String path)
StoredObjectRef
.
Such objects will be invisible to the user and also be automatically deleted if the referencing entity is deleted. It is made permanent as soon as the referencing entity is saved.
tenant
- the tenant owning the objectbucketName
- the bucket in which the object is placedreference
- the reference for which the object was createdpath
- the path of the object to be createdVirtualObject.TEMPORARY
set to true. Therefore the
referencing entity must be saved to set this flag to false via StoredObjectRefProperty
.protected void deleteReferencedObjects(String reference, @Nullable String excludedObjectKey)
Removes automatically created object (createTemporaryObject(Tenant, String, String, String)
)
if the reference is updated or the referencing entity is deleted.
reference
- the reference (field+id of the entity) being referencedexcludedObjectKey
- if the reference is just changed, the new object is excluded, to just delete the old
objectsprotected void markAsUsed(String reference, String objectKey)
createTemporaryObject(Tenant, String, String, String)
it is marked
as an temporary upload up until the referencing entity is saved.
The StoredObjectRef
and its StoredObjectRefProperty
will then invoke this method to make
the object permanently stored.
reference
- the reference to attach to the object if it was newly createdobjectKey
- the object to mark as permanentpublic String calculateMd5(File file) throws IOException
file
- the file to hashIOException
- in case of an IO errorpublic void updateFile(@Nonnull StoredObject file, @Nonnull File data, @Nullable String filename)
file
- the S3 file to updatedata
- the data to storefilename
- the original filename to savepublic void updateFile(@Nonnull StoredObject file, @Nonnull InputStream data, @Nullable String filename, @Nullable String md5, long size)
file
- the S3 file to updatedata
- the data to storefilename
- the original filename to savemd5
- the md5 hash value to use to verify the uploadsize
- the size (number of bytes) being uploaded (must be positive)public OutputStream updateFile(@Nonnull StoredObject file)
Note that most probably, the file will be updated once the stream is closed and not immediatelly on a write. Also note that it is essential to close the stream to release underlying resources.
file
- the file to update.@Nonnull public InputStream getData(@Nonnull StoredObject file)
file
- the object to fetch the contents forpublic void delete(StoredObject object)
object
- the object to deleteprotected void deletePhysicalObject(String bucket, String physicalKey)
bucket
- the bucket to delete the object fromphysicalKey
- the physical key of the object to deleteprotected boolean verifyHash(String key, String hash)
key
- the key to verifyhash
- the hash to verifyprotected void deliverPhysicalFile(sirius.web.http.WebContext ctx, String bucket, String physicalKey, String fileExtension)
ctx
- the request to respond tobucket
- the bucket to deliver fromphysicalKey
- the physical file to deliverfileExtension
- the file extension of the file (to determine the Content-Type)public DownloadBuilder prepareDownload(String bucket, String objectKey)
bucket
- the bucket containing the objectobjectKey
- the object to create an URL forprotected DownloadBuilder prepareDownload(VirtualObject object)
VirtualObject
which might avoid a lookup.object
- the object to deliverprotected String createURL(DownloadBuilder downloadBuilder)
downloadBuilder
- the builder specifying the details of the urlCopyright © 2018. All rights reserved.