/** * Returns the URL to an object identified by its bucket and key. * * The URL returned by this method is not signed nor does it ensure the the * bucket and key given to the method exist. If you need a signed URL, then * use the {@see \Aws\S3\S3Client::createPresignedRequest} method and get * the URI of the signed request. * * @param string $bucket The name of the bucket where the object is located * @param string $key The key of the object * * @return string The URL to the object */ public function getObjectUrl($site, $key) { $config = $this->_config; $bucket = $config['bucket']; $builtBucket = strlen($bucket) > 0 ? $site . '.' . $bucket : $site; return $this->_s3Client->getObjectUrl($builtBucket, $key); }
/** * @param string $bucket * @return string */ public function listAction($bucket) { $errors = []; $buckets = []; try { $result = $this->s3Client->listBuckets(); $buckets = $result->get('Buckets'); } catch (S3Exception $e) { $errors[] = sprintf('Cannot retrieve buckets: %s', $e->getMessage()); } $objects = []; if (!empty($bucket)) { try { $maxIteration = 10; $iteration = 0; $marker = ''; do { $result = $this->s3Client->listObjects(['Bucket' => $bucket, 'Marker' => $marker]); if ($result->get('Contents')) { $objects = array_merge($objects, $result->get('Contents')); } if (count($objects)) { $marker = $objects[count($objects) - 1]['Key']; } } while ($result->get('IsTruncated') && ++$iteration < $maxIteration); if ($result->get('IsTruncated')) { $errors[] = sprintf('The number of keys greater than %u, the first part is shown', count($objects)); } } catch (S3Exception $e) { $errors[] = sprintf('Cannot retrieve objects: %s', $e->getMessage()); } } return $this->twig->render('list.html.twig', ['selected_bucket' => $bucket, 'buckets' => $buckets, 'objects' => $objects, 'errors' => $errors]); }
public function testGetVersioningEnabled() { $this->_client->putBucketVersioning(array('Bucket' => $this->_bucket, 'Status' => 'Enabled')); $this->assertSame(true, $this->_restore->getVersioningEnabled()); $this->_client->putBucketVersioning(array('Bucket' => $this->_bucket, 'Status' => 'Suspended')); $this->assertSame(false, $this->_restore->getVersioningEnabled()); }
public function submitForm(array &$form, FormStateInterface $form_state) { $category = $form_state->getValue('category'); $header_bg = $form_state->getValue('header_bg')[0]; $fid = $form_state->getValue('header_bg')[0]; $filename = db_select('file_managed', 'f')->condition('f.fid', $fid, '=')->fields('f', array('filename'))->execute()->fetchField(); $data = db_select('file_managed', 'f')->condition('f.fid', $fid, '=')->fields('f', array('uri'))->execute()->fetchField(); $config = $this->config('amazons.settings'); $AccessKeyId = $config->get('AccessKeyId'); $SecretAccessKey = $config->get('SecretAccessKey'); $region = $config->get('region'); $bucket = $config->get('bucketname'); $s3Client = new \Aws\S3\S3Client(['version' => 'latest', 'region' => $region, 'credentials' => ['key' => $AccessKeyId, 'secret' => $SecretAccessKey]]); //Creating bucket dynamically // $result1 = $s3Client->createBucket(array('Bucket' => 'mybucket')); // // echo "<pre>"; // print_r($result1); // exit; //$bucket = 'ncdfiles'; $key = date('ymdhis') . time() . $filename; //$data = 'public://ncdfiles/' . $filename; $result = $s3Client->putObject(array('Bucket' => $bucket, 'Key' => $key, 'SourceFile' => $data, 'ACL' => 'public-read')); db_insert('amazon_s3_data')->fields(array('cat_name' => $category, 'file_path' => $result['ObjectURL'], 'fid' => $fid))->execute(); drupal_set_message('File uploaded to AWS server'); return; }
/** * Uploads string as file to s3 * @param $name * @param $content * @param string $contentType * @return string */ public function uploadString($name, $content, $contentType = 'text/plain') { $s3FileName = $this->getFilePathAndUniquePrefix() . $name; list($bucket, $prefix) = explode('/', $this->s3path, 2); $this->s3client->putObject(['Bucket' => $bucket, 'Key' => (empty($prefix) ? '' : trim($prefix, '/') . '/') . $s3FileName, 'ContentType' => $contentType, 'ACL' => 'private', 'ServerSideEncryption' => 'AES256', 'Body' => $content]); return $this->withUrlPrefix($s3FileName); }
/** * Get latest file for a project * * @param \Aws\S3\S3Client $s3 * @param Array $config * @param InputInterface $input * * @return mixed */ protected function getLatestFile($s3, $config, $input) { try { // Download latest available backup $results = $s3->getIterator('ListObjects', array('Bucket' => $config['bucket'], 'Prefix' => $input->getArgument('name'))); $newest = null; foreach ($results as $item) { if (is_null($newest) || $item['LastModified'] > $newest['LastModified']) { $newest = $item; } } if (!$results->count()) { // Credentials Exception would have been thrown by now, so now we can safely check for item count. throw new \Exception('No backups found for ' . $input->getArgument('name')); } } catch (InstanceProfileCredentialsException $e) { $this->getOutput()->writeln('<error>AWS credentials not found. Please run `configure` command.</error>'); exit; } catch (\Exception $e) { $this->getOutput()->writeln('<error>' . $e->getMessage() . '</error>'); exit; } $itemKeyChunks = explode('/', $newest['Key']); return array_pop($itemKeyChunks); }
/** * @param S3Client $client * @param array $options */ public function __construct(S3Client $client, $options = []) { parent::__construct($options); // We need to register the S3 stream wrapper so that we can take advantage of the base class $this->client = $client; $this->client->registerStreamWrapper(); }
/** * When providing the $source argument, you may provide a string referencing * the path to a directory on disk to upload, an s3 scheme URI that contains * the bucket and key (e.g., "s3://bucket/key"), or an \Iterator object * that yields strings containing filenames that are the path to a file on * disk or an s3 scheme URI. The "/key" portion of an s3 URI is optional. * * When providing an iterator for the $source argument, you must also * provide a 'base_dir' key value pair in the $options argument. * * The $dest argument can be the path to a directory on disk or an s3 * scheme URI (e.g., "s3://bucket/key"). * * The options array can contain the following key value pairs: * * - base_dir: The directory to remove from the filename when saving. * - before: A callable that accepts the following positional arguments: * source, dest, command; where command is an instance of a Command * object. The provided command will be either a GetObject, PutObject, * InitiateMultipartUpload, or UploadPart command. * - mup_threshold: Size in bytes in which a multipart upload should be * used instead of PutObject. Defaults to 20971520 (20 MB). * - concurrency: Number of files to upload concurrently. Defaults to 5. * - debug: Set to true to print out debug information for transfers. Set * to an fopen() resource to write to a specific stream. * * @param S3Client $client Client used for transfers. * @param string|\Iterator $source Where the files are transferred from. * @param string $dest Where the files are transferred to. * @param array $options Hash of options. */ public function __construct(S3Client $client, $source, $dest, array $options = []) { $client->registerStreamWrapper(); if (is_string($source)) { $this->base_dir = $source; $source = Utils::recursiveDirIterator($source); } elseif (!$source instanceof \Iterator) { throw new \InvalidArgumentException('source must be the path to a ' . 'directory or an iterator that yields file names.'); } elseif (!$this->base_dir) { throw new \InvalidArgumentException('You must provide the source ' . 'argument as a string or provide the "base_dir" option.'); } $valid = ['mup_threshold', 'base_dir', 'before', 'concurrency', 'debug']; foreach ($valid as $opt) { if (isset($options[$opt])) { $this->{$opt} = $options[$opt]; } } if ($this->mup_threshold < 5248000) { throw new \InvalidArgumentException('mup_threshold must be >= 5248000'); } // Normalize the destination and source directory. $this->dest = rtrim(str_replace('\\', '/', $dest), '/'); $this->base_dir = rtrim(str_replace('\\', '/', $this->base_dir), '/'); $this->destScheme = $this->getScheme($this->dest); $this->sourceScheme = $this->getScheme($this->base_dir); $this->client = $client; if ($this->destScheme == 's3') { $this->s3Args = $this->getS3Args($this->dest); } if ($this->debug) { $this->wrapDebug(); } $this->source = $this->wrapIterator($source); }
function writeCsv(array $headerRow, array $resultsArray) { $this->s3Client->registerStreamWrapper(); $exportBucketPath = $this->getOutputFilePath($this->exportPathWithProtocol); $this->writeToDestination($exportBucketPath, $headerRow, $resultsArray); return $exportBucketPath; }
/** * {@inheritdoc} */ public function delete($path) { try { $this->s3->deleteObject(['Bucket' => $this->bucket, 'Key' => $path]); } catch (AwsExceptionInterface $e) { throw Exception\StorageException::deleteError($path, $e); } }
/** * Constructor * * @access public * @param string $key AWS API Key * @param string $secret AWS API Secret * @param string $region AWS S3 Region * @param string $bucket AWS S3 bucket * @param string $prefix Object prefix */ public function __construct($key, $secret, $region, $bucket, $prefix) { $this->bucket = $bucket; $credentials = new Credentials($key, $secret); $this->client = new S3Client(['credentials' => $credentials, 'region' => $region, 'version' => '2006-03-01']); $this->client->registerStreamWrapper(); $this->prefix = $prefix; }
/** * {@inheritdoc} */ public function getUrl($filename) { $path = $filename; if ($this->directory) { $path = sprintf('%s/%s', $this->directory, $path); } return $this->client->getObjectUrl($this->bucket, $path); }
/** * @param $filename * @return \Aws\Result|null */ public function remove($filename) { $result = null; try { $result = $this->s3->deleteObject(['Bucket' => $this->bucket, 'Key' => $filename]); } catch (\Exception $e) { } return $result; }
public function testCanCompleteMultipartUpload() { $this->prepareTransfer(); $model = $this->getMockBuilder('Guzzle\\Service\\Resource\\Model')->disableOriginalConstructor()->getMock(); $command = $this->getMockBuilder('Guzzle\\Service\\Command\\OperationCommand')->disableOriginalConstructor()->getMock(); $command->expects($this->any())->method('getResult')->will($this->returnValue($model)); $this->client->expects($this->any())->method('getCommand')->will($this->returnValue($command)); $this->assertInstanceOf('Guzzle\\Service\\Resource\\Model', $this->callProtectedMethod($this->transfer, 'complete')); }
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; }
public function testBasicOperations() { $inputBucket = 'php-integ-transcoder-test-bucket-input'; $outputBucket = 'php-integ-transcoder-test-bucket-output'; $roleName = 'php-integ-transcoder-test-role'; $policyName = 'php-integ-transcoder-test-policy'; $pipelineName = 'php-integ-transcoder-test-pipeline'; self::log('Create input and output buckets for the Elastic Transcoder pipeline.'); $commands = array(); $commands[] = $this->s3->getCommand('CreateBucket', array('Bucket' => $inputBucket)); $commands[] = $this->s3->getCommand('CreateBucket', array('Bucket' => $outputBucket)); $this->s3->execute($commands); self::log('Create an IAM Role for the Elastic Transcoder pipeline.'); $result = $this->iam->getCommand('CreateRole', array('RoleName' => $roleName, 'AssumeRolePolicyDocument' => self::DUMMY_IAM_POLICY_ASSUME_ROLE))->getResult(); $roleArn = $result->getPath('Role/Arn'); self::log('Put a policy on the IAM Role for the Elastic Transcoder pipeline.'); $result = $this->iam->getCommand('PutRolePolicy', array('PolicyName' => $policyName, 'RoleName' => $roleName, 'PolicyDocument' => self::DUMMY_IAM_POLICY_ALLOW_S3))->getResult(); self::log('Use TestRole to validate our pipeline inputs. NOTE: Ours are not valid on purpose.'); $result = $this->transcoder->getCommand('TestRole', array('InputBucket' => $inputBucket, 'OutputBucket' => $outputBucket, 'Role' => $roleArn, 'Topics' => array()))->getResult(); $this->assertEquals('false', $result['Success']); self::log('Create an Elastic Transcoder pipeline.'); $result = $this->transcoder->getCommand('CreatePipeline', array('Name' => $pipelineName, 'InputBucket' => $inputBucket, 'OutputBucket' => $outputBucket, 'Role' => $roleArn, 'Notifications' => array_fill_keys(array('Progressing', 'Completed', 'Warning', 'Error'), '')))->getResult(); $pipelineId = $result->getPath('Pipeline/Id'); self::log('Make sure created Elastic Transcoder pipeline is in the list of pipelines.'); $result = $this->transcoder->getCommand('ListPipelines')->getResult(); $pipelineNames = $result->getPath('Pipelines/*/Name'); $this->assertContains($pipelineName, $pipelineNames); self::log('Make sure ListPipelines iterator works.'); $found = false; foreach ($this->transcoder->getIterator('ListPipelines') as $pipeline) { if ($pipeline['Name'] == $pipelineName) { $found = true; break; } } if (!$found) { $this->fail('Did not find the pipeline in the iterator results.'); } self::log('Make sure created Elastic Transcoder pipeline can be read.'); $result = $this->transcoder->getCommand('ReadPipeline', array('Id' => $pipelineId))->getResult(); $this->assertEquals($pipelineName, $result->getPath('Pipeline/Name')); self::log('Delete the Elastic Transcoder pipeline.'); $response = $this->transcoder->getCommand('DeletePipeline', array('Id' => $pipelineId))->getResponse(); $this->assertEquals(202, $response->getStatusCode()); self::log('Delete the policy from the IAM Role for the Elastic Transcoder pipeline.'); $result = $this->iam->getCommand('DeleteRolePolicy', array('PolicyName' => $policyName, 'RoleName' => $roleName))->getResult(); self::log('Delete the IAM Role for the Elastic Transcoder pipeline.'); $result = $this->iam->getCommand('DeleteRole', array('RoleName' => $roleName))->getResult(); self::log('Delete the input and output buckets for the Elastic Transcoder pipeline.'); $commands = array(); $commands[] = $this->s3->getCommand('DeleteBucket', array('Bucket' => $inputBucket)); $commands[] = $this->s3->getCommand('DeleteBucket', array('Bucket' => $outputBucket)); $this->s3->execute($commands); }
/** * {@inheritDoc} */ public function find($path) { $s3Url = $this->client->getObjectUrl($this->bucket, trim($this->prefix, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR)); if (false == @getimagesize($s3Url)) { return $this->fallbackLoader->find($path); } $tmpFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename($s3Url); file_put_contents($tmpFilePath, file_get_contents($s3Url)); $mimeType = $this->mimeTypeGuesser->guess($tmpFilePath); unlink($tmpFilePath); return new Binary(file_get_contents($s3Url), $mimeType, $this->extensionGuesser->guess($mimeType)); }
/** * @param string $html the content to be uploaded. * @return string the URL of the uploaded content. */ public function uploadHtml($headers, $html) { $file = date('YmdHis-') . uniqid("", true); $key = (!empty($this->directoryPath) ? $this->directoryPath . '/' : '') . $file . '.html'; $url = $this->_client->getObjectUrl($this->bucket, $key); $result = $this->_client->putObject(array_merge(['ACL' => 'public-read', 'Bucket' => $this->bucket, 'Body' => str_replace($this->archiveUrlTag, $url, $html), 'CacheControl' => 'max-age=31536000, public', 'ContentType' => 'text/html', 'Key' => $key, 'Metadata' => ['X-UID-MailHeader' => \yii\helpers\Json::encode($headers)], 'Expires' => gmdate('D, d M Y H:i:s \\G\\M\\T', strtotime('+5 year'))], $this->uploadOptions)); if ($result) { return $url; } else { return false; } }
/** * @param $file_contenthash * @param $file_statusamazon * @return string */ public function getTokenUrl($file_contenthash, $file_statusamazon) { $fileRelativeLocation = substr($this->path_from_hash($file_contenthash), 1); if (!class_exists('S3Util')) { require_once __DIR__ . '/S3Util.php'; } $file = S3Util::getPrefix() . $fileRelativeLocation; if ($file_statusamazon == 'public') { return S3Util::getS3Url() . $fileRelativeLocation; } return $this->client->getObjectUrl($this->bucket_name, $file, '+5 minutes'); }
/** * Constructs the PostObject. * * @param S3Client $client Client used with the POST object * @param string $bucket Bucket to use * @param array $formInputs Associative array of form input fields. * @param string|array $jsonPolicy JSON encoded POST policy document. The * policy will be base64 encoded and applied * to the form on your behalf. */ public function __construct(S3Client $client, $bucket, array $formInputs, $jsonPolicy) { $this->client = $client; $this->bucket = $bucket; if (is_array($jsonPolicy)) { $jsonPolicy = json_encode($jsonPolicy); } $this->jsonPolicy = $jsonPolicy; $this->formAttributes = ['action' => $this->generateUri(), 'method' => 'POST', 'enctype' => 'multipart/form-data']; $this->formInputs = $formInputs + ['key' => '${filename}']; $credentials = $client->getCredentials()->wait(); $this->formInputs += $this->getPolicyAndSignature($credentials); }
/** * Create a link to a S3 object from a bucket. If expiration is not empty, then it is used to create * a signed URL * * @param string $object The object name (full path) * @param string $bucket The bucket name * @param string|int $expiration The Unix timestamp to expire at or a string that can be evaluated by strtotime * @throws InvalidDomainNameException * @return string */ public function __invoke($object, $bucket = '', $expiration = '') { $bucket = trim($bucket ?: $this->getDefaultBucket(), '/'); if (empty($bucket)) { throw new InvalidDomainNameException('An empty bucket name was given'); } if ($expiration) { $command = $this->client->getCommand('GetObject', ['Bucket' => $bucket, 'Key' => $object]); return $this->client->createPresignedRequest($command, $expiration)->getUri()->__toString(); } else { return $this->client->getObjectUrl($bucket, $object); } }
/** * Criar storage do aws S3 * * @param array $config * * @return StorageInterface */ protected function registerS3Storage(array $config) { $this->app['nwlaravel.filestorage.storage'] = $this->app->share(function ($app) use($config) { $s3 = S3Client::factory(array('key' => $config['access'], 'secret' => $config['secret'])); return new S3Storage($config['root'], $s3); }); }
/** * @return S3Client */ private function getMockS3Client() { $mock = new MockPlugin(array(new Response(200, null, '{"Records":[{"r1":"r1"},{"r2":"r2"},{"r3":"r3"}]}'), new Response(200, null, '{}'), new Response(200, null, '{"Records":[{"r4":"r4"},{"r5":"r5"}]}'))); $client = S3Client::factory(array('key' => 'foo', 'secret' => 'bar')); $client->addSubscriber($mock); return $client; }
/** * List contents of a directory * * @param string $dirname * @param bool $recursive * @return array directory contents */ public function listContents($dirname = '', $recursive = false) { $result = $this->client->listObjects(array('Bucket' => $this->bucket))->getAll(array('Contents')); $contents = isset($result['Contents']) ? $result['Contents'] : array(); $result = array_map(array($this, 'normalizeObject'), $contents); return Util::emulateDirectories($result); }
public function testReceiveAlteredReceiptHandle() { $this->sqsClient->method('__call')->with('receiveMessage')->willReturnCallback(function ($name, array $args) { $factory = new ClaimCheckFactory('MyBucket'); $serializer = new ClaimCheckJsonSerializer(); return new Result(['Messages' => [['Body' => $serializer->serialize($factory->create('MyKey')), 'ReceiptHandle' => 'MyReceiptHandle']]]); }); $this->s3Client->method('__call')->with('getObject')->willReturnCallback(function ($name, array $args) { return new Result(['Body' => 'MyOriginalMessage']); }); $actual = $this->sut->receiveMessage(); self::assertInstanceOf(Result::class, $actual); self::assertContains('MyReceiptHandle', $actual->search('Messages[].ReceiptHandle|[0]')); self::assertContains('MyBucket', $actual->search('Messages[].ReceiptHandle|[0]')); self::assertContains('MyKey', $actual->search('Messages[].ReceiptHandle|[0]')); }
/** * @inheritdoc * @param array $values */ public function __construct(array $values = []) { $values['amazon_s3_client'] = $this->share(function (Application $app) { return S3Client::factory(array_merge($this->getCredentials(), ['version' => '2006-03-01', 'ssl.certificate_authority' => false])); }); $values['amazon_s3_credentials_cookie_name'] = 'credentials'; $values['controller.amazon_s3_client'] = $this->share(function (Application $app) { return new AmazonS3Controller($app['twig'], $app['amazon_s3_client']); }); $values['controller.authentication'] = $this->share(function (Application $app) { return new AuthenticationController($app['twig'], $app['amazon_s3_credentials_cookie_name']); }); parent::__construct($values); $this->register(new TwigServiceProvider(), ['twig.path' => __DIR__ . '/../views', 'twig.options' => ['cache' => is_writable(__DIR__ . '/..') ? __DIR__ . '/../cache/twig' : false]]); $this->register(new UrlGeneratorServiceProvider()); $this->register(new ServiceControllerServiceProvider()); $this->get('/login', 'controller.authentication:loginAction')->bind('login')->before(function (Request $request, Application $app) { $credentials = $this->getCredentials(); if (!empty($credentials)) { return new RedirectResponse($app['url_generator']->generate('list')); } }); $this->post('/login', 'controller.authentication:authenticateAction'); $this->post('/logout', 'controller.authentication:logoutAction')->bind('logout'); $this->get('/{bucket}', 'controller.amazon_s3_client:listAction')->value('bucket', null)->bind('list')->before(function (Request $request, Application $app) { $credentials = $this->getCredentials(); if (empty($credentials)) { return $app->handle(Request::create($app['url_generator']->generate('login')), HttpKernelInterface::SUB_REQUEST, false); } }); }
/** * Register specific services for the module */ public function registerServices(DiInterface $di) { $config = $di->get('config'); //Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace("Promoziti\\Modules\\Business\\Controllers"); return $dispatcher; }); $di->set('view', function () { $view = new View(); $view->setViewsDir(__DIR__ . '/views/'); $view->registerEngines(array('.volt' => function ($view, $di) { $volt = new VoltEngine($view, $di); $config = $di->get('config'); $volt->setOptions(array('compileAlways' => true, 'compiledPath' => $config->application->cache_dir . "volt/", 'compiledSeparator' => '_')); return $volt; })); return $view; }); $di->set('aws_s3', function () use($config) { //version 2.7 style $s3 = \Aws\S3\S3Client::factory(array('key' => $config->application->security->aws->key, 'secret' => $config->application->security->aws->secret, 'region' => 'us-west-2', 'version' => '2006-03-01')); return $s3; }); }
/** * Creates adapter instance * * @param Enlight_Event_EventArgs $args * @return AdapterInterface */ public function createS3Adapter(Enlight_Event_EventArgs $args) { $defaultConfig = ['key' => '', 'secret' => '', 'region' => '', 'version' => 'latest', 'bucket' => '', 'prefix' => '']; $config = array_merge($defaultConfig, $args->get('config')); $client = S3Client::factory(['credentials' => ['key' => $config['key'], 'secret' => $config['secret']], 'region' => $config['region'], 'version' => $config['version']]); return new AwsS3Adapter($client, $config['bucket'], $config['prefix']); }
public function __construct($key, $secret) { $this->key = $key; $this->secret = $secret; $arr = array('key' => $key, 'secret' => $secret, 'region' => AWS_REGION); $this->s3 = S3Client::factory($arr); }
public static function get() { if (S3ClientBuilder::$client == null) { S3ClientBuilder::$client = S3Client::factory(); } return S3ClientBuilder::$client; }