public function testCanCreateNewStateByInitiatingMultipartUpload()
 {
     $acl = new Acp(new Grantee('123'));
     $acl->addGrant(new Grant(new Grantee('123'), Permission::READ));
     $client = $this->getServiceBuilder()->get('s3');
     $mock = $this->setMockResponse($client, array('s3/initiate_multipart_upload'));
     $transfer = UploadBuilder::newInstance()->setBucket('foo')->setKey('bar')->setClient($client)->setSource(EntityBody::factory())->setHeaders(array('Foo' => 'Bar'))->setAcp($acl)->calculateMd5(true)->build();
     $requests = $mock->getReceivedRequests();
     $this->assertEquals(1, count($requests));
     $this->assertEquals('/bar?uploads', $requests[0]->getResource());
     $this->assertEquals('Bar', (string) $requests[0]->getHeader('Foo'));
     $this->assertTrue($requests[0]->hasHeader('x-amz-grant-read'));
 }
Пример #2
0
 /**
  * @depends testPutAndListObjects
  */
 public function testGetObjectAcl()
 {
     self::log("Getting the object's ACL");
     $model = $this->client->getObjectAcl(array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY));
     $data = array();
     foreach (Acp::fromArray($model->toArray()) as $grant) {
         $grantee = $grant->getGrantee();
         $data[$grantee->getGroupUri()] = array($grantee->getType(), $grant->getPermission());
     }
     $this->assertEquals(2, count($data));
     $this->assertArrayHasKey('http://acs.amazonaws.com/groups/global/AllUsers', $data);
     $this->assertArrayHasKey('http://acs.amazonaws.com/groups/global/AuthenticatedUsers', $data);
     $this->assertEquals(array('Group', 'READ_ACP'), $data['http://acs.amazonaws.com/groups/global/AllUsers']);
     $this->assertEquals(array('Group', 'READ'), $data['http://acs.amazonaws.com/groups/global/AuthenticatedUsers']);
 }
Пример #3
0
 public function testUploadFile()
 {
     $this->migrate();
     $this->login();
     // Preparation: Create an image entry.
     $image = $this->app->createModel('image', array('position' => 0));
     R::store($image);
     $this->assertNotEmpty($image->getID());
     // upload to aws s3
     $filename = 'original.jpg';
     $filepath = realpath(__DIR__ . '/' . $filename);
     $_SERVER['REQUEST_METHOD'] = "POST";
     $params = array('image_id' => $image->getID(), 'data' => '{ "id": ' . $image->getID() . ', "type": "uploadOnce", "settingName": "multiple_many_types" }', 'settingName' => 'multiple_many_types', 'uploadOnce' => 1);
     $size = filesize($filepath);
     $_FILES = array('file-0' => array('name' => $filename, 'type' => 'image/jpeg', 'tmp_name' => $filepath, 'error' => 0, 'size' => $size));
     ob_start();
     $this->app->runControllerFromRawUrl('admin/cRUDImages/uploadImage', $params);
     $output = ob_get_clean();
     echo "\nOutput of uploadImage:\n";
     echo $output;
     $output_r = json_decode($output);
     print_r($output_r);
     $this->assertFalse(property_exists($output_r[0], 'error'));
     // See if file uploaded successfully.
     echo "\nFind image with id " . $image->getID();
     $imageBean = R::findOne('image', 'id = ?', array($image->getID()));
     $this->assertNotEmpty($imageBean);
     echo "\n...Found!";
     $data = json_decode($imageBean['multiple_many_types']);
     echo "\nUploaded images:\n";
     print_r($data);
     $this->assertEquals(1, count($data));
     // Uploaded data must contain 'images/Image/1/original-smartphone'
     $this->assertNotEquals(-1, strpos(SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone), 'public/images/Image/' . $image->getID() . '/original-smartphone'));
     // Now see if file does exist in Amazon S3 repository.
     $amazonS3Config = $this->app->config('amazonS3');
     $client = \Aws\S3\S3Client::factory($amazonS3Config['settings']);
     $result = $client->getObject(array('Bucket' => $amazonS3Config['bucket'], 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone)));
     $this->assertNotEmpty($result['Body']);
     //        $this->app->getLogger()->log("result get object : " . print_r($result, true));
     // TEST ACL
     $resultAcl = $client->getObjectAcl(array('Bucket' => $amazonS3Config['bucket'], 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone)));
     //        $this->app->getLogger()->log("result get object ACL : " . print_r($resultAcl, true));
     $this->assertNotEmpty($resultAcl["Grants"]);
     $dataAcl = array();
     foreach (\Aws\S3\Model\Acp::fromArray($resultAcl->toArray()) as $grant) {
         $grantee = $grant->getGrantee();
         $dataAcl[$grantee->getGroupUri()] = array($grantee->getType(), $grant->getPermission());
     }
     $this->assertEquals(2, count($dataAcl));
     $this->assertArrayHasKey('http://acs.amazonaws.com/groups/global/AllUsers', $dataAcl);
     $this->assertEquals(array('Group', 'READ'), $dataAcl['http://acs.amazonaws.com/groups/global/AllUsers']);
     // ---- end of test ACL ----
     // Local file must have been deleted.
     $filepath = \Skully\App\Helpers\FileHelper::replaceSeparators($this->app->getTheme()->getBasePath() . $data[0]->smartphone);
     echo "\nChecking if file {$filepath} is deleted...";
     $this->assertFalse(file_exists($filepath));
     echo "\nYep";
     // Delete Models
     try {
         R::trash($imageBean);
     } catch (\Exception $e) {
         echo 'failed to delete image bean. reason: ' . $e->getMessage();
     }
     //        // Cleanup by removing files in bucket.
     //        $client->deleteObject(array(
     //            'Bucket' => $amazonS3Config['bucket'],
     //            'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone))
     //        );
     //
     //        $client->deleteObject(array(
     //            'Bucket' => $amazonS3Config['bucket'],
     //            'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->desktop))
     //        );
     // Check existence of deleted files on s3 server
     $result = $client->doesObjectExist($amazonS3Config['bucket'], SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone));
     $this->assertFalse($result);
     $result = $client->doesObjectExist($amazonS3Config['bucket'], SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->desktop));
     $this->assertFalse($result);
 }
Пример #4
0
 public function testCanCreateNewStateByInitiatingMultipartUpload()
 {
     $acl = new Acp(new Grantee('123'));
     $acl->addGrant(new Grant(new Grantee('123'), Permission::READ));
     $client = $this->getServiceBuilder()->get('s3');
     $mock = $this->setMockResponse($client, array('s3/initiate_multipart_upload'));
     $expires = time() + 1000;
     $transfer = UploadBuilder::newInstance()->setBucket('foo')->setKey('bar')->setClient($client)->setSource(__FILE__)->setHeaders(array('Foo' => 'Bar'))->setOption('Expires', $expires)->setAcp($acl)->calculateMd5(true)->build();
     $requests = $mock->getReceivedRequests();
     $this->assertEquals(1, count($requests));
     $this->assertEquals('/bar?uploads', $requests[0]->getResource());
     $this->assertEquals('Bar', (string) $requests[0]->getHeader('Foo'));
     $this->assertEquals($expires, strtotime((string) $requests[0]->getHeader('Expires')));
     $this->assertEquals('text/x-php', (string) $requests[0]->getHeader('Content-Type'));
     $this->assertNotEmpty((string) $requests[0]->getHeader('x-amz-meta-x-amz-Content-MD5'));
     $this->assertEquals('id="123"', (string) $requests[0]->getHeader('x-amz-grant-read'));
     $this->assertTrue($requests[0]->hasHeader('x-amz-grant-read'));
 }
Пример #5
0
 /**
  * Write a string to a file
  *
  * @param  string  $path     file path
  * @param  string  $content  new file content
  * @return bool
  * */
 protected function _filePutContents($path, $content)
 {
     $oldSettings = $this->getFile($path);
     $settings = array("Bucket" => $this->bucket, "Key" => $path, "ACL" => $this->options['acl'], "Body" => $content);
     if ($oldSettings) {
         $settings['Metadata'] = $oldSettings['Metadata'];
         $settings['ContentType'] = $oldSettings['ContentType'];
         $settings['ACP'] = \Aws\S3\Model\Acp::fromArray($this->s3->getObjectAcl(array('Bucket' => $this->bucket, 'Key' => $path))->toArray());
         unset($settings['ACL']);
     }
     return $this->s3->putObject($settings);
 }
Пример #6
0
 public function testCanUpdateCommandHeaders()
 {
     // Build up mock grants via cloning
     $grant = $this->getMockBuilder('Aws\\S3\\Model\\Grant')->disableOriginalConstructor()->getMock();
     $g1 = clone $grant;
     $g1->expects($this->any())->method('getParameterArray')->will($this->returnValue(array('GrantRead' => 'id="user-id-1"')));
     $g2 = clone $grant;
     $g2->expects($this->any())->method('getParameterArray')->will($this->returnValue(array('GrantRead' => 'id="user-id-2"')));
     $g3 = clone $grant;
     $g3->expects($this->any())->method('getParameterArray')->will($this->returnValue(array('GrantWrite' => 'id="user-id-3"')));
     $s3 = $this->getServiceBuilder()->get('s3');
     $acp = new Acp($this->getMockedOwner(), array($g1, $g2, $g3));
     $cmd = $s3->getCommand('PutObject');
     $acp->updateCommand($cmd);
     $this->assertEquals('id="user-id-1", id="user-id-2"', $cmd->get('GrantRead'));
     $this->assertEquals('id="user-id-3"', $cmd->get('GrantWrite'));
 }