public static function setUpBeforeClass() { if (!class_exists('\\Aws\\S3\\S3Client')) { self::markTestSkipped('Amazon PHP SDK 2 not installed'); } parent::setUpBeforeClass(); self::$client = \Aws\S3\S3Client::factory(array('key' => self::$s3_key, 'secret' => self::$s3_secret, 'region' => self::$test_region)); try { self::$client->createBucket(array('Bucket' => self::$test_upload_bucket, 'LocationConstraint' => self::$test_region)); self::$client->waitUntilBucketExists(array('Bucket' => self::$test_upload_bucket)); } catch (\Aws\S3\Exception\BucketAlreadyOwnedByYouException $exception) { } }
public function __construct($params) { if (!isset($params['key']) || !isset($params['secret']) || !isset($params['bucket'])) { throw new \Exception("Access Key, Secret and Bucket have to be configured."); } $this->id = 'amazon::' . $params['key'] . md5($params['secret']); $this->bucket = $params['bucket']; $scheme = $params['use_ssl'] === 'false' ? 'http' : 'https'; $this->test = isset($params['test']); $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; $params['region'] = !isset($params['region']) || $params['region'] === '' ? 'eu-west-1' : $params['region']; $params['hostname'] = !isset($params['hostname']) || $params['hostname'] === '' ? 's3.amazonaws.com' : $params['hostname']; if (!isset($params['port']) || $params['port'] === '') { $params['port'] = $params['use_ssl'] === 'false' ? 80 : 443; } $base_url = $scheme . '://' . $params['hostname'] . ':' . $params['port'] . '/'; $this->connection = S3Client::factory(array('key' => $params['key'], 'secret' => $params['secret'], 'base_url' => $base_url, 'region' => $params['region'])); if (!$this->connection->isValidBucketName($this->bucket)) { throw new \Exception("The configured bucket name is invalid."); } if (!$this->connection->doesBucketExist($this->bucket)) { try { $result = $this->connection->createBucket(array('Bucket' => $this->bucket)); $this->connection->waitUntilBucketExists(array('Bucket' => $this->bucket, 'waiter.interval' => 1, 'waiter.max_attempts' => 15)); $this->testTimeout(); } catch (S3Exception $e) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); throw new \Exception("Creation of bucket failed."); } } if (!$this->file_exists('.')) { $result = $this->connection->putObject(array('Bucket' => $this->bucket, 'Key' => $this->cleanKey('.'), 'Body' => '', 'ContentType' => 'httpd/unix-directory', 'ContentLength' => 0)); $this->testTimeout(); } }
public function setUp() { $config = CM_Config::get(); $className = __CLASS__; $key = (string) $config->{$className}->key; $secret = (string) $config->{$className}->secret; $region = (string) $config->{$className}->region; if (empty($key) || empty($secret)) { $this->markTestSkipped('Missing `key` or `secret` config.'); } $this->_client = \Aws\S3\S3Client::factory(array('key' => $key, 'secret' => $secret)); $this->_client->getConfig()->set('curl.options', array('body_as_string' => true)); // https://github.com/aws/aws-sdk-php/issues/140#issuecomment-25117635 $this->_bucket = strtolower(str_replace('_', '-', 'test-' . __CLASS__ . uniqid())); $this->_filesystem = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_AwsS3($this->_client, $this->_bucket)); $this->_client->createBucket(array('Bucket' => $this->_bucket, 'LocationConstraint' => $region)); $this->_client->waitUntilBucketExists(array('Bucket' => $this->_bucket)); $this->_client->putBucketVersioning(array('Bucket' => $this->_bucket, 'Status' => 'Enabled')); $this->_restore = new CMService_AwsS3Versioning_Client($this->_client, $this->_bucket, new CM_OutputStream_Null()); }
/** * Create a presigned URL with a command object that has x-amz-* headers. * * @depends testGetObjectWithSaveAs */ public function testCreatePresignedUrlWithAcl() { $this->client->waitUntilBucketExists(array('Bucket' => $this->bucket)); $client = $this->client; $bucket = $this->bucket; $command = $client->getCommand('PutObject', array('Bucket' => $bucket, 'Key' => 'preput', 'ACL' => 'public-read', 'Content-Type' => 'plain/text', 'Body' => '')); $signedUrl = $command->createPresignedUrl('+10 minutes'); $ch = curl_init($signedUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: plain/text')); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, 'abc123'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); }
/** * Returns the connection * * @return S3Client connected client * @throws \Exception if connection could not be made */ public function getConnection() { if (!is_null($this->connection)) { return $this->connection; } $scheme = ($this->params['use_ssl'] === 'false') ? 'http' : 'https'; $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; $this->connection = S3Client::factory(array( 'key' => $this->params['key'], 'secret' => $this->params['secret'], 'base_url' => $base_url, 'region' => $this->params['region'] )); if (!$this->connection->isValidBucketName($this->bucket)) { throw new \Exception("The configured bucket name is invalid."); } if (!$this->connection->doesBucketExist($this->bucket)) { try { $this->connection->createBucket(array( 'Bucket' => $this->bucket )); $this->connection->waitUntilBucketExists(array( 'Bucket' => $this->bucket, 'waiter.interval' => 1, 'waiter.max_attempts' => 15 )); $this->testTimeout(); } catch (S3Exception $e) { \OCP\Util::logException('files_external', $e); throw new \Exception('Creation of bucket failed. '.$e->getMessage()); } } return $this->connection; }
/** * waitUntilExist * * @param array $policy * * @return boolean */ public function waitUntilExist(array $policy = array()) { $params = ['Bucket' => $this->name, 'Policy' => $policy]; return $this->client->waitUntilBucketExists($params); }
protected function verifyBucketExists(S3Client $s3Client) { if (!strlen($this->bucketName)) { throw new Exception("[S3Sync::verifyBucketExists] Unable to verify S3 bucket: bucket name not set"); } if (!$s3Client->doesBucketExist($this->bucketName)) { $result = $s3Client->createBucket(array('ACL' => 'private', 'Bucket' => $this->bucketName)); $s3Client->waitUntilBucketExists(array('Bucket' => $this->bucketName)); if (!$s3Client->doesBucketExist($this->bucketName)) { throw new Exception('[S3Sync::verifyBucketExists] Unable to create S3 bucket ' . $this->bucketName); } } return true; }