public function opendir($path) { $path = $this->normalizePath($path); if ($path === '.') { $path = ''; } else { if ($path) { $path .= '/'; } } try { $files = array(); $result = $this->connection->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Delimiter' => '/', 'Prefix' => $path), array('return_prefixes' => true)); foreach ($result as $object) { $file = basename(isset($object['Key']) ? $object['Key'] : $object['Prefix']); if ($file != basename($path)) { $files[] = $file; } } \OC\Files\Stream\Dir::register('amazons3' . $path, $files); return opendir('fakedir://amazons3' . $path); } catch (S3Exception $e) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); return false; } }
/** * @depends testHeadBucket */ public function notestPutAndListObjects() { $this->client->waitUntil('bucket_exists', array('Bucket' => $this->bucket)); $command = $this->client->getCommand('PutObject', array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY, 'ContentMD5' => true, 'Body' => 'åbc 123', 'ContentType' => 'application/foo', 'ACP' => $this->acp, 'Metadata' => array('test' => '123', 'abc' => '@pples', 'foo' => '', 'null' => null, 'space' => ' ', 'zero' => '0', 'trim' => ' hi '))); self::log("Uploading an object"); $result = $command->execute(); // make sure the expect header wasn't sent $this->assertNull($command->getRequest()->getHeader('Expect')); $this->assertInstanceOf('Guzzle\\Service\\Resource\\Model', $result); $this->assertNotEmpty($result['ETag']); $this->client->waitUntil('object_exists', array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY)); self::log("HEAD the object"); $result = $this->client->headObject(array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY)); $this->assertEquals('application/foo', $result['ContentType']); $this->assertEquals('123', $result['Metadata']['test']); $this->assertEquals('@pples', $result['Metadata']['abc']); // Ensure the object was created correctly self::log("GETting the object"); $result = $this->client->getObject(array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY)); $this->assertInstanceOf('Guzzle\\Service\\Resource\\Model', $result); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $result['Body']); $this->assertEquals('åbc 123', (string) $result['Body']); $this->assertEquals('application/foo', $result['ContentType']); $this->assertEquals('123', $result['Metadata']['test']); $this->assertEquals('@pples', $result['Metadata']['abc']); // Ensure the object was created and we can find it in the iterator self::log("Checking if the item is in the ListObjects results"); $iterator = $this->client->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Prefix' => self::TEST_KEY)); $objects = $iterator->toArray(); $this->assertEquals(1, count($objects)); $this->assertEquals('foo', $objects[0]['Key']); }
/** * List contents of a directory * * @param string $dirname * @param bool $recursive * @return array directory contents */ public function listContents($dirname = '', $recursive = false) { $objectsIterator = $this->client->getIterator('listObjects', array('Bucket' => $this->bucket, 'Prefix' => $this->prefix($dirname))); $contents = iterator_to_array($objectsIterator); $result = array_map(array($this, 'normalizeObject'), $contents); return Util::emulateDirectories($result); }
/** * Copy Object * * @param string $from Path From * @param string $to Path To * * @return bool */ public function copy($from, $to) { $from = ltrim($from, "/"); $to = ltrim($to, "/"); if (!$this->exists($from)) { $path = $this->bucket . "/" . $from; throw new ExceptionStorage("Object {$path} Not found", static::E_NOT_IS_DIR); } if ($this->exists($to)) { $path = $this->bucket . "/" . $to; throw new ExceptionStorage("Object {$path} exists"); } // Copia Object $this->clientS3->copyObject(array('Bucket' => $this->bucket, 'Key' => $to, 'CopySource' => $this->bucket . '/' . rawurlencode($from), 'ACL' => static::ACL_PUBLIC_READ, 'MetadataDirective' => 'COPY')); $pathFrom = trim($from, '/') . '/'; $iterator = $this->clientS3->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Prefix' => $pathFrom)); foreach ($iterator as $object) { $pattern = preg_quote($pathFrom, "/"); $Key = preg_replace("/^{$pattern}/", "", $object['Key']); if (!empty($Key)) { $pattern = preg_quote($pathFrom, "/"); $Key = preg_replace("/^{$pattern}/", "", $object['Key']); $origem = $this->bucket . '/' . $object['Key']; $destino = trim($to, "/") . "/" . $Key; // Copia Object $this->clientS3->copyObject(array('Bucket' => $this->bucket, 'Key' => $destino, 'CopySource' => $origem, 'ACL' => static::ACL_PUBLIC_READ, 'MetadataDirective' => 'COPY')); } } return true; }
/** * 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); }
/** * Remove file * * @param string $path file path * @return Guzzle\Service\Resource\Model **/ protected function _unlink($path) { $clear = new \Aws\S3\Model\ClearBucket($this->s3, $this->bucket); $iterator = $this->s3->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Prefix' => $this->pathToKey($path))); $clear->setIterator($iterator); $clear->clear(); return $this->s3->deleteObject(array("Bucket" => $this->bucket, "Key" => $this->pathToKey($path))); }
/** * List contents of a directory. * * @param string $directory * @param bool $recursive * * @return array */ public function listContents($directory = '', $recursive = false) { $prefix = $this->applyPathPrefix(rtrim($directory, '/') . '/'); $options = ['Bucket' => $this->bucket, 'Prefix' => ltrim($prefix, '/')]; $iterator = $this->s3Client->getIterator('ListObjects', $options); $normalizer = [$this, 'normalizeResponse']; $normalized = array_map($normalizer, iterator_to_array($iterator)); return Util::emulateDirectories($normalized); }
/** * @return array */ public function listObject() { $result = []; $iterator = $this->s3->getIterator('ListObjects', array('Bucket' => $this->bucket)); foreach ($iterator as $object) { $result[] = $object['Key']; } return $result; }
/** * @param $namespace * @param $filter * @return array */ public function find($namespace, $filter) { $pattern = self::buildPattern($filter); $iterator = $this->s3Client->getIterator('ListObjects', ['Bucket' => $this->basePath, 'Prefix' => Utils::normalizePath($this->relativePath . '/' . $namespace)]); $results = []; foreach ($iterator as $object) { if (!$pattern || preg_match($pattern, '/' . strtr($object['Key'], '\\', '/'))) { $url = $this->s3Client->getObjectUrl($this->basePath, $object['Key']); $results[$url] = new \SplFileInfo($url); } } return $results; }
/** * @param string $path * @return array */ public function getFilesList($path = "") { $files = array(); $options = array('Bucket' => $this->bucket); if ($path) { $options['Prefix'] = $path; $options['Delimiter'] = '/'; } $iterator = $this->s3Client->getIterator('ListObjects', $options); foreach ($iterator as $object) { $files[] = array('timestamp' => date("U", strtotime($object['LastModified'])), 'filename' => $object['Key']); } return $files; }
/** * Get recursive $path listing collection * @param string $path Path for listing contents * @param array $restrict Collection of restricted paths * @param array $result Collection of restricted paths * @return array $result Resulting collection used in recursion */ public function dir($path, $restrict = array(), &$result = array()) { $iterator = $this->client->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Prefix' => $path)); foreach ($iterator as $object) { $key = $object['Key']; if (!$this->isKeyDir($key)) { $result[] = $key; } } // Sort results if (sizeof($result)) { sort($result); } return $result; }
/** * List cache keys. * @return array array of keys. */ private function listCacheKeys() { try { $objects = $this->_client->getIterator('ListObjects', ['Bucket' => $this->bucket, 'Prefix' => $this->directoryPath]); $keys = []; foreach ($objects as $object) { //yield $object['Key']; $keys[] = $object['Key']; } return $keys; } catch (\Aws\S3\Exception\S3Exception $e) { return []; } }
public function getFilesList($basePath) { return new eZDFSFileHandlerDFSAmazonFilterIterator($this->s3client->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Prefix' => $basePath))); }
$prefix = isset($_POST['prefix']) ? $_POST['prefix'] . '/' : ''; $s3Bucket = isset($_POST['s3Bucket']) ? $_POST['s3Bucket'] : $defaultBucket; $fileKey = isset($_POST['fileKey']) ? $_POST['fileKey'] : ''; $submit = isset($_POST['submit']) ? $_POST['submit'] : ''; $fileName = $fileType = ''; //Create AWS connection and s3client //Must provide your own s3credentials.php file $s3client = new Aws\S3\S3Client(['version' => 'latest', 'region' => 'us-east-1', 'credentials' => $credentials]); echo "<html>\r\n"; echo " <h1>Bucket:: {$s3Bucket}</h1>\r\n"; // Check if DEL form submited if ($submit == 'Delete' && isset($fileKey)) { $s3client->deleteObject(array('Bucket' => $s3Bucket, 'Key' => $fileKey)); } // Display files currently uploaded in S3Bucket $o_iter = $s3client->getIterator('ListObjects', array('Bucket' => $s3Bucket)); foreach ($o_iter as $o) { if ($o['Size'] != '0') { // Because we don't want to list folders echo "<form enctype='multipart/form-data' name='fileDELForm' method='post' action='index.php' style='border:1px;' >"; $prefixKey = explode("/", $o['Key']); if ($prefixKey[0] == 'public') { echo "<a href='http://" . $s3Bucket . "/" . $o['Key'] . "'>"; echo "<img src='http://{$s3Bucket}/" . $o['Key'] . "' style='width:100px;height:100px;'/></a>"; echo "<input type='hidden' name='fileKey' id='fileKey' value='" . $o['Key'] . "' />"; echo "{$o['Key']}"; echo "<input type='submit' name='submit' id='submit' value='Delete' />"; } if ($prefixKey[0] == 'QSA') { $objectPre = $s3client->getCommand('GetObject', ['Bucket' => $s3Bucket, 'Key' => $o['Key']]); $presignedRequest = $s3client->createPresignedRequest($objectPre, '+30 minutes');