Class FileStore


  • @Component
    public class FileStore
    extends java.lang.Object
    S3 Mock file store.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileStore​(java.lang.String rootDirectory)
      Constructs a new FileStore.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void abortMultipartUpload​(java.lang.String bucketName, java.lang.String fileName, java.lang.String uploadId)
      Aborts the upload.
      java.lang.String completeMultipartUpload​(java.lang.String bucketName, java.lang.String fileName, java.lang.String uploadId, java.util.List<Part> parts)
      Finishes the upload.
      java.lang.String completeMultipartUpload​(java.lang.String bucketName, java.lang.String fileName, java.lang.String uploadId, java.util.List<Part> parts, java.lang.String encryption, java.lang.String kmsKeyId)
      Completes an Multipart Upload for the given Id.
      java.lang.String copyPart​(java.lang.String bucket, java.lang.String key, int from, int to, java.lang.String partNumber, java.lang.String destinationBucket, java.lang.String destinationFilename, java.lang.String uploadId)
      Copies the range, define by from/to, from the S3 Object, identified by the given key to given destination into the given bucket.
      CopyObjectResult copyS3Object​(java.lang.String sourceBucketName, java.lang.String sourceObjectName, java.lang.String destinationBucketName, java.lang.String destinationObjectName)
      Copies an object, identified by bucket and name, to a another bucket and objectName.
      CopyObjectResult copyS3Object​(java.lang.String sourceBucketName, java.lang.String sourceObjectName, java.lang.String destinationBucketName, java.lang.String destinationObjectName, java.util.Map<java.lang.String,​java.lang.String> userMetadata)
      Deprecated.
      This method is not used in S3Mock.
      CopyObjectResult copyS3ObjectEncrypted​(java.lang.String sourceBucketName, java.lang.String sourceObjectName, java.lang.String destinationBucketName, java.lang.String destinationObjectName, java.lang.String encryption, java.lang.String kmsKeyId)
      Copies an object to another bucket and encrypted object.
      CopyObjectResult copyS3ObjectEncrypted​(java.lang.String sourceBucketName, java.lang.String sourceObjectName, java.lang.String destinationBucketName, java.lang.String destinationObjectName, java.lang.String encryption, java.lang.String kmsKeyId, java.util.Map<java.lang.String,​java.lang.String> userMetadata)
      Copies an object to another bucket and encrypted object.
      Bucket createBucket​(java.lang.String bucketName)
      Creates a new bucket.
      boolean deleteBucket​(java.lang.String bucketName)
      Deletes a Bucket and all of its contents.
      boolean deleteObject​(java.lang.String bucketName, java.lang.String objectName)
      Removes an object from a bucket.
      java.lang.Boolean doesBucketExist​(java.lang.String bucketName)
      Checks if the specified bucket exists.
      Bucket getBucket​(java.lang.String bucketName)
      Retrieves a bucket identified by its name.
      java.util.List<Part> getMultipartUploadParts​(java.lang.String bucketName, java.lang.String fileName, java.lang.String uploadId)
      Get all multipart upload parts.
      S3Object getS3Object​(java.lang.String bucketName, java.lang.String objectName)
      Retrieves an Object from a bucket.
      java.util.List<S3Object> getS3Objects​(java.lang.String bucketName, java.lang.String prefix)
      Retrieves list of Objects from a bucket.
      java.util.List<Bucket> listBuckets()
      Lists all buckets managed by this FileStore.
      java.util.Collection<MultipartUpload> listMultipartUploads()
      Lists the not-yet completed parts of an multipart upload.
      MultipartUpload prepareMultipartUpload​(java.lang.String bucketName, java.lang.String fileName, java.lang.String contentType, java.lang.String contentEncoding, java.lang.String uploadId, Owner owner, Owner initiator)
      Prepares everything to store files uploaded as multipart upload.
      MultipartUpload prepareMultipartUpload​(java.lang.String bucketName, java.lang.String fileName, java.lang.String contentType, java.lang.String contentEncoding, java.lang.String uploadId, Owner owner, Owner initiator, java.util.Map<java.lang.String,​java.lang.String> userMetadata)
      Prepares everything to store files uploaded as multipart upload.
      java.lang.String putPart​(java.lang.String bucketName, java.lang.String fileName, java.lang.String uploadId, java.lang.String partNumber, java.io.InputStream inputStream, boolean useV4ChunkedWithSigningFormat)
      Uploads a part of a multipart upload.
      S3Object putS3Object​(java.lang.String bucketName, java.lang.String fileName, java.lang.String contentType, java.lang.String contentEncoding, java.io.InputStream dataStream, boolean useV4ChunkedWithSigningFormat)
      Deprecated.
      This method is only used in S3Mock tests.
      S3Object putS3Object​(java.lang.String bucketName, java.lang.String fileName, java.lang.String contentType, java.lang.String contentEncoding, java.io.InputStream dataStream, boolean useV4ChunkedWithSigningFormat, java.util.Map<java.lang.String,​java.lang.String> userMetadata)
      Deprecated.
      This method is not used in S3Mock.
      S3Object putS3Object​(java.lang.String bucketName, java.lang.String fileName, java.lang.String contentType, java.lang.String contentEncoding, java.io.InputStream dataStream, boolean useV4ChunkedWithSigningFormat, java.util.Map<java.lang.String,​java.lang.String> userMetadata, java.lang.String encryption, java.lang.String kmsKeyId)
      Generically stores a File inside a Bucket.
      S3Object putS3ObjectWithKMSEncryption​(java.lang.String bucketName, java.lang.String fileName, java.lang.String contentType, java.io.InputStream dataStream, boolean useV4ChunkedWithSigningFormat, java.lang.String encryption, java.lang.String kmsKeyId)
      Deprecated.
      This method is not used in S3Mock.
      S3Object putS3ObjectWithKMSEncryption​(java.lang.String bucketName, java.lang.String fileName, java.lang.String contentType, java.io.InputStream dataStream, boolean useV4ChunkedWithSigningFormat, java.util.Map<java.lang.String,​java.lang.String> userMetadata, java.lang.String encryption, java.lang.String kmsKeyId)
      Deprecated.
      This method is not used in S3Mock.
      void setObjectTags​(java.lang.String bucketName, java.lang.String fileName, java.util.List<Tag> tags)
      Sets tags for a given object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileStore

        public FileStore​(@Value("${root:}")
                         java.lang.String rootDirectory)
        Constructs a new FileStore.
        Parameters:
        rootDirectory - The directory to use. If omitted, a temp directory will be used.
    • Method Detail

      • createBucket

        public Bucket createBucket​(java.lang.String bucketName)
                            throws java.io.IOException
        Creates a new bucket.
        Parameters:
        bucketName - name of the Bucket to be created.
        Returns:
        the newly created Bucket.
        Throws:
        java.io.IOException - if the bucket cannot be created or the bucket already exists but is not a directory.
      • listBuckets

        public java.util.List<Bucket> listBuckets()
        Lists all buckets managed by this FileStore.
        Returns:
        List of all Buckets.
      • getBucket

        public Bucket getBucket​(java.lang.String bucketName)
        Retrieves a bucket identified by its name.
        Parameters:
        bucketName - name of the bucket to be retrieved
        Returns:
        the Bucket or null if not found
      • putS3Object

        @Deprecated
        public S3Object putS3Object​(java.lang.String bucketName,
                                    java.lang.String fileName,
                                    java.lang.String contentType,
                                    java.lang.String contentEncoding,
                                    java.io.InputStream dataStream,
                                    boolean useV4ChunkedWithSigningFormat)
                             throws java.io.IOException
        Deprecated.
        This method is only used in S3Mock tests.
        Stores a File inside a Bucket.
        Parameters:
        bucketName - Bucket to store the File in.
        fileName - name of the File to be stored.
        contentType - The files Content Type.
        contentEncoding - The files Content Encoding.
        dataStream - The File as InputStream.
        useV4ChunkedWithSigningFormat - If true, V4-style signing is enabled.
        Returns:
        S3Object.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • putS3Object

        @Deprecated
        public S3Object putS3Object​(java.lang.String bucketName,
                                    java.lang.String fileName,
                                    java.lang.String contentType,
                                    java.lang.String contentEncoding,
                                    java.io.InputStream dataStream,
                                    boolean useV4ChunkedWithSigningFormat,
                                    java.util.Map<java.lang.String,​java.lang.String> userMetadata)
                             throws java.io.IOException
        Deprecated.
        This method is not used in S3Mock.
        Stores a File inside a Bucket.
        Parameters:
        bucketName - Bucket to store the File in.
        fileName - name of the File to be stored.
        contentType - The files Content Type.
        contentEncoding - The files Content Encoding.
        dataStream - The File as InputStream.
        useV4ChunkedWithSigningFormat - If true, V4-style signing is enabled.
        userMetadata - User metadata to store for this object, will be available for the object with the key prefixed with "x-amz-meta-".
        Returns:
        S3Object.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • putS3Object

        public S3Object putS3Object​(java.lang.String bucketName,
                                    java.lang.String fileName,
                                    java.lang.String contentType,
                                    java.lang.String contentEncoding,
                                    java.io.InputStream dataStream,
                                    boolean useV4ChunkedWithSigningFormat,
                                    java.util.Map<java.lang.String,​java.lang.String> userMetadata,
                                    java.lang.String encryption,
                                    java.lang.String kmsKeyId)
                             throws java.io.IOException
        Generically stores a File inside a Bucket.
        Parameters:
        bucketName - Bucket to store the File in.
        fileName - name of the File to be stored.
        contentType - The files Content Type.
        contentEncoding - The files Content Encoding.
        dataStream - The File as InputStream.
        useV4ChunkedWithSigningFormat - If true, V4-style signing is enabled.
        userMetadata - User metadata to store for this object, will be available for the object with the key prefixed with "x-amz-meta-".
        encryption - The Encryption Type.
        kmsKeyId - The KMS encryption key id.
        Returns:
        S3Object.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • putS3ObjectWithKMSEncryption

        @Deprecated
        public S3Object putS3ObjectWithKMSEncryption​(java.lang.String bucketName,
                                                     java.lang.String fileName,
                                                     java.lang.String contentType,
                                                     java.io.InputStream dataStream,
                                                     boolean useV4ChunkedWithSigningFormat,
                                                     java.lang.String encryption,
                                                     java.lang.String kmsKeyId)
                                              throws java.io.IOException
        Deprecated.
        This method is not used in S3Mock.
        Stores an encrypted File inside a Bucket.
        Parameters:
        bucketName - Bucket to store the File in.
        fileName - name of the File to be stored.
        contentType - The files Content Type.
        dataStream - The File as InputStream.
        useV4ChunkedWithSigningFormat - If true, V4-style signing is enabled.
        encryption - The Encryption Type.
        kmsKeyId - The KMS encryption key id.
        Returns:
        S3Object.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • putS3ObjectWithKMSEncryption

        @Deprecated
        public S3Object putS3ObjectWithKMSEncryption​(java.lang.String bucketName,
                                                     java.lang.String fileName,
                                                     java.lang.String contentType,
                                                     java.io.InputStream dataStream,
                                                     boolean useV4ChunkedWithSigningFormat,
                                                     java.util.Map<java.lang.String,​java.lang.String> userMetadata,
                                                     java.lang.String encryption,
                                                     java.lang.String kmsKeyId)
                                              throws java.io.IOException
        Deprecated.
        This method is not used in S3Mock.
        Stores an encrypted File inside a Bucket.
        Parameters:
        bucketName - Bucket to store the File in.
        fileName - name of the File to be stored.
        contentType - The files Content Type.
        dataStream - The File as InputStream.
        useV4ChunkedWithSigningFormat - If true, V4-style signing is enabled.
        userMetadata - User metadata to store for this object, will be available for the object with the key prefixed with "x-amz-meta-".
        encryption - The Encryption Type.
        kmsKeyId - The KMS encryption key id.
        Returns:
        S3Object.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • setObjectTags

        public void setObjectTags​(java.lang.String bucketName,
                                  java.lang.String fileName,
                                  java.util.List<Tag> tags)
                           throws java.io.IOException
        Sets tags for a given object.
        Parameters:
        bucketName - Bucket where the file is stored in.
        fileName - name of the file to which tags have to be attached.
        tags - List of tag objects.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getS3Object

        public S3Object getS3Object​(java.lang.String bucketName,
                                    java.lang.String objectName)
        Retrieves an Object from a bucket.
        Parameters:
        bucketName - the Bucket in which to look the file in.
        objectName - name of the object.
        Returns:
        the retrieved S3Object or null if not found
      • getS3Objects

        public java.util.List<S3Object> getS3Objects​(java.lang.String bucketName,
                                                     java.lang.String prefix)
                                              throws java.io.IOException
        Retrieves list of Objects from a bucket.
        Parameters:
        bucketName - the Bucket in which to list the file(s) in.
        prefix - String object file name starts with
        Returns:
        the retrieved List<S3Object> or null if not found
        Throws:
        java.io.IOException - if directory stream fails
      • copyS3Object

        public CopyObjectResult copyS3Object​(java.lang.String sourceBucketName,
                                             java.lang.String sourceObjectName,
                                             java.lang.String destinationBucketName,
                                             java.lang.String destinationObjectName)
                                      throws java.io.IOException
        Copies an object, identified by bucket and name, to a another bucket and objectName.
        Parameters:
        sourceBucketName - name of the bucket to copy from.
        sourceObjectName - name of the object to copy.
        destinationBucketName - name of the destination bucket.
        destinationObjectName - name of the destination object.
        Returns:
        an CopyObjectResult or null if source couldn't be found.
        Throws:
        java.io.FileNotFoundException - no FileInputStream of the sourceFile can be created.
        java.io.IOException - If File can't be read.
      • copyS3Object

        @Deprecated
        public CopyObjectResult copyS3Object​(java.lang.String sourceBucketName,
                                             java.lang.String sourceObjectName,
                                             java.lang.String destinationBucketName,
                                             java.lang.String destinationObjectName,
                                             java.util.Map<java.lang.String,​java.lang.String> userMetadata)
                                      throws java.io.IOException
        Deprecated.
        This method is not used in S3Mock.
        Copies an object, identified by bucket and name, to a another bucket and objectName.
        Parameters:
        sourceBucketName - name of the bucket to copy from.
        sourceObjectName - name of the object to copy.
        destinationBucketName - name of the destination bucket.
        destinationObjectName - name of the destination object.
        userMetadata - User metadata to store for destination object
        Returns:
        an CopyObjectResult or null if source couldn't be found.
        Throws:
        java.io.FileNotFoundException - no FileInputStream of the sourceFile can be created.
        java.io.IOException - If File can't be read.
      • copyS3ObjectEncrypted

        public CopyObjectResult copyS3ObjectEncrypted​(java.lang.String sourceBucketName,
                                                      java.lang.String sourceObjectName,
                                                      java.lang.String destinationBucketName,
                                                      java.lang.String destinationObjectName,
                                                      java.lang.String encryption,
                                                      java.lang.String kmsKeyId)
                                               throws java.io.IOException
        Copies an object to another bucket and encrypted object.
        Parameters:
        sourceBucketName - name of the bucket to copy from.
        sourceObjectName - name of the object to copy.
        destinationBucketName - name of the destination bucket.
        destinationObjectName - name of the destination object.
        encryption - The Encryption Type.
        kmsKeyId - The KMS encryption key id.
        Returns:
        an CopyObjectResult or null if source couldn't be found.
        Throws:
        java.io.FileNotFoundException - no FileInputStream of the sourceFile can be created.
        java.io.IOException - If File can't be read.
      • copyS3ObjectEncrypted

        public CopyObjectResult copyS3ObjectEncrypted​(java.lang.String sourceBucketName,
                                                      java.lang.String sourceObjectName,
                                                      java.lang.String destinationBucketName,
                                                      java.lang.String destinationObjectName,
                                                      java.lang.String encryption,
                                                      java.lang.String kmsKeyId,
                                                      java.util.Map<java.lang.String,​java.lang.String> userMetadata)
                                               throws java.io.IOException
        Copies an object to another bucket and encrypted object.
        Parameters:
        sourceBucketName - name of the bucket to copy from.
        sourceObjectName - name of the object to copy.
        destinationBucketName - name of the destination bucket.
        destinationObjectName - name of the destination object.
        encryption - The Encryption Type.
        kmsKeyId - The KMS encryption key id.
        userMetadata - User metadata to store for destination object
        Returns:
        an CopyObjectResult or null if source couldn't be found.
        Throws:
        java.io.FileNotFoundException - no FileInputStream of the sourceFile can be created.
        java.io.IOException - If File can't be read.
      • doesBucketExist

        public java.lang.Boolean doesBucketExist​(java.lang.String bucketName)
        Checks if the specified bucket exists. Amazon S3 buckets are named in a global namespace; use this method to determine if a specified bucket name already exists, and therefore can't be used to create a new bucket.
        Parameters:
        bucketName - Name of the bucket to check for existence
        Returns:
        true if Bucket exists
      • deleteObject

        public boolean deleteObject​(java.lang.String bucketName,
                                    java.lang.String objectName)
                             throws java.io.IOException
        Removes an object from a bucket.
        Parameters:
        bucketName - name of the bucket containing the object.
        objectName - name of the object to be deleted.
        Returns:
        true if deletion succeeded.
        Throws:
        java.io.IOException - if File could not be accessed.
      • deleteBucket

        public boolean deleteBucket​(java.lang.String bucketName)
                             throws java.io.IOException
        Deletes a Bucket and all of its contents.
        Parameters:
        bucketName - name of the bucket to be deleted.
        Returns:
        true if deletion succeeded.
        Throws:
        java.io.IOException - if bucket-file could not be accessed.
      • prepareMultipartUpload

        public MultipartUpload prepareMultipartUpload​(java.lang.String bucketName,
                                                      java.lang.String fileName,
                                                      java.lang.String contentType,
                                                      java.lang.String contentEncoding,
                                                      java.lang.String uploadId,
                                                      Owner owner,
                                                      Owner initiator,
                                                      java.util.Map<java.lang.String,​java.lang.String> userMetadata)
        Prepares everything to store files uploaded as multipart upload.
        Parameters:
        bucketName - in which to upload
        fileName - of the file to upload
        contentType - the content type
        contentEncoding - the content encoding
        uploadId - id of the upload
        owner - owner of the upload
        initiator - initiator of the upload
        userMetadata - custom metadata
        Returns:
        upload result
      • prepareMultipartUpload

        public MultipartUpload prepareMultipartUpload​(java.lang.String bucketName,
                                                      java.lang.String fileName,
                                                      java.lang.String contentType,
                                                      java.lang.String contentEncoding,
                                                      java.lang.String uploadId,
                                                      Owner owner,
                                                      Owner initiator)
        Prepares everything to store files uploaded as multipart upload.
        Parameters:
        bucketName - in which to upload
        fileName - of the file to upload
        contentType - the content type
        contentEncoding - the content encoding
        uploadId - id of the upload
        owner - owner of the upload
        initiator - initiator of the upload
        Returns:
        upload result
      • listMultipartUploads

        public java.util.Collection<MultipartUpload> listMultipartUploads()
        Lists the not-yet completed parts of an multipart upload.
        Returns:
        the list of not-yet completed multipart uploads.
      • abortMultipartUpload

        public void abortMultipartUpload​(java.lang.String bucketName,
                                         java.lang.String fileName,
                                         java.lang.String uploadId)
        Aborts the upload.
        Parameters:
        bucketName - to which was uploaded
        fileName - which was uploaded
        uploadId - of the upload
      • putPart

        public java.lang.String putPart​(java.lang.String bucketName,
                                        java.lang.String fileName,
                                        java.lang.String uploadId,
                                        java.lang.String partNumber,
                                        java.io.InputStream inputStream,
                                        boolean useV4ChunkedWithSigningFormat)
                                 throws java.io.IOException
        Uploads a part of a multipart upload.
        Parameters:
        bucketName - in which to upload
        fileName - of the file to upload
        uploadId - id of the upload
        partNumber - number of the part to store
        inputStream - file data to be stored
        useV4ChunkedWithSigningFormat - If true, V4-style signing is enabled.
        Returns:
        the md5 hash of this part
        Throws:
        java.io.IOException - if file could not be read to calculate digest
      • completeMultipartUpload

        public java.lang.String completeMultipartUpload​(java.lang.String bucketName,
                                                        java.lang.String fileName,
                                                        java.lang.String uploadId,
                                                        java.util.List<Part> parts)
        Finishes the upload.
        Parameters:
        bucketName - to which was uploaded.
        fileName - which was uploaded.
        uploadId - of the upload.
        parts - to concatenate.
        Returns:
        the etag of the complete file.
      • completeMultipartUpload

        public java.lang.String completeMultipartUpload​(java.lang.String bucketName,
                                                        java.lang.String fileName,
                                                        java.lang.String uploadId,
                                                        java.util.List<Part> parts,
                                                        java.lang.String encryption,
                                                        java.lang.String kmsKeyId)
        Completes an Multipart Upload for the given Id.
        Parameters:
        bucketName - in which to upload.
        fileName - of the file to upload.
        uploadId - id of the upload.
        parts - to concatenate.
        encryption - The Encryption Type.
        kmsKeyId - The KMS encryption key id.
        Returns:
        etag of the uploaded file.
      • getMultipartUploadParts

        public java.util.List<Part> getMultipartUploadParts​(java.lang.String bucketName,
                                                            java.lang.String fileName,
                                                            java.lang.String uploadId)
        Get all multipart upload parts.
        Parameters:
        bucketName - name of the bucket
        fileName - name of the file (object key)
        uploadId - upload identifier
        Returns:
        Array of Files
      • copyPart

        public java.lang.String copyPart​(java.lang.String bucket,
                                         java.lang.String key,
                                         int from,
                                         int to,
                                         java.lang.String partNumber,
                                         java.lang.String destinationBucket,
                                         java.lang.String destinationFilename,
                                         java.lang.String uploadId)
                                  throws java.io.IOException
        Copies the range, define by from/to, from the S3 Object, identified by the given key to given destination into the given bucket.
        Parameters:
        bucket - The source Bucket.
        key - Identifies the S3 Object.
        from - Byte range form.
        to - Byte range to.
        partNumber - The part to copy.
        destinationBucket - The Bucket the target file (will) reside in.
        destinationFilename - The target file.
        uploadId - id of the upload.
        Returns:
        etag of the uploaded file.
        Throws:
        java.io.IOException - When writing the file fails.