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

Commit

Permalink
Change S3 clients instanciation.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Jul 4, 2016
1 parent 29aad73 commit 964b33d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 77 deletions.
11 changes: 9 additions & 2 deletions core/src/plugins/access.s3/class.pydioS3Client.php
Expand Up @@ -24,14 +24,21 @@
use Aws\S3\StreamWrapper;
require_once __DIR__ . DIRECTORY_SEPARATOR . "class.s3CacheService.php";
defined('AJXP_EXEC') or die( 'Access not allowed');

/**
* Class S3Client
* @package AccessS3
*/
class S3Client extends AwsS3Client
{
/**
* Register a new stream wrapper who overwrite the Amazon S3 stream wrapper with this client instance.
* @param string $repositoryId
* @return $this|void
*/
public function registerStreamWrapper()
public function registerStreamWrapper($repositoryId)
{
/* S3Client + s3 protocol + cacheInterface */
StreamWrapper::register($this, "s3", new s3CacheService());
StreamWrapper::register($this, "s3.".$repositoryId, new s3CacheService());
}
}
3 changes: 2 additions & 1 deletion core/src/plugins/access.s3/class.s3AccessDriver.php
Expand Up @@ -52,7 +52,7 @@ public function performChecks()
* Override parent to register underlying wrapper (s3) as well
*/
public function detectStreamWrapper($register = false){

/*
if(isSet($this->repository)){
require_once("aws.phar");
$options = array(
Expand Down Expand Up @@ -99,6 +99,7 @@ public function detectStreamWrapper($register = false){
$this->s3Client->registerStreamWrapper();
}
*/
return parent::detectStreamWrapper($register);
}

Expand Down
183 changes: 109 additions & 74 deletions core/src/plugins/access.s3/class.s3AccessWrapper.php
Expand Up @@ -31,13 +31,80 @@
class s3AccessWrapper extends fsAccessWrapper
{
public static $lastException;
protected static $clients = [];

/**
* @param Repository $repoObject
* @param boolean $registerStream
* @return \AccessS3\S3Client
*/
protected static function getClientForRepository($repoObject, $registerStream = true)
{
require_once("aws.phar");
if (!isSet(self::$clients[$repoObject->getId()])) {
// Get a client
$options = array(
'key' => $repoObject->getOption("API_KEY"),
'secret' => $repoObject->getOption("SECRET_KEY")
);
$signatureVersion = $repoObject->getOption("SIGNATURE_VERSION");
if (!empty($signatureVersion)) {
$options['signature'] = $signatureVersion;
}
$baseURL = $repoObject->getOption("STORAGE_URL");
if (!empty($baseURL)) {
$options["base_url"] = $baseURL;
}
$region = $repoObject->getOption("REGION");
if (!empty($region)) {
$options["region"] = $region;
}
$proxy = $repoObject->getOption("PROXY");
if (!empty($proxy)) {
$options['request.options'] = array('proxy' => $proxy);
}
$apiVersion = $repoObject->getOption("API_VERSION");
if ($apiVersion === "") {
$apiVersion = "latest";
}
//SDK_VERSION IS A GLOBAL PARAM
ConfService::getConfStorageImpl()->_loadPluginConfig("access.s3", $globalOptions);
$sdkVersion = $globalOptions["SDK_VERSION"]; //$repoObject->driverInstance->driverConf['SDK_VERSION'];
if ($sdkVersion !== "v2" && $sdkVersion !== "v3") {
$sdkVersion = "v2";
}
if ($sdkVersion === "v3") {
require_once(__DIR__ . DIRECTORY_SEPARATOR . "class.pydioS3Client.php");
$s3Client = new \AccessS3\S3Client([
"version" => $apiVersion,
"region" => $region,
"credentials" => $options
]);
$s3Client->registerStreamWrapper($repoObject->getId());
} else {
$s3Client = Aws\S3\S3Client::factory($options);
if ($repoObject->getOption("VHOST_NOT_SUPPORTED")) {
// Use virtual hosted buckets when possible
require_once("ForcePathStyleListener.php");
$s3Client->addSubscriber(new \Aws\S3\ForcePathStyleStyleListener());
}
$s3Client->registerStreamWrapper();
}
self::$clients[$repoObject->getId()] = $s3Client;
}
return self::$clients[$repoObject->getId()];
}

/**
* Initialize the stream from the given path.
* Concretely, transform ajxp.s3:// into s3://
*
* @param string $path
* @param $streamType
* @param bool $storeOpenContext
* @param bool $skipZip
* @return mixed Real path or -1 if currentListing contains the listing : original path converted to real path
* @throws Exception
*/
protected static function initPath($path, $streamType, $storeOpenContext = false, $skipZip = false)
{
Expand All @@ -49,12 +116,18 @@ protected static function initPath($path, $streamType, $storeOpenContext = false
self::$lastException = $e;
throw $e;
}
// Make sure to register s3:// wrapper
$client = self::getClientForRepository($repoObject, true);
$protocol = "s3://";
if ($client instanceof \AccessS3\S3Client) {
$protocol = "s3." . $repoId . "://";
}
$basePath = $repoObject->getOption("PATH");
$baseContainer = $repoObject->getOption("CONTAINER");
if(!empty($basePath)){
$baseContainer.=rtrim($basePath, "/");
}
$p = "s3://".$baseContainer.str_replace("//", "/", $url["path"]);
$p = $protocol . $baseContainer . str_replace("//", "/", $url["path"]);
return $p;
}

Expand All @@ -64,9 +137,10 @@ protected static function initPath($path, $streamType, $storeOpenContext = false
*
* @param String $path Maybe in the form "ajxp.fs://repositoryId/pathToFile"
* @param String $mode
* @param unknown_type $options
* @param unknown_type $opened_path
* @return unknown
* @param string $options
* @param resource $context
* @return resource
* @internal param string $opened_path
*/
public function stream_open($path, $mode, $options, &$context)
{
Expand All @@ -89,9 +163,9 @@ public function stream_open($path, $mode, $options, &$context)
* Stats the given path.
* Fix PEAR by adding S_ISREG mask when file case.
*
* @param unknown_type $path
* @param unknown_type $flags
* @return unknown
* @param string $path
* @param integer $flags
* @return array
*/
public function url_stat($path, $flags)
{
Expand All @@ -109,19 +183,16 @@ public function url_stat($path, $flags)
//return null;
}
return $stat;

// Non existing file
return null;
}

/**
* Opens a handle to the dir
* Fix PEAR by being sure it ends up with "/", to avoid
* adding the current dir to the children list.
*
* @param unknown_type $path
* @param unknown_type $options
* @return unknown
* @param string $path
* @param string $options
* @return resource
*/
public function dir_opendir ($path , $options )
{
Expand All @@ -147,17 +218,9 @@ public static function removeTmpFile($tmpDir, $tmpFile)
if(is_dir($tmpDir)) rmdir($tmpDir);
}

protected static function closeWrapper()
{
if (self::$crtZip != null) {
self::$crtZip = null;
self::$currentListing = null;
self::$currentListingKeys = null;
self::$currentListingIndex = null;
self::$currentFileKey = null;
}
}

/**
* @inheritdoc
*/
public static function getRealFSReference($path, $persistent = false)
{
$tmpFile = AJXP_Utils::getAjxpTmpDir()."/".md5(time()).".".pathinfo($path, PATHINFO_EXTENSION);
Expand All @@ -170,12 +233,17 @@ public static function getRealFSReference($path, $persistent = false)
return $tmpFile;
}


/**
* @inheritdoc
*/
public static function isRemote()
{
return true;
}

/**
* @inheritdoc
*/
public static function copyFileInStream($path, $stream)
{
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"Should load ".$path);
Expand All @@ -188,9 +256,18 @@ public static function copyFileInStream($path, $stream)
fclose($fp);
}

public static function changeMode($path, $chmodValue){}
/**
* @inheritdoc
*/
public static function changeMode($path, $chmodValue)
{
}

public function rename($from, $to){
/**
* @inheritdoc
*/
public function rename($from, $to)
{

$fromUrl = parse_url($from);
$repoId = $fromUrl["host"];
Expand All @@ -207,7 +284,7 @@ public function rename($from, $to){

if($isDir === true || is_dir($from)){
AJXP_Logger::debug(__CLASS__, __FUNCTION__, "S3 Renaming dir $from to $to");
require_once("aws.phar");
require_once("aws-v2.phar");

$fromUrl = parse_url($from);
$repoId = $fromUrl["host"];
Expand All @@ -217,52 +294,7 @@ public function rename($from, $to){
self::$lastException = $e;
throw $e;
}
// Get a client
$options = array(
'key' => $repoObject->getOption("API_KEY"),
'secret' => $repoObject->getOption("SECRET_KEY")
);
$signatureVersion = $repoObject->getOption("SIGNATURE_VERSION");
if(!empty($signatureVersion)){
$options['signature'] = $signatureVersion;
}
$baseURL = $repoObject->getOption("STORAGE_URL");
if(!empty($baseURL)){
$options["base_url"] = $baseURL;
}
$region = $repoObject->getOption("REGION");
if(!empty($region)){
$options["region"] = $region;
}
$proxy = $repoObject->getOption("PROXY");
if(!empty($proxy)){
$options['request.options'] = array('proxy' => $proxy);
}
$apiVersion = $repoObject->getOption("API_VERSION");
if ($apiVersion === "") {
$apiVersion = "latest";
}
//SDK_VERSION IS A GLOBAL PARAM
$sdkVersion = $repoObject->driverInstance->driverConf['SDK_VERSION'];
if ($sdkVersion !== "v2" && $sdkVersion !== "v3") {
$sdkVersion = "v2";
}
if ($sdkVersion === "v3") {
require_once ("class.pydioS3Client.php");
$s3Client = new AccessS3\S3Client([
"version" => $apiVersion,
"region" => $region,
"credentials" => $options
]);
} else {
$s3Client = Aws\S3\S3Client::factory($options);
if($repoObject->getOption("VHOST_NOT_SUPPORTED")){
// Use virtual hosted buckets when possible
require_once("ForcePathStyleListener.php");
$s3Client->addSubscriber(new \Aws\S3\ForcePathStyleStyleListener());
}
}

$s3Client = self::getClientForRepository($repoObject, false);
$bucket = $repoObject->getOption("CONTAINER");
$basePath = $repoObject->getOption("PATH");
$fromKeyname = trim(str_replace("//", "/", $basePath.parse_url($from, PHP_URL_PATH)),'/');
Expand All @@ -278,6 +310,7 @@ public function rename($from, $to){

// Perform a batch of CopyObject operations.
$batch = array();
$failed = array();
$iterator = $s3Client->getIterator('ListObjects', array(
'Bucket' => $bucket,
'Prefix' => $fromKeyname."/"
Expand All @@ -302,6 +335,8 @@ public function rename($from, $to){
$toDelete[] = $currentFrom;
}
AJXP_Logger::debug(__CLASS__, __FUNCTION__, "S3 Execute batch on ".count($batch)." objects");
ConfService::getConfStorageImpl()->_loadPluginConfig("access.s3", $globalOptions);
$sdkVersion = $globalOptions["SDK_VERSION"];
if ($sdkVersion === "v3") {
foreach ($batch as $command) {
$successful = $s3Client->execute($command);
Expand Down

0 comments on commit 964b33d

Please sign in to comment.