Example #1
0
 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) {
     }
 }
 /**
  * Create an Amazon S3 bucket
  *
  * @expectedException \Aws\S3\Exception\BucketAlreadyExistsException
  * @example Aws\S3\S3Client::createBucket
  */
 public function no_testBucketAlreadyExists()
 {
     //$client = $this->client;
     //$client->setRegion('us-east-1');
     // @begin
     $this->client->createBucket(array('Bucket' => 'mybucket'));
 }
Example #3
0
 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();
     }
 }
Example #4
0
 /**
  * Attempt to build a bucket (if it doesn't already exist).
  *
  * @param string $bucketName
  */
 protected function buildBucket($bucketName)
 {
     if (!$this->s3Client->doesBucketExist($bucketName, true)) {
         $this->s3Client->createBucket(['ACL' => $this->attachedFile->ACL, 'Bucket' => $bucketName, 'LocationConstraint' => $this->attachedFile->region]);
     }
     $this->bucketExists = true;
 }
Example #5
0
 /**
  * create
  *
  * @param array $params
  *
  * @return boolean
  */
 public function create(array $params = array())
 {
     if ($this->exist()) {
         return true;
     }
     $params['Bucket'] = $this->name;
     return $this->client->createBucket($params);
 }
Example #6
0
 public function testCreateBucket()
 {
     //start create S3 client
     $client = new S3Client(['credentials' => ['key' => 'AKIAIOSFODNN7EXAMPLE', 'secret' => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'], 'region' => $this->bucket_region, 'version' => 'latest', 'endpoint' => $this->end_point, 'scheme' => 'http']);
     //end create S3 client
     //start create bucket
     $client->createBucket(['ACL' => 'private', 'Bucket' => $this->bucket_name, 'CreateBucketConfiguration' => ['LocationConstraint' => $this->bucket_region]]);
     //end create bucket
     return $client;
 }
Example #7
0
 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());
 }
Example #8
0
 /**
  * @depends testHeadBucket
  */
 public function testPutBucketLocation()
 {
     $this->log(__METHOD__);
     $bucketName = self::getResourcePrefix() . '-s3eutest';
     try {
         $this->client->headBucket(array('Bucket' => $bucketName));
     } catch (\Exception $e) {
         $this->client->createBucket(array('Bucket' => $bucketName, 'LocationConstraint' => 'EU'));
     }
     $this->client->waitUntil('bucket_exists', array('Bucket' => $bucketName));
     $result = $this->client->getBucketLocation(array('Bucket' => $bucketName));
     $this->assertEquals('EU', $result['Location']);
     $this->client->deleteBucket(array('Bucket' => $bucketName));
 }
Example #9
0
 /**
  * @param array $properties
  * @param array $metadata
  *
  * @throws BadRequestException
  * @throws DfException
  * @internal param array $properties
  * @return array
  */
 public function createContainer($properties, $metadata = [])
 {
     $name = ArrayUtils::get($properties, 'name', ArrayUtils::get($properties, 'path'));
     if (empty($name)) {
         throw new BadRequestException('No name found for container in create request.');
     }
     try {
         $this->checkConnection();
         $this->blobConn->createBucket(['Bucket' => $name]);
         return ['name' => $name, 'path' => $name];
     } catch (\Exception $ex) {
         throw new DfException("Failed to create container '{$name}': " . $ex->getMessage());
     }
 }
Example #10
0
	/**
	 * 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;
	}
Example #11
0
 /**
  * Creates a new bucket.
  *
  * @param string $bucket
  * @param array  $params
  *
  * @return mixed
  */
 public function createBucket($bucket, array $params = [])
 {
     $params['Bucket'] = $bucket;
     return $this->instance->createBucket($params);
 }
Example #12
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
use Illuminate\Filesystem\FilesystemAdapter;
//start define settings
$end_point = 'http://webapp.dev:4569';
$bucket_region = 'us-east-1';
$bucket_name = 'default1bucket';
$root_folder_in_bucket = 'news';
$file_path = 'test.jpg';
//end define settings
//start create S3 client
$s3_client = new S3Client(['credentials' => ['key' => 'AKIAIOSFODNN7EXAMPLE', 'secret' => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'], 'region' => $bucket_region, 'version' => 'latest', 'endpoint' => $end_point, 'scheme' => 'http']);
//end create S3 client
//start create bucket
$s3_client->createBucket(['ACL' => 'private', 'Bucket' => $bucket_name, 'CreateBucketConfiguration' => ['LocationConstraint' => $bucket_region]]);
//end create bucket
//start create laravel filesystem adapter
$cloud_adapter = new AwsS3Adapter($s3_client, $bucket_name, $root_folder_in_bucket);
$filesystem = new Filesystem($cloud_adapter);
$disk = new FilesystemAdapter($filesystem);
//end create laravel filesystem adapter
//start put file on disk
if (!$disk->exists($file_path)) {
    $disk->put($file_path, file_get_contents('../resources/assets/img/test.jpg'));
}
//start put file on disk
print "<img src='" . $disk->url($file_path) . "'/>";
Example #13
0
 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;
 }
echo $_POST['phone'];
$uploaddir = '/tmp/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
$s3 = new Aws\S3\S3Client(['version' => 'latest', 'region' => 'us-east-1']);
$bucket = uniqid("php-ars-test-bucket-", false);
# AWS PHP SDK version 3 create bucket
$result = $s3->createBucket(['ACL' => 'public-read', 'Bucket' => $bucket]);
$s3->waitUntil('BucketExists', array('Bucket' => $bucket));
# PHP version 3
$result = $s3->putObject(['ACL' => 'public-read', 'Bucket' => $bucket, 'Key' => "Hello" . $uploadfile, 'ContentType' => $_FILES['userfile']['tmp_name'], 'Body' => fopen($uploadfile, 'r+')]);
$url = $result['ObjectURL'];
echo $url;
$result = $s3->getObject(array('Bucket' => $bucket, 'Key' => "Hello" . $uploadfile, 'ContentType' => $_FILES['userfile']['tmp_name'], 'SaveAs' => '/tmp/originalimage.jpg'));
$image = new Imagick(glob('/tmp/originalimage.jpg'));
$image->oilPaintImage(2);
//Oilpaint image
$image->setImageFormat("jpg");
$image->writeImages('/tmp/modifiedimage.jpg', true);
$modifiedbucket = uniqid("modified-image-", false);
$result = $s3->createBucket(['ACL' => 'public-read', 'Bucket' => $modifiedbucket]);
$resultrendered = $s3->putObject(['ACL' => 'public-read', 'Bucket' => $modifiedbucket, 'Key' => "Hello" . $uploadfile, 'SourceFile' => "/tmp/modifiedimage.jpg", 'ContentType' => $_FILES['userfile']['tmp_name'], 'Body' => fopen("/tmp/modifiedimage.jpg", 'r+')]);
unlink('/tmp/modifiedimage.jpg');
 /**
  * _createBucket
  * Create the bucket, if not exists, for Amazon S3
  *
  * @param string $bucketName name of the bucket.
  * @return void
  */
 protected function _createBucket($bucketName)
 {
     //TODO: migliorare con un ritorno di qualcosa...
     if (!$this->_s3Client->doesBucketExist($bucketName)) {
         $this->_s3Client->createBucket(array('Bucket' => $bucketName));
     }
     // Poll the bucket until it is accessible
     $this->_s3Client->waitUntil('BucketExists', array('Bucket' => $bucketName));
 }