/** * Transport the file to Amazon Glacier and return the archive ID. * * @param \Transit\File $file * @return string * @throws \Transit\Exception\TransportationException */ public function transport(File $file) { $config = $this->_config; $response = null; // If larger then 100MB, split upload into parts if ($file->size() >= 100 * Size::MB) { $uploader = UploadBuilder::newInstance()->setClient($this->getClient())->setSource($file->path())->setVaultName($config['vault'])->setAccountId($config['accountId'] ?: '-')->setPartSize(10 * Size::MB)->build(); try { $response = $uploader->upload(); } catch (MultipartUploadException $e) { $uploader->abort(); } } else { $response = $this->getClient()->uploadArchive(array_filter(array('vaultName' => $config['vault'], 'accountId' => $config['accountId'], 'body' => EntityBody::factory(fopen($file->path(), 'r'))))); } // Return archive ID if successful if ($response) { $file->delete(); return $response->getPath('archiveId'); } throw new TransportationException(sprintf('Failed to transport %s to Amazon Glacier', $file->basename())); }
public function testBuildsDifferentUploaderBasedOnConcurrency() { $generator = $this->getMockBuilder('Aws\\Glacier\\Model\\MultipartUpload\\UploadPartGenerator')->setMethods(array('getPartSize'))->disableOriginalConstructor()->getMock(); $generator->expects($this->any())->method('getPartSize')->will($this->returnValue(1024 * 1024)); $state = new TransferState(UploadId::fromParams(array('accountId' => 'foo', 'vaultName' => 'baz', 'uploadId' => 'bar'))); $state->setPartGenerator($generator); $b = UploadBuilder::newInstance()->setVaultName('foo')->setPartGenerator($generator)->setClient($this->getServiceBuilder()->get('glacier'))->resumeFrom($state)->setSource(EntityBody::factory(fopen(__FILE__, 'r'))); $this->assertInstanceOf('Aws\\Glacier\\Model\\MultipartUpload\\SerialTransfer', $b->build()); $b->setConcurrency(2); $this->assertInstanceOf('Aws\\Glacier\\Model\\MultipartUpload\\ParallelTransfer', $b->build()); }
public function testMultipartUploadAbstractions() { $source = EntityBody::factory(str_repeat('x', 6 * Size::MB + 425)); /** @var $transfer Transfer */ $transfer = UploadBuilder::newInstance()->setClient($this->client)->setSource($source)->setVaultName(self::TEST_VAULT)->setPartSize(Size::MB)->setArchiveDescription('Foo bar 3')->build(); $transfer->getEventDispatcher()->addListener($transfer::BEFORE_PART_UPLOAD, function ($event) { static $count = 0; if ($count > 2) { throw new \Exception(); } $count++; }); try { $transfer->upload(); $serializedState = null; $this->fail('Unexpected code execution - exit point 1'); } catch (MultipartUploadException $e) { $serializedState = serialize($e->getState()); } $state = unserialize($serializedState); $this->assertInstanceOf('Aws\\Glacier\\Model\\MultipartUpload\\TransferState', $state); /** @var $transfer Transfer */ $transfer = UploadBuilder::newInstance()->setClient($this->client)->setSource($source)->setVaultName(self::TEST_VAULT)->resumeFrom($state)->build(); try { $result = $transfer->upload(); } catch (MultipartUploadException $e) { $result = null; $this->fail('Unexpected code execution - exit point 2'); } $this->assertNotEmpty($result['archiveId']); $this->assertEquals($result['checksum'], $transfer->getState()->getPartGenerator()->getRootChecksum()); $this->client->deleteArchive(array('vaultName' => self::TEST_VAULT, 'archiveId' => $result['archiveId'])); }