\Omega\Filesystem\Adapter\Amazon AbstractAmazonS3

Abstract class for implementing an Amazon S3 adapter.

This class serves as a base implementation for an Amazon S3 filesystem adapter. It provides core methods to interact with Amazon S3, such as reading, writing, deleting, and renaming files. It also supports metadata management, size calculation, mime type detection, and listing keys. Specific functionality, such as the S3 client creation, is left to the implementing classes.

Summary

Methods
Properties
Constants
__construct
setMetadata
getMetadata
rename
keys
delete
read
write
exists
mtime
size
listKeys
isDirectory
mimeType
No public properties found
No public constants found
getOptions
computePath
computeKey
guessContentType
ensureBucketExists
createClient
service
bucket
options
bucketExists
content
detectContentType
No protected constants found
No private methods found
No private properties found
No private constants found

Properties

$service

$service : object

Type

object — The S3 service client

$bucket

$bucket : string

Type

string — The name of the S3 bucket

$options

$options : array

Type

array<string|int, mixed> — Options for the S3 adapter

$bucketExists

$bucketExists : bool

Type

bool — Indicates whether the bucket exists

$content

$content : array

Type

array<string|int, mixed> — Metadata content

$detectContentType

$detectContentType : bool

Type

bool — Flag for detecting content type

Methods

__construct()

__construct(array  $config) : mixed

Constructor.

Initializes the adapter with configuration details, including the bucket name, credentials, and optional settings. Throws an exception if the required config parameters are missing.

Parameters

array $config

The configuration array with bucket, key, secret, and other options.

Throws

\LogicException

If the configuration lacks required parameters.

Returns

mixed —

setMetadata()

setMetadata(string  $key, array  $content) : void

Sets metadata for the specified file (or key).

Parameters

string $key

The file key for which the metadata is being set.

array $content

An associative array containing the metadata to set.

Returns

void —

getMetadata()

getMetadata(string  $key) : array

Retrieves metadata for the specified file (or key).

Parameters

string $key

The file key for which metadata is being retrieved.

Returns

array —

An associative array containing the metadata of the file.

rename()

rename(string  $sourceKey, string  $targetKey) : bool

Renames a file.

Parameters

string $sourceKey

The current key or path of the file.

string $targetKey

The new key or path for the file.

Returns

bool —

True on success, false on failure.

keys()

keys() : array

Returns an array of all keys (files and directories).

Returns

array —

The list of keys.

delete()

delete(string  $key) : bool

Deletes the file.

Parameters

string $key

The key or path of the file to delete.

Returns

bool —

True on success, false on failure.

read()

read(string  $key) : string|bool

Reads the content of the file.

Parameters

string $key

The key or path of the file to read.

Returns

string|bool —

The content of the file or false if it cannot be read.

write()

write(string  $key, string  $content) : int|bool

Writes the given content into the file.

Parameters

string $key

The key or path of the file to write.

string $content

The content to write into the file.

Returns

int|bool —

The number of bytes written or false on failure.

exists()

exists(string  $key) : bool

Indicates whether the file exists.

Parameters

string $key

The key or path of the file to check.

Returns

bool —

True if the file exists, false otherwise.

mtime()

mtime(string  $key) : int|bool

Returns the last modified time.

Parameters

string $key

The key or path of the file.

Returns

int|bool —

The last modified time as a UNIX timestamp, or false on failure.

size()

size(string  $key) : int|false

Retrieves the size of the specified file (key).

Parameters

string $key

The file key for which the size is being retrieved.

Returns

int|false —

The size of the file in bytes, or false if it cannot be determined.

listKeys()

listKeys(string  $prefix = '') : array

Lists keys that begin with the given prefix.

Parameters

string $prefix

The prefix to filter the keys (optional, defaults to an empty string).

Returns

array —

An array of keys starting with the specified prefix.

isDirectory()

isDirectory(string  $key) : bool

Check if the key represents a directory.

Parameters

string $key

The key or path to check.

Returns

bool —

True if the key is a directory, false otherwise.

mimeType()

mimeType(string  $key) : string|false

Retrieves the MIME type of the specified file (key).

Parameters

string $key

The file key for which the MIME type is being retrieved.

Returns

string|false —

The MIME type of the file, or false if it cannot be determined.

getOptions()

getOptions(string  $key, array  $options = []) : array

Combines and merges global options, key-specific options, and metadata.

Parameters

string $key

The file key.

array $options

Additional options to be merged.

Returns

array —

The merged options.

computePath()

computePath(string  $key) : string

Computes the full path for the specified key.

Parameters

string $key

The file key.

Returns

string —

The computed path.

computeKey()

computeKey(string  $path) : string

Computes the key from a given path.

Parameters

string $path

The full path.

Returns

string —

The key derived from the path.

guessContentType()

guessContentType(string  $content) : string

Guesses the content type of a file or resource.

Parameters

string $content

The content or resource to guess the mime type for.

Returns

string —

The mime type.

ensureBucketExists()

ensureBucketExists() : bool

Ensures the specified bucket exists.

If the bucket does not exist and the create option is set to true, it will try to create the bucket. Throws a RuntimeException if the bucket does not exist and the create option is false.

Throws

\RuntimeException

if the bucket does not exist or could not be created.

Returns

bool —

True if the bucket exists or was successfully created.

createClient()

createClient(array  $config) : object

Creates a new SimpleS3Client instance based on the provided configuration.

Parameters

array $config

Configuration options for the S3 client.

Returns

object —

An instance of SimpleS3Client.