/** * Delete a CloudFront distribution * * @param array $dist Distribution array info identical to output of getDistribution() * @return boolean */ public static function deleteDistribution($dist) { if (!extension_loaded('openssl')) { self::__triggerError(sprintf("S3::deleteDistribution({$dist['id']}): %s", "CloudFront functionality requires SSL"), __FILE__, __LINE__); return false; } $useSSL = self::$useSSL; self::$useSSL = true; // CloudFront requires SSL $rest = new S3Request('DELETE', '', '2008-06-30/distribution/' . $dist['id'], 'cloudfront.amazonaws.com'); $rest->setHeader('If-Match', $dist['hash']); $rest = self::__getCloudFrontResponse($rest); self::$useSSL = $useSSL; if ($rest->error === false && $rest->code !== 204) { $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); } if ($rest->error !== false) { self::__triggerError(sprintf("S3::deleteDistribution({$dist['id']}): [%s] %s", $rest->error['code'], $rest->error['message']), __FILE__, __LINE__); return false; } return true; }
/** * Delete a CloudFront distribution * * @param array $dist Distribution array info identical to output of getDistribution() * @return boolean */ public static function deleteDistribution($dist) { self::$useSSL = true; // CloudFront requires SSL $rest = new S3Request('DELETE', '', '2008-06-30/distribution/' . $dist['id'], 'cloudfront.amazonaws.com'); $rest->setHeader('If-Match', $dist['hash']); $rest = self::__getCloudFrontResponse($rest); if ($rest->error === false && $rest->code !== 204) { $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); } if ($rest->error !== false) { trigger_error(sprintf("S3::deleteDistribution({$dist['id']}): [%s] %s", $rest->error['code'], $rest->error['message']), E_USER_WARNING); return false; } return true; }
public static function copyObject($srcBucket, $srcUri, $bucket, $uri, $acl = self::ACL_PUBLIC_READ, $metaHeaders = array(), $requestHeaders = array()) { $rest = new S3Request('PUT', $bucket, $uri); $rest->setHeader('Content-Length', 0); foreach ($requestHeaders as $h => $v) { $rest->setHeader($h, $v); } foreach ($metaHeaders as $h => $v) { $rest->setAmzHeader('x-amz-meta-' . $h, $v); } $rest->setAmzHeader('x-amz-acl', $acl)->setAmzHeader('x-amz-copy-source', sprintf('/%s/%s', $srcBucket, $srcUri)); if (sizeof($requestHeaders) > 0 || sizeof($metaHeaders) > 0) { $rest->setAmzHeader('x-amz-metadata-directive', 'REPLACE'); } $rest = $rest->getResponse(); if ($rest->error !== false || $rest->code !== 200) { throw new Exception(sprintf("S3::copyObject(%s, %s, %s, %s): [%s] %s", $srcBucket, $srcUri, $bucket, $uri, $rest->code, 'Unexpected HTTP status')); } return isset($rest->body->LastModified, $rest->body->ETag) ? array('time' => date('Y-m-d H:i:s', strtotime((string) $rest->body->LastModified)), 'hash' => substr((string) $rest->body->ETag, 1, -1)) : false; }
/** * Invalidates files in a CloudFront distribution * * @param string $distributionId Distribution ID from listDistributions() * @param string $path Path to file to be invalidated * @return boolean */ public static function invalidate($distributionId, $path) { self::$useSSL = true; // CloudFront requires SSL $rest = new S3Request('POST', '', '2010-08-01/distribution/' . $distributionId . '/invalidation', 'cloudfront.amazonaws.com'); $rest->data = self::__getCloudFrontInvalidationBatchXML($path); $rest->size = strlen($rest->data); $rest->setHeader('Content-Type', 'text/xml'); $rest = self::__getCloudFrontResponse($rest); if ($rest->error === false && $rest->code !== 201) { $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); } if ($rest->error !== false) { trigger_error(sprintf("S3::invalidate(): [%s] %s", $rest->error['code'], $rest->error['message']), E_USER_WARNING); return false; } return true; }
/** * Creates invalidation bath * * @static * @param integer $distributionId * @param array $paths * @return array|bool */ public static function createInvalidation($distributionId, $paths) { self::$use_ssl = true; // CloudFront requires SSL $rest = new S3Request('POST', '', '2010-11-01/distribution/' . $distributionId . '/invalidation', 'cloudfront.amazonaws.com'); $rest->data = self::__getCloudFrontInvalidationBath($paths); $rest->size = strlen($rest->data); $rest->setHeader('Content-Type', 'application/xml'); $rest = self::__getCloudFrontResponse($rest); if ($rest->error === false && $rest->code !== 201) { $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); } if ($rest->error !== false) { trigger_error(sprintf("S3::createInvalidation(%d, '%s'): [%s] %s", $distributionId, implode(', ', $paths), $rest->error['code'], $rest->error['message']), E_USER_WARNING); return false; } elseif ($rest->body instanceof SimpleXMLElement) { return self::__parseCloudFrontInvalidation($rest->body); } return false; }
/** * Put an object * * @param mixed $input Input data * @param string $bucket Bucket name * @param string $uri Object URI * @param constant $acl ACL constant * @param array $metaHeaders Array of x-amz-meta-* headers * @param string $contentType Content type * @return boolean */ public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = null) { if ($input == false) { return false; } $rest = new S3Request('PUT', $bucket, $uri); if (is_string($input)) { $input = array('data' => $input, 'size' => strlen($input), 'md5sum' => base64_encode(md5($input, true))); } // Data if (isset($input['fp'])) { $rest->fp =& $input['fp']; } elseif (isset($input['file'])) { $rest->fp = @fopen($input['file'], 'rb'); } elseif (isset($input['data'])) { $rest->data = $input['data']; } // Content-Length (required) if (isset($input['size']) && $input['size'] > 0) { $rest->size = $input['size']; } else { if (isset($input['file'])) { $rest->size = filesize($input['file']); } elseif (isset($input['data'])) { $rest->size = strlen($input['data']); } } // Content-Type if ($contentType !== null) { $input['type'] = $contentType; } elseif (!isset($input['type']) && isset($input['file'])) { $input['type'] = self::__getMimeType($input['file']); } else { $input['type'] = 'application/octet-stream'; } // We need to post with the content-length and content-type, MD5 is optional if ($rest->size > 0 && ($rest->fp !== false || $rest->data !== false)) { $rest->setHeader('Content-Type', $input['type']); if (isset($input['md5sum'])) { $rest->setHeader('Content-MD5', $input['md5sum']); } $rest->setAmzHeader('x-amz-acl', $acl); foreach ($metaHeaders as $h => $v) { $rest->setAmzHeader('x-amz-meta-' . $h, $v); } $rest->getResponse(); } else { $rest->response->error = array('code' => 0, 'message' => 'Missing input parameters'); } if ($rest->response->error === false && $rest->response->code !== 200) { $rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status'); } if ($rest->response->error !== false) { trigger_error(sprintf("S3::putObject(): [%s] %s", $rest->response->error['code'], $rest->response->error['message']), E_USER_WARNING); return false; } return true; }
/** * Set object or bucket Access Control Policy * * @param string $bucket Bucket name * @param string $uri Object URI * @param array $acp Access Control Policy Data (same as the data returned from getAccessControlPolicy) * @return boolean */ public static function setAccessControlPolicy($bucket, $uri = '', $acp = array()) { $dom = new DOMDocument(); $dom->formatOutput = true; $accessControlPolicy = $dom->createElement('AccessControlPolicy'); $accessControlList = $dom->createElement('AccessControlList'); // It seems the owner has to be passed along too $owner = $dom->createElement('Owner'); $owner->appendChild($dom->createElement('ID', $acp['owner']['id'])); $owner->appendChild($dom->createElement('DisplayName', $acp['owner']['name'])); $accessControlPolicy->appendChild($owner); foreach ($acp['acl'] as $g) { $grant = $dom->createElement('Grant'); $grantee = $dom->createElement('Grantee'); $grantee->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); if (isset($g['id'])) { // CanonicalUser (DisplayName is omitted) $grantee->setAttribute('xsi:type', 'CanonicalUser'); $grantee->appendChild($dom->createElement('ID', $g['id'])); } elseif (isset($g['email'])) { // AmazonCustomerByEmail $grantee->setAttribute('xsi:type', 'AmazonCustomerByEmail'); $grantee->appendChild($dom->createElement('EmailAddress', $g['email'])); } elseif ($g['type'] == 'Group') { // Group $grantee->setAttribute('xsi:type', 'Group'); $grantee->appendChild($dom->createElement('URI', $g['uri'])); } $grant->appendChild($grantee); $grant->appendChild($dom->createElement('Permission', $g['permission'])); $accessControlList->appendChild($grant); } $accessControlPolicy->appendChild($accessControlList); $dom->appendChild($accessControlPolicy); $rest = new S3Request('PUT', $bucket, $uri); $rest->setParameter('acl', null); $rest->data = $dom->saveXML(); $rest->size = strlen($rest->data); $rest->setHeader('Content-Type', 'application/xml'); $rest = $rest->getResponse(); if ($rest->error === false && $rest->code !== 200) { $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); } if ($rest->error !== false) { trigger_error(sprintf("S3::setAccessControlPolicy({$bucket}, {$uri}): [%s] %s", $rest->error['code'], $rest->error['message']), E_USER_WARNING); return false; } return true; }
/** * Puts a file or data to the storage system * * @param $input * @param $bucket * @param $uri * @param $acl * @param $metaHeaders * @param $requestHeaders * @return unknown_type */ public function put($input, $bucket, $uri, $acl = StorageManager::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array()) { if ($input == false) return false; $rest = new S3Request($this->id,$this->secret,true,'PUT', $bucket, $uri); if (is_string($input)) $input = array( 'data' => $input, 'size' => strlen($input), 'md5sum' => base64_encode(md5($input, true)) ); // Data if (isset($input['fp'])) $rest->fp =& $input['fp']; elseif (isset($input['file'])) $rest->fp = @fopen($input['file'], 'rb'); elseif (isset($input['data'])) $rest->data = $input['data']; // Content-Length (required) if (isset($input['size']) && $input['size'] > -1) $rest->size = $input['size']; else { if (isset($input['file'])) $rest->size = filesize($input['file']); elseif (isset($input['data'])) $rest->size = strlen($input['data']); } // Custom request headers (Content-Type, Content-Disposition, Content-Encoding) if (is_array($requestHeaders)) foreach ($requestHeaders as $h => $v) $rest->setHeader($h, $v); elseif (is_string($requestHeaders)) // Support for legacy contentType parameter $input['type'] = $requestHeaders; // Content-Type if (!isset($input['type'])) { if (isset($requestHeaders['Content-Type'])) $input['type'] =& $requestHeaders['Content-Type']; elseif (isset($input['file'])) $input['type'] = $this->getMimeType($input['file']); else $input['type'] = 'application/octet-stream'; } // We need to post with Content-Length and Content-Type, MD5 is optional if ($rest->size > 0 && ($rest->fp !== false || $rest->data !== false)) { $rest->setHeader('Content-Type', $input['type']); if (isset($input['md5sum'])) $rest->setHeader('Content-MD5', $input['md5sum']); $rest->setAmzHeader('x-amz-acl', $acl); foreach ($metaHeaders as $h => $v) $rest->setAmzHeader('x-amz-meta-'.$h, $v); $rest->getResponse(); } else $rest->response->error = array('code' => 0, 'message' => 'Missing input parameters'); if ($rest->response->error === false && $rest->response->code !== 200) $rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status'); if ($rest->response->error !== false) { print_r($rest->response); throw new AWSException(sprintf("S3::putObject(): [%s] %s", $rest->response->error['code'], $rest->response->error['message'])); } return true; }