/** * @depends testPutObjectsWithUtf8Keys */ public function testCopiesObjects() { self::log("Copying the object"); $result = $this->client->copyObject(array('Bucket' => $this->bucket, 'Key' => 'copy-key', 'CopySource' => $this->bucket . '/' . self::TEST_KEY, 'MetadataDirective' => 'COPY', 'ServerSideEncryption' => 'AES256')); $this->assertNotEmpty($result['ETag']); $this->assertEquals('AES256', $result['ServerSideEncryption']); $this->assertNotEmpty($result['LastModified']); $this->client->waitUntil('object_exists', "{$this->bucket}/copy-key"); }
/** * Create a presigned URL with a command object that has x-amz-* headers. * * @depends testGetObjectWithSaveAs */ public function testCreatePresignedUrlWithAcl() { $this->client->waitUntil('BucketExists', 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); }
/** * @depends testPutAndListObjects */ public function testPreSignedUrlAllowsSpecialCharacters() { self::log('Uploading an object with a space in the key'); $this->client->waitUntil('bucket_exists', array('Bucket' => $this->bucket)); $key = 'foo baz bar!'; $this->client->putObject(array('Bucket' => $this->bucket, 'Key' => $key, 'Body' => 'hi')); $this->client->waitUntil('object_exists', array('Bucket' => $this->bucket, 'Key' => $key)); self::log('Creating an downloading using a pre-signed URL'); $extra = urlencode("attachment; filename=\"{$key}\""); $request = $this->client->get("{$this->bucket}/{$key}?response-content-disposition={$extra}"); $url = $this->client->createPresignedUrl($request, '+10 minutes'); self::log($url); $client = new Client(); $this->assertEquals('hi', file_get_contents($url)); $this->assertEquals('hi', $client->get($url)->send()->getBody(true)); }
protected function saveImage(array $file, $subdirectory = '') { $s3 = new S3Client(['version' => 'latest', 'region' => 'eu-west-1']); $subdirectory = trim($subdirectory, '/'); list($name, $extension) = explode('.', trim($file['name'], '/')); $filename = md5((string) rand()) . '.' . $extension; $directory = self::$public_image_directory . '/' . $subdirectory . '/'; $bucket_name = 'comp3013'; if (!$s3->doesBucketExist($bucket_name)) { echo 'Error, bucket didnt exist'; exit(0); } while ($s3->doesObjectExist($bucket_name, $directory . $filename)) { $filename = md5((string) rand()) . '.' . $extension; } $parameters = ['ContentType' => $file['type'], 'Bucket' => $bucket_name, 'Key' => $directory . $filename, 'SourceFile' => $file['tmp_name']]; print_r($parameters); $s3->putObject($parameters); $s3->waitUntil('ObjectExists', ['Bucket' => $bucket_name, 'Key' => $directory . $filename]); //exit(0); return 'http://comp3013.s3-website-eu-west-1.amazonaws.com/' . $directory . $filename; }
$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'); $finishedurl = $resultrendered['ObjectURL'];
/** * _uploadFile * * Uploads the file to the directory * * @param \Cake\ORM\Entity $entity Entity to upload from. * @param string $field Field to use. * @param array $options Options. * @return bool */ protected function _uploadFile($entity, $field, $options = []) { // creating the bucket if not exists $bucketName = $this->_getBucketName($this->_customerSite, $field); $this->_createBucket($bucketName); $_upload = $this->_uploads[$field]; $ext = pathinfo($_upload['name'], PATHINFO_EXTENSION); $fileKey = $this->_getS3FolderPath($entity, $field); // Upload an object by streaming the contents of a file $result = $this->_s3Client->putObject(array('Bucket' => $bucketName, 'Key' => $fileKey, 'SourceFile' => $_upload['tmp_name'], 'ContentLength' => $_upload['size'], 'ContentType' => $_upload['type'], 'ACL' => 'public-read', 'Metadata' => array('Author' => 'whiterabbitsuite.com'))); // We can poll the object until it is accessible $this->_s3Client->waitUntil('ObjectExists', array('Bucket' => $bucketName, 'Key' => $fileKey)); //TODO: gestire eccezioni return true; }