PHP Class S3

Show file Open project: tpyo/amazon-s3-php-class Class Usage Examples

Public Properties

Property Type Description
$defDelimiter string Default delimiter to be used, for example while getBucket().
$endpoint string AWS URI
$proxy null | array Proxy information
$sslCACert string SSL CA cert (only required if you are having problems with your system CA cert)
$sslCert string SSL client certfificate
$sslKey boolean SSL client key
$useExceptions boolean Use PHP exceptions?
$useSSL boolean Connect using SSL?
$useSSLValidation boolean Use SSL validation?
$useSSLVersion const Use SSL version

Public Methods

Method Description
__construct ( string $accessKey = null, string $secretKey = null, boolean $useSSL = false, string $endpoint = 's3.amazonaws.com' ) : void Constructor - if you're not using the class statically
__getSignature ( string $string ) : string Generate the auth string: "AWS AccessKey:Signature"
__getTime ( ) : integer Get the current time
copyObject ( string $srcBucket, string $srcUri, string $bucket, string $uri, constant $acl = self::ACL_PRIVATE, array $metaHeaders = [], array $requestHeaders = [], constant $storageClass = self::STORAGE_CLASS_STANDARD ) : mixed | false Copy an object
createDistribution ( string $bucket, boolean $enabled = true, array $cnames = [], string $comment = null, string $defaultRootObject = null, string $originAccessIdentity = null, array $trustedSigners = [] ) : array | false Create a CloudFront distribution
deleteBucket ( string $bucket ) : boolean Delete an empty bucket
deleteDistribution ( array $dist ) : boolean Delete a CloudFront distribution
deleteObject ( string $bucket, string $uri ) : boolean Delete an object
disableBucketLogging ( string $bucket ) : boolean Disable bucket logging
freeSigningKey ( ) : void Free signing key from memory, MUST be called if you are using setSigningKey()
getAccessControlPolicy ( string $bucket, string $uri = '' ) : mixed | false Get object or bucket Access Control Policy
getAuthenticatedURL ( string $bucket, string $uri, integer $lifetime, boolean $hostBucket = false, boolean $https = false ) : string Get a query string authenticated URL
getBucket ( string $bucket, string $prefix = null, string $marker = null, string $maxKeys = null, string $delimiter = null, boolean $returnCommonPrefixes = false ) : array | false Get contents for a bucket
getBucketLocation ( string $bucket ) : string | false Get a bucket's location
getBucketLogging ( string $bucket ) : array | false Get logging status for a bucket
getDistribution ( string $distributionId ) : array | false Get CloudFront distribution info
getDistributionInvalidationList ( string $distributionId ) : array List your invalidation batches for invalidateDistribution() in a CloudFront distribution
getHttpUploadPostParams ( string $bucket, string $uriPrefix = '', constant $acl = self::ACL_PRIVATE, integer $lifetime = 3600, integer $maxFileSize = 5242880, string $successRedirect = "201", array $amzHeaders = [], array $headers = [], boolean $flashVars = false ) : object Get upload POST parameters for form uploads
getObject ( string $bucket, string $uri, mixed $saveTo = false ) : mixed Get an object
getObjectInfo ( string $bucket, string $uri, boolean $returnInfo = true ) : mixed | false Get object information
getSignedCannedURL ( string $url, integer $lifetime ) : string Get a CloudFront canned policy URL
getSignedPolicyURL ( array $policy ) : string Get a CloudFront signed policy URL
hasAuth ( ) : boolean Check if AWS keys have been set
inputFile ( string $file, mixed $md5sum = true ) : array | false Create input info array for putObject()
inputResource ( string &$resource, integer $bufferSize = false, string $md5sum = '' ) : array | false Create input array info for putObject() with a resource
invalidateDistribution ( string $distributionId, array $paths ) : boolean Invalidate objects in a CloudFront distribution
listBuckets ( boolean $detailed = false ) : array | false Get a list of buckets
listDistributions ( ) : array Get a list of CloudFront distributions
listOriginAccessIdentities ( ) : array List CloudFront Origin Access Identities
putBucket ( string $bucket, constant $acl = self::ACL_PRIVATE, string $location = false ) : boolean Put a bucket
putObject ( mixed $input, string $bucket, string $uri, constant $acl = self::ACL_PRIVATE, array $metaHeaders = [], array $requestHeaders = [], constant $storageClass = self::STORAGE_CLASS_STANDARD, constant $serverSideEncryption = self::SSE_NONE ) : boolean Put an object
putObjectFile ( string $file, string $bucket, string $uri, constant $acl = self::ACL_PRIVATE, array $metaHeaders = [], string $contentType = null ) : boolean Put an object from a file (legacy function)
putObjectString ( string $string, string $bucket, string $uri, constant $acl = self::ACL_PRIVATE, array $metaHeaders = [], string $contentType = 'text/plain' ) : boolean Put an object from a string (legacy function)
setAccessControlPolicy ( string $bucket, string $uri = '', array $acp = [] ) : boolean Set object or bucket Access Control Policy
setAuth ( string $accessKey, string $secretKey ) : void Set AWS access key and secret key
setBucketLogging ( string $bucket, string $targetBucket, string $targetPrefix = null ) : boolean Set logging for a bucket
setBucketRedirect ( string $bucket = NULL, string $location = NULL ) : boolean Set up a bucket redirection
setEndpoint ( string $host ) : void Set the service endpoint
setExceptions ( boolean $enabled = true ) : void Set the error mode to exceptions
setProxy ( string $host, string $user = null, string $pass = null, constant $type = CURLPROXY_SOCKS5 ) : void Set proxy information
setSSL ( boolean $enabled, boolean $validate = true ) : void Set SSL on or off
setSSLAuth ( string $sslCert = null, string $sslKey = null, string $sslCACert = null ) : void Set SSL client certificates (experimental)
setSigningKey ( string $keyPairId, string $signingKey, boolean $isFile = true ) : boolean Set signing key
setTimeCorrectionOffset ( string $offset ) : void Set AWS time correction offset (use carefully)
updateDistribution ( array $dist ) : array | false Update a CloudFront distribution

Private Methods

Method Description
__getCloudFrontDistributionConfigXML ( string $bucket, boolean $enabled, string $comment, string $callerReference = '0', array $cnames = [], string $defaultRootObject = null, string $originAccessIdentity = null, array $trustedSigners = [] ) : string Get a DistributionConfig DOMDocument
__getCloudFrontInvalidationBatchXML ( array $paths, integer $callerReference = '0' ) : string Get a InvalidationBatch DOMDocument
__getCloudFrontResponse ( &$rest ) : object Grab CloudFront response
__getHash ( string $string ) : string Creates a HMAC-SHA1 hash
__getMIMEType ( &$file ) : string Get MIME type for file
__parseCloudFrontDistributionConfig ( &$node ) : array Parse a CloudFront distribution config
__triggerError ( string $message, string $file, integer $line, integer $code ) : void Internal error handler

Method Details

__construct() public method

Constructor - if you're not using the class statically
public __construct ( string $accessKey = null, string $secretKey = null, boolean $useSSL = false, string $endpoint = 's3.amazonaws.com' ) : void
$accessKey string Access key
$secretKey string Secret key
$useSSL boolean Enable SSL
$endpoint string Amazon URI
return void

__getSignature() public static method

Generate the auth string: "AWS AccessKey:Signature"
public static __getSignature ( string $string ) : string
$string string String to sign
return string

__getTime() public static method

Get the current time
public static __getTime ( ) : integer
return integer

copyObject() public static method

Copy an object
public static copyObject ( string $srcBucket, string $srcUri, string $bucket, string $uri, constant $acl = self::ACL_PRIVATE, array $metaHeaders = [], array $requestHeaders = [], constant $storageClass = self::STORAGE_CLASS_STANDARD ) : mixed | false
$srcBucket string Source bucket name
$srcUri string Source object URI
$bucket string Destination bucket name
$uri string Destination object URI
$acl constant ACL constant
$metaHeaders array Optional array of x-amz-meta-* headers
$requestHeaders array Optional array of request headers (content type, disposition, etc.)
$storageClass constant Storage class constant
return mixed | false | false

createDistribution() public static method

Create a CloudFront distribution
public static createDistribution ( string $bucket, boolean $enabled = true, array $cnames = [], string $comment = null, string $defaultRootObject = null, string $originAccessIdentity = null, array $trustedSigners = [] ) : array | false
$bucket string Bucket name
$enabled boolean Enabled (true/false)
$cnames array Array containing CNAME aliases
$comment string Use the bucket name as the hostname
$defaultRootObject string Default root object
$originAccessIdentity string Origin access identity
$trustedSigners array Array of trusted signers
return array | false | false

deleteBucket() public static method

Delete an empty bucket
public static deleteBucket ( string $bucket ) : boolean
$bucket string Bucket name
return boolean

deleteDistribution() public static method

Delete a CloudFront distribution
public static deleteDistribution ( array $dist ) : boolean
$dist array Distribution array info identical to output of getDistribution()
return boolean

deleteObject() public static method

Delete an object
public static deleteObject ( string $bucket, string $uri ) : boolean
$bucket string Bucket name
$uri string Object URI
return boolean

disableBucketLogging() public static method

Disable bucket logging
public static disableBucketLogging ( string $bucket ) : boolean
$bucket string Bucket name
return boolean

freeSigningKey() public static method

Free signing key from memory, MUST be called if you are using setSigningKey()
public static freeSigningKey ( ) : void
return void

getAccessControlPolicy() public static method

Get object or bucket Access Control Policy
public static getAccessControlPolicy ( string $bucket, string $uri = '' ) : mixed | false
$bucket string Bucket name
$uri string Object URI
return mixed | false | false

getAuthenticatedURL() public static method

Get a query string authenticated URL
public static getAuthenticatedURL ( string $bucket, string $uri, integer $lifetime, boolean $hostBucket = false, boolean $https = false ) : string
$bucket string Bucket name
$uri string Object URI
$lifetime integer Lifetime in seconds
$hostBucket boolean Use the bucket name as the hostname
$https boolean Use HTTPS ($hostBucket should be false for SSL verification)
return string

getBucket() public static method

If maxKeys is null this method will loop through truncated result sets
public static getBucket ( string $bucket, string $prefix = null, string $marker = null, string $maxKeys = null, string $delimiter = null, boolean $returnCommonPrefixes = false ) : array | false
$bucket string Bucket name
$prefix string Prefix
$marker string Marker (last file listed)
$maxKeys string Max keys (maximum number of keys to return)
$delimiter string Delimiter
$returnCommonPrefixes boolean Set to true to return CommonPrefixes
return array | false | false

getBucketLocation() public static method

Get a bucket's location
public static getBucketLocation ( string $bucket ) : string | false
$bucket string Bucket name
return string | false | false

getBucketLogging() public static method

This will return false if logging is not enabled. Note: To enable logging, you also need to grant write access to the log group
public static getBucketLogging ( string $bucket ) : array | false
$bucket string Bucket name
return array | false | false

getDistribution() public static method

Get CloudFront distribution info
public static getDistribution ( string $distributionId ) : array | false
$distributionId string Distribution ID from listDistributions()
return array | false | false

getDistributionInvalidationList() public static method

http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/ListInvalidation.html returned array looks like this: Array ( [I31TWB0CN9V6XD] => InProgress [IT3TFE31M0IHZ] => Completed [I12HK7MPO1UQDA] => Completed [I1IA7R6JKTC3L2] => Completed )
public static getDistributionInvalidationList ( string $distributionId ) : array
$distributionId string Distribution ID from listDistributions()
return array

getHttpUploadPostParams() public static method

Get upload POST parameters for form uploads
public static getHttpUploadPostParams ( string $bucket, string $uriPrefix = '', constant $acl = self::ACL_PRIVATE, integer $lifetime = 3600, integer $maxFileSize = 5242880, string $successRedirect = "201", array $amzHeaders = [], array $headers = [], boolean $flashVars = false ) : object
$bucket string Bucket name
$uriPrefix string Object URI prefix
$acl constant ACL constant
$lifetime integer Lifetime in seconds
$maxFileSize integer Maximum filesize in bytes (default 5MB)
$successRedirect string Redirect URL or 200 / 201 status code
$amzHeaders array Array of x-amz-meta-* headers
$headers array Array of request headers or content type as a string
$flashVars boolean Includes additional "Filename" variable posted by Flash
return object

getObject() public static method

Get an object
public static getObject ( string $bucket, string $uri, mixed $saveTo = false ) : mixed
$bucket string Bucket name
$uri string Object URI
$saveTo mixed Filename or resource to write to
return mixed

getObjectInfo() public static method

Get object information
public static getObjectInfo ( string $bucket, string $uri, boolean $returnInfo = true ) : mixed | false
$bucket string Bucket name
$uri string Object URI
$returnInfo boolean Return response information
return mixed | false | false

getSignedCannedURL() public static method

Get a CloudFront canned policy URL
public static getSignedCannedURL ( string $url, integer $lifetime ) : string
$url string URL to sign
$lifetime integer URL lifetime
return string

getSignedPolicyURL() public static method

Get a CloudFront signed policy URL
public static getSignedPolicyURL ( array $policy ) : string
$policy array Policy
return string

hasAuth() public static method

Check if AWS keys have been set
public static hasAuth ( ) : boolean
return boolean

inputFile() public static method

Create input info array for putObject()
public static inputFile ( string $file, mixed $md5sum = true ) : array | false
$file string Input file
$md5sum mixed Use MD5 hash (supply a string if you want to use your own)
return array | false | false

inputResource() public static method

Create input array info for putObject() with a resource
public static inputResource ( string &$resource, integer $bufferSize = false, string $md5sum = '' ) : array | false
$resource string Input resource to read from
$bufferSize integer Input byte size
$md5sum string MD5 hash to send (optional)
return array | false | false

invalidateDistribution() public static method

Thanks to Martin Lindkvist for S3::invalidateDistribution()
public static invalidateDistribution ( string $distributionId, array $paths ) : boolean
$distributionId string Distribution ID from listDistributions()
$paths array Array of object paths to invalidate
return boolean

listBuckets() public static method

Get a list of buckets
public static listBuckets ( boolean $detailed = false ) : array | false
$detailed boolean Returns detailed bucket list when true
return array | false | false

listDistributions() public static method

Get a list of CloudFront distributions
public static listDistributions ( ) : array
return array

listOriginAccessIdentities() public static method

List CloudFront Origin Access Identities
public static listOriginAccessIdentities ( ) : array
return array

putBucket() public static method

Put a bucket
public static putBucket ( string $bucket, constant $acl = self::ACL_PRIVATE, string $location = false ) : boolean
$bucket string Bucket name
$acl constant ACL flag
$location string Set as "EU" to create buckets hosted in Europe
return boolean

putObject() public static method

Put an object
public static putObject ( mixed $input, string $bucket, string $uri, constant $acl = self::ACL_PRIVATE, array $metaHeaders = [], array $requestHeaders = [], constant $storageClass = self::STORAGE_CLASS_STANDARD, constant $serverSideEncryption = self::SSE_NONE ) : boolean
$input mixed Input data
$bucket string Bucket name
$uri string Object URI
$acl constant ACL constant
$metaHeaders array Array of x-amz-meta-* headers
$requestHeaders array Array of request headers or content type as a string
$storageClass constant Storage class constant
$serverSideEncryption constant Server-side encryption
return boolean

putObjectFile() public static method

Put an object from a file (legacy function)
public static putObjectFile ( string $file, string $bucket, string $uri, constant $acl = self::ACL_PRIVATE, array $metaHeaders = [], string $contentType = null ) : boolean
$file string Input file path
$bucket string Bucket name
$uri string Object URI
$acl constant ACL constant
$metaHeaders array Array of x-amz-meta-* headers
$contentType string Content type
return boolean

putObjectString() public static method

Put an object from a string (legacy function)
public static putObjectString ( string $string, string $bucket, string $uri, constant $acl = self::ACL_PRIVATE, array $metaHeaders = [], string $contentType = 'text/plain' ) : boolean
$string string Input data
$bucket string Bucket name
$uri string Object URI
$acl constant ACL constant
$metaHeaders array Array of x-amz-meta-* headers
$contentType string Content type
return boolean

setAccessControlPolicy() public static method

Set object or bucket Access Control Policy
public static setAccessControlPolicy ( string $bucket, string $uri = '', array $acp = [] ) : boolean
$bucket string Bucket name
$uri string Object URI
$acp array Access Control Policy Data (same as the data returned from getAccessControlPolicy)
return boolean

setAuth() public static method

Set AWS access key and secret key
public static setAuth ( string $accessKey, string $secretKey ) : void
$accessKey string Access key
$secretKey string Secret key
return void

setBucketLogging() public static method

Set logging for a bucket
public static setBucketLogging ( string $bucket, string $targetBucket, string $targetPrefix = null ) : boolean
$bucket string Bucket name
$targetBucket string Target bucket (where logs are stored)
$targetPrefix string Log prefix (e,g; domain.com-)
return boolean

setBucketRedirect() public static method

Set up a bucket redirection
public static setBucketRedirect ( string $bucket = NULL, string $location = NULL ) : boolean
$bucket string Bucket name
$location string Target host name
return boolean

setEndpoint() public method

Set the service endpoint
public setEndpoint ( string $host ) : void
$host string Hostname
return void

setExceptions() public static method

Set the error mode to exceptions
public static setExceptions ( boolean $enabled = true ) : void
$enabled boolean Enable exceptions
return void

setProxy() public static method

Set proxy information
public static setProxy ( string $host, string $user = null, string $pass = null, constant $type = CURLPROXY_SOCKS5 ) : void
$host string Proxy hostname and port (localhost:1234)
$user string Proxy username
$pass string Proxy password
$type constant CURL proxy type
return void

setSSL() public static method

Set SSL on or off
public static setSSL ( boolean $enabled, boolean $validate = true ) : void
$enabled boolean SSL enabled
$validate boolean SSL certificate validation
return void

setSSLAuth() public static method

Set SSL client certificates (experimental)
public static setSSLAuth ( string $sslCert = null, string $sslKey = null, string $sslCACert = null ) : void
$sslCert string SSL client certificate
$sslKey string SSL client key
$sslCACert string SSL CA cert (only required if you are having problems with your system CA cert)
return void

setSigningKey() public static method

Set signing key
public static setSigningKey ( string $keyPairId, string $signingKey, boolean $isFile = true ) : boolean
$keyPairId string AWS Key Pair ID
$signingKey string Private Key
$isFile boolean Load private key from file, set to false to load string
return boolean

setTimeCorrectionOffset() public static method

This can be used when an inaccurate system time is generating invalid request signatures. It should only be used as a last resort when the system time cannot be changed.
public static setTimeCorrectionOffset ( string $offset ) : void
$offset string Time offset (set to zero to use AWS server time)
return void

updateDistribution() public static method

Update a CloudFront distribution
public static updateDistribution ( array $dist ) : array | false
$dist array Distribution array info identical to output of getDistribution()
return array | false | false

Property Details

$defDelimiter public static property

Default delimiter to be used, for example while getBucket().
public static string $defDelimiter
return string

$endpoint public static property

AWS URI
public static string $endpoint
return string

$proxy public static property

Proxy information
public static null|array $proxy
return null | array

$sslCACert public static property

SSL CA cert (only required if you are having problems with your system CA cert)
public static string $sslCACert
return string

$sslCert public static property

SSL client certfificate
public static string $sslCert
return string

$sslKey public static property

SSL client key
public static bool $sslKey
return boolean

$useExceptions public static property

Use PHP exceptions?
public static bool $useExceptions
return boolean

$useSSL public static property

Connect using SSL?
public static bool $useSSL
return boolean

$useSSLValidation public static property

Use SSL validation?
public static bool $useSSLValidation
return boolean

$useSSLVersion public static property

Use SSL version
public static const $useSSLVersion
return const