Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Create the Container/AWS Bucket if it does not exist. #1327

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions core/src/plugins/access.s3/S3AccessDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,23 @@ public function getS3Service(){
*/
public function directoryUsage(AJXP_Node $node){
$client = $this->getS3Service();
$bucket = $node->getRepository()->getContextOption($node->getContext(), "CONTAINER"); //(isSet($repositoryResolvedOptions["CONTAINER"])?$repositoryResolvedOptions["CONTAINER"]:$this->repository->getOption("CONTAINER"));
$path = rtrim($node->getRepository()->getContextOption($node->getContext(), "PATH"), "/").$node->getPath(); //(isSet($repositoryResolvedOptions["PATH"])?$repositoryResolvedOptions["PATH"]:"");
$objects = $client->getIterator('ListObjects', array(
'Bucket' => $bucket,
'Prefix' => $path
));

$usage = 0;
foreach ($objects as $object) {
$usage += (double)$object['Size'];
$bucket = $node->getRepository()->getContextOption($node->getContext(), "CONTAINER"); //(isSet($repositoryResolvedOptions["CONTAINER"])?$repositoryResolvedOptions["CONTA$
$path = rtrim($node->getRepository()->getContextOption($node->getContext(), "PATH"), "/").$node->getPath(); //(isSet($repositoryResolvedOptions["PATH"])?$repositoryRes$

$usage = 0;
if(!empty($client)){
$objects = $client->getIterator('ListObjects', array(
'Bucket' => $bucket,
'Prefix' => $path
));


foreach ($objects as $object) {
$usage += (double)$object['Size'];
}
}
return $usage;

return $usage;

}

Expand Down
23 changes: 23 additions & 0 deletions core/src/plugins/access.s3/S3AccessWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ protected static function getClientForContext(ContextInterface $ctx, $registerSt
require_once("S3Client.php");
$s3Client = new S3Client($config, $repoObject->getId());
$s3Client->registerStreamWrapper();

//This attempts to create the bucket if it does not exist, which removes the need for an admin
//to have to have access to the aws system to create the bucket prior to creating a new workspace
$container = $repoObject->getContextOption($ctx, "CONTAINER");
$region = $repoObject->getContextOption($ctx, "REGION");
$ctr_exists = $s3Client->doesBucketExist($container,true, config);

if(!empty($container)){
if($ctr_exists != 1){
try{
// Create a valid bucket and use a LocationConstraint
$result = $s3Client->createBucket(array(
'Bucket' => $container,
'LocationConstraint' => $region,
));


} catch (Exception $e) {
//Return a simple message rather than aws standard error message

}
}
}
self::$clients[$repoObject->getId()] = $s3Client;
}
return self::$clients[$repoObject->getId()];
Expand Down