s3 external storage listing rework

Signed-off-by: Robin Appelman <robin@icewind.nl>
pull/29220/head
Robin Appelman 2021-10-13 19:42:31 +07:00
parent fadeae8c8a
commit 09ffac5e6d
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
3 changed files with 213 additions and 97 deletions

@ -0,0 +1,65 @@
name: S3 External storage
on:
push:
branches:
- master
- stable*
paths:
- 'apps/files_external/**'
pull_request:
paths:
- 'apps/files_external/**'
env:
APP_NAME: files_external
jobs:
s3-external-tests:
runs-on: ubuntu-latest
strategy:
# do not stop on another job's failure
fail-fast: false
matrix:
php-versions: ['7.4', '8.0']
name: php${{ matrix.php-versions }}-${{ matrix.ftpd }}
services:
minio:
image: minio/minio:RELEASE.2021-10-06T23-36-31Z
ports:
- "9000:9000"
steps:
- name: Checkout server
uses: actions/checkout@v2
with:
submodules: true
- name: Set up php ${{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: phpunit
extensions: mbstring, iconv, fileinfo, intl, sqlite, pdo_sqlite, zip, gd
- name: Set up Nextcloud
run: |
mkdir data
./occ maintenance:install --verbose --database=sqlite --database-name=nextcloud --database-host=127.0.0.1 --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass password
./occ app:enable --force ${{ env.APP_NAME }}
php -S localhost:8080 &
- name: PHPUnit
run: |
echo "<?php return ['run' => true,'hostname' => 'localhost','key' => 'minioadmin','secret' => 'minioadmin', 'bucket' => 'bucket', 'port' => 9000, 'use_ssl' => false, 'autocreate' => true, 'use_path_style' => true];" > apps/${{ env.APP_NAME }}/tests/config.amazons3.php
phpunit --configuration tests/phpunit-autotest-external.xml apps/files_external/tests/Storage/Amazons3Test.php
s3-external-summary:
runs-on: ubuntu-latest
needs: s3-external-tests
if: always()
steps:
- name: Summary status
run: if ${{ needs.s3-external-tests.result != 'success' }}; then exit 1; fi

@ -46,6 +46,7 @@ use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory; use Icewind\Streams\IteratorDirectory;
use OC\Cache\CappedMemoryCache; use OC\Cache\CappedMemoryCache;
use OC\Files\Cache\CacheEntry; use OC\Files\Cache\CacheEntry;
use OC\Files\Filesystem;
use OC\Files\ObjectStore\S3ConnectionTrait; use OC\Files\ObjectStore\S3ConnectionTrait;
use OC\Files\ObjectStore\S3ObjectTrait; use OC\Files\ObjectStore\S3ObjectTrait;
use OCP\Constants; use OCP\Constants;
@ -71,6 +72,9 @@ class AmazonS3 extends \OC\Files\Storage\Common {
/** @var IMimeTypeDetector */ /** @var IMimeTypeDetector */
private $mimeDetector; private $mimeDetector;
/** @var bool|null */
private $versioningEnabled = null;
public function __construct($parameters) { public function __construct($parameters) {
parent::__construct($parameters); parent::__construct($parameters);
$this->parseParams($parameters); $this->parseParams($parameters);
@ -120,12 +124,20 @@ class AmazonS3 extends \OC\Files\Storage\Common {
unset($this->objectCache[$existingKey]); unset($this->objectCache[$existingKey]);
} }
} }
unset($this->directoryCache[$key], $this->filesCache[$key]); unset($this->filesCache[$key]);
$keys = array_keys($this->directoryCache->getData());
$keyLength = strlen($key);
foreach ($keys as $existingKey) {
if (substr($existingKey, 0, $keyLength) === $key) {
unset($this->directoryCache[$existingKey]);
}
}
unset($this->directoryCache[$key]);
} }
/** /**
* @param $key * @param $key
* @return Result|boolean * @return array|false
*/ */
private function headObject($key) { private function headObject($key) {
if (!isset($this->objectCache[$key])) { if (!isset($this->objectCache[$key])) {
@ -133,7 +145,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
$this->objectCache[$key] = $this->getConnection()->headObject([ $this->objectCache[$key] = $this->getConnection()->headObject([
'Bucket' => $this->bucket, 'Bucket' => $this->bucket,
'Key' => $key 'Key' => $key
]); ])->toArray();
} catch (S3Exception $e) { } catch (S3Exception $e) {
if ($e->getStatusCode() >= 500) { if ($e->getStatusCode() >= 500) {
throw $e; throw $e;
@ -159,32 +171,44 @@ class AmazonS3 extends \OC\Files\Storage\Common {
* @throws \Exception * @throws \Exception
*/ */
private function doesDirectoryExist($path) { private function doesDirectoryExist($path) {
if (!isset($this->directoryCache[$path])) { if ($path === '.' || $path === '') {
return true;
}
if (isset($this->directoryCache[$path])) {
return $this->directoryCache[$path];
}
try {
// Maybe this isn't an actual key, but a prefix. // Maybe this isn't an actual key, but a prefix.
// Do a prefix listing of objects to determine. // Do a prefix listing of objects to determine.
try { $result = $this->getConnection()->listObjectsV2([
$result = $this->getConnection()->listObjects([ 'Bucket' => $this->bucket,
'Bucket' => $this->bucket, 'Prefix' => rtrim($path, '/'),
'Prefix' => rtrim($path, '/'), 'MaxKeys' => 1,
'MaxKeys' => 1, ]);
'Delimiter' => '/',
]);
if ((isset($result['Contents'][0]['Key']) && $result['Contents'][0]['Key'] === rtrim($path, '/') . '/') if (isset($result['Contents'])) {
|| isset($result['CommonPrefixes'])) { $this->directoryCache[$path] = true;
$this->directoryCache[$path] = true; return true;
} else {
$this->directoryCache[$path] = false;
}
} catch (S3Exception $e) {
if ($e->getStatusCode() === 403) {
$this->directoryCache[$path] = false;
}
throw $e;
} }
// empty directories have their own object
$object = $this->headObject($path);
if ($object) {
$this->directoryCache[$path] = true;
return true;
}
} catch (S3Exception $e) {
if ($e->getStatusCode() === 403) {
$this->directoryCache[$path] = false;
}
throw $e;
} }
return $this->directoryCache[$path];
$this->directoryCache[$path] = false;
return false;
} }
/** /**
@ -284,7 +308,9 @@ class AmazonS3 extends \OC\Files\Storage\Common {
protected function clearBucket() { protected function clearBucket() {
$this->clearCache(); $this->clearCache();
try { try {
$this->getConnection()->clearBucket($this->bucket); $this->getConnection()->clearBucket([
"Bucket" => $this->bucket
]);
return true; return true;
// clearBucket() is not working with Ceph, so if it fails we try the slower approach // clearBucket() is not working with Ceph, so if it fails we try the slower approach
} catch (\Exception $e) { } catch (\Exception $e) {
@ -318,7 +344,9 @@ class AmazonS3 extends \OC\Files\Storage\Common {
} }
// we reached the end when the list is no longer truncated // we reached the end when the list is no longer truncated
} while ($objects['IsTruncated']); } while ($objects['IsTruncated']);
$this->deleteObject($path); if ($path !== '' && $path !== null) {
$this->deleteObject($path);
}
} catch (S3Exception $e) { } catch (S3Exception $e) {
\OC::$server->getLogger()->logException($e, ['app' => 'files_external']); \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
return false; return false;
@ -327,54 +355,12 @@ class AmazonS3 extends \OC\Files\Storage\Common {
} }
public function opendir($path) { public function opendir($path) {
$path = $this->normalizePath($path);
if ($this->isRoot($path)) {
$path = '';
} else {
$path .= '/';
}
try { try {
$files = []; $content = iterator_to_array($this->getDirectoryContent($path));
$results = $this->getConnection()->getPaginator('ListObjects', [ return IteratorDirectory::wrap(array_map(function (array $item) {
'Bucket' => $this->bucket, return $item['name'];
'Delimiter' => '/', }, $content));
'Prefix' => $path, } catch (S3Exception $e) {
]);
foreach ($results as $result) {
// sub folders
if (is_array($result['CommonPrefixes'])) {
foreach ($result['CommonPrefixes'] as $prefix) {
$directoryName = trim($prefix['Prefix'], '/');
$files[] = substr($directoryName, strlen($path));
$this->directoryCache[$directoryName] = true;
}
}
if (is_array($result['Contents'])) {
foreach ($result['Contents'] as $object) {
if (isset($object['Key']) && $object['Key'] === $path) {
// it's the directory itself, skip
continue;
}
$file = basename(
isset($object['Key']) ? $object['Key'] : $object['Prefix']
);
$files[] = $file;
// store this information for later usage
$this->filesCache[$path . $file] = [
'ContentLength' => $object['Size'],
'LastModified' => (string)$object['LastModified'],
];
}
}
}
return IteratorDirectory::wrap($files);
} catch (S3Exception $e) {
\OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
return false; return false;
} }
} }
@ -382,33 +368,19 @@ class AmazonS3 extends \OC\Files\Storage\Common {
public function stat($path) { public function stat($path) {
$path = $this->normalizePath($path); $path = $this->normalizePath($path);
try { if ($this->is_dir($path)) {
$stat = []; $stat = $this->getDirectoryMetaData($path);
if ($this->is_dir($path)) { } else {
$cacheEntry = $this->getCache()->get($path); $object = $this->headObject($path);
if ($cacheEntry instanceof CacheEntry) { if ($object === false) {
$stat['size'] = $cacheEntry->getSize(); return false;
$stat['mtime'] = $cacheEntry->getMTime();
} else {
// Use dummy values
$stat['size'] = -1; // Pending
$stat['mtime'] = time();
}
} else {
$stat['size'] = $this->getContentLength($path);
$stat['mtime'] = strtotime($this->getLastModified($path));
} }
$stat['atime'] = time(); $object["Key"] = $path;
$stat = $this->objectToMetaData($object);
return $stat;
} catch (S3Exception $e) {
\OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
return false;
} }
} $stat['atime'] = time();
public function hasUpdated($path, $time) { return $stat;
return $this->getMountOption('filesystem_check_changes', 1) === 1 || parent::hasUpdated($path, $time);
} }
/** /**
@ -711,4 +683,83 @@ class AmazonS3 extends \OC\Files\Storage\Common {
public static function checkDependencies() { public static function checkDependencies() {
return true; return true;
} }
public function getDirectoryContent($directory): \Traversable {
$path = $this->normalizePath($directory);
if ($this->isRoot($path)) {
$path = '';
} else {
$path .= '/';
}
$results = $this->getConnection()->getPaginator('ListObjectsV2', [
'Bucket' => $this->bucket,
'Delimiter' => '/',
'Prefix' => $path,
]);
foreach ($results as $result) {
// sub folders
if (is_array($result['CommonPrefixes'])) {
foreach ($result['CommonPrefixes'] as $prefix) {
$dir = $this->getDirectoryMetaData($prefix['Prefix']);
if ($dir) {
yield $dir;
}
}
}
if (is_array($result['Contents'])) {
foreach ($result['Contents'] as $object) {
$this->objectCache[$object['Key']] = $object;
if ($object['Key'] !== $path) {
yield $this->objectToMetaData($object);
}
}
}
}
}
private function objectToMetaData(array $object): array {
return [
'name' => basename($object['Key']),
'mimetype' => $this->mimeDetector->detectPath($object['Key']),
'mtime' => strtotime($object['LastModified']),
'storage_mtime' => strtotime($object['LastModified']),
'etag' => $object['ETag'],
'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE,
'size' => (int)($object['Size'] ?? $object['ContentLength']),
];
}
private function getDirectoryMetaData(string $path): ?array {
$path = trim($path, '/');
// when versioning is enabled, delete markers are returned as part of CommonPrefixes
// resulting in "ghost" folders, verify that each folder actually exists
if ($this->versioningEnabled() && !$this->doesDirectoryExist($path)) {
return null;
}
$cacheEntry = $this->getCache()->get($path);
if ($cacheEntry instanceof CacheEntry) {
return $cacheEntry->getData();
} else {
return [
'name' => basename($path),
'mimetype' => 'httpd/unix-directory',
'mtime' => time(),
'storage_mtime' => time(),
'etag' => uniqid(),
'permissions' => Constants::PERMISSION_ALL,
'size' => -1,
];
}
}
public function versioningEnabled(): bool {
if ($this->versioningEnabled === null) {
$result = $this->getConnection()->getBucketVersioning(['Bucket' => $this->getBucket()]);
$this->versioningEnabled = $result->get('Status') === 'Enabled';
}
return $this->versioningEnabled;
}
} }

@ -65,7 +65,7 @@ trait S3ObjectTrait {
} }
$opts = [ $opts = [
'http' => [ 'http' => [
'protocol_version' => 1.1, 'protocol_version' => $request->getProtocolVersion(),
'header' => $headers, 'header' => $headers,
], ],
]; ];