/** * Downloads a bucket to the local filesystem * * @param string $directory Directory to download to * @param string $bucket Bucket to download from * @param string $keyPrefix Only download objects that use this key prefix * @param array $options Associative array of download options * - params: Array of parameters to use with each GetObject operation performed during the transfer * - base_dir: Base directory to remove from each object key when storing in the local filesystem * - force: Set to true to download every file, even if the file is already on the local filesystem and has not * changed * - concurrency: Maximum number of parallel downloads (defaults to 10) * - debug: Set to true or a fopen resource to enable debug mode to print information about each download * - allow_resumable: Set to true to allow previously interrupted downloads to be resumed using a Range GET */ public function downloadBucket($directory, $bucket, $keyPrefix = '', array $options = array()) { $options = new Collection($options); $builder = $options['builder'] ?: DownloadSyncBuilder::getInstance(); $builder->setDirectory($directory)->setClient($this)->setBucket($bucket)->setKeyPrefix($keyPrefix)->setConcurrency($options['concurrency'] ?: 10)->setBaseDir($options['base_dir'])->force($options['force'])->setOperationParams($options['params'] ?: array())->enableDebugOutput($options['debug']); if ($options['allow_resumable']) { $builder->allowResumableDownloads(); } $builder->build()->transfer(); }
public function testDoesNotDownloadGlacierStorageObjects() { $res = <<<EOT HTTP/1.1 200 OK x-amz-id-2: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX x-amz-request-id: XXXXXXXXXXXXXXXX Date: Thu, 04 Jul 2012 12:00:00 GMT Content-Type: application/xml Server: AmazonS3 <?xml version="1.0" encoding="UTF-8"?> <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Name>bucket-1</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys></MaxKeys> <Delimiter>/</Delimiter> <IsTruncated>false</IsTruncated> <Contents> <Key>e</Key> <LastModified>2012-04-07T12:00:00.000Z</LastModified> <ETag>"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"</ETag> <Size>0</Size> <Owner> <ID>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</ID> <DisplayName>XXXXXXXXXX</DisplayName> </Owner> <StorageClass>GLACIER</StorageClass> </Contents> <Contents> <Key>f</Key> <LastModified>2012-04-07T12:00:00.000Z</LastModified> <ETag>"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"</ETag> <Size>0</Size> <Owner> <ID>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</ID> <DisplayName>XXXXXXXXXX</DisplayName> </Owner> <StorageClass>STANDARD</StorageClass> </Contents> </ListBucketResult> EOT; $s3 = S3Client::factory(array('key' => 'foo', 'secret' => 'bar')); $s3->getEventDispatcher()->addSubscriber(new MockPlugin(array(Response::fromMessage($res), new Response(200)))); $dir = __DIR__ . '/../../../../../build/artifacts'; if (!is_dir($dir)) { mkdir($dir); } DownloadSyncBuilder::getInstance()->setClient($s3)->setBucket('Foo')->setDirectory($dir)->build()->transfer(); $this->assertFileNotExists($dir . '/e'); $this->assertFileExists($dir . '/f'); unlink($dir . '/f'); }
/** * Copy a directory from local space to S3 * * @param string $remotePath * @param string $localPath * @param ProgressUpdater $progress * @return array */ public function getRemoteDir($remotePath, $localPath, $progress = null) { if (substr($remotePath, -1) == '/') { $remotePath = substr($remotePath, 0, -1); } $fileData = array(); $fileData['exit_code'] = null; $s3Client = $this->awsConnect(); if ($this->verifyBucketExists($s3Client)) { if (!is_dir($localPath)) { mkdir($localPath, 0777, true); } $downloader = \Aws\S3\Sync\DownloadSyncBuilder::getInstance()->setBucket($this->bucketName)->setClient($s3Client)->setConcurrency($this->concurrentDownloads)->setBaseDir($remotePath)->force(true)->setKeyPrefix($remotePath); $downloader->setDirectory($localPath); $downloadSync = $downloader->build(); //add callback to progress updater and logging $downloadSync->getEventDispatcher()->addListener(Sync\DownloadSync::AFTER_TRANSFER, function (Guzzle\Common\Event $e) use($progress, $fileData) { if (is_object($progress)) { $progress->incrementStep(); } $c = $e['command']; $this->debug('[S3Sync::getRemoteDir] Copied ' . $c->get('Key') . ' => ' . $c->get('SaveAs')); $fileData[$c->get('Key')] = $c->get('SaveAs'); }); $downloadSync->transfer(); $fileData['exit_code'] = 0; } else { $fileData['exit_code'] = 1; } return $fileData; }
protected function getDebugSync() { $out = fopen('php://temp', 'r+'); $sync = DownloadSyncBuilder::getInstance()->enableDebugOutput($out)->setClient($this->getServiceBuilder()->get('s3', true))->setBucket('Foo')->setDirectory(__DIR__)->setSourceIterator(new \ArrayIterator(array(__FILE__)))->build(); return array($sync, $out); }