Update 1: New functions, Error Handling, Unix Time & More

This is the first update!
-> Two new functions allow for easily making files public or private.
-> Files' public or private status can now be set while uploading.
-> Added error handling capabilities.
-> All time values are automatically converted to Unix time.
-> Result objects are automatically converted to arrays.
-> Fixed a bug with ACLs where a different client was being called.
-> Fixed a bug with file uploads where save as was not working.
This commit is contained in:
Devang Srivastava 2018-01-03 23:21:33 +05:30 committed by Devang Srivastava
parent 3c5b9c790e
commit 2557aef44f

View file

@ -11,20 +11,24 @@ class SpacesConnect {
function __construct($access_key, $secret_key, $spaceName = "", $region = "nyc3", $host = "digitaloceanspaces.com") { function __construct($access_key, $secret_key, $spaceName = "", $region = "nyc3", $host = "digitaloceanspaces.com") {
if(!empty($spaceName)) { if(!empty($spaceName)) {
$endpoint = "https://".$spaceName.".".$region.".".$host; $endpoint = "https://".$spaceName.".".$region.".".$host;
} }
else { else {
$endpoint = "https://".$region.".".$host; $endpoint = "https://".$region.".".$host;
} }
$this->client = Aws\S3\S3Client::factory(array( try {
'region' => $region, $this->client = Aws\S3\S3Client::factory(array(
'version' => 'latest', 'region' => $region,
'endpoint' => $endpoint, 'version' => 'latest',
'credentials' => array( 'endpoint' => $endpoint,
'key' => $access_key, 'credentials' => array(
'secret' => $secret_key, 'key' => $access_key,
), 'secret' => $secret_key,
'bucket_endpoint' => true ),
)); 'bucket_endpoint' => true
));
} catch (\Exception $e) {
$this->HandleAWSException($e);
}
$this->space = $spaceName; $this->space = $spaceName;
$this->access_key = $access_key; $this->access_key = $access_key;
$this->secret_key = $secret_key; $this->secret_key = $secret_key;
@ -39,23 +43,31 @@ class SpacesConnect {
$region = $this->region; $region = $this->region;
} }
$current_space = $this->space; $current_space = $this->space;
$this->setSpace($spaceName); try {
$success = $this->client->createBucket(array('Bucket' => $spaceName)); $this->setSpace($spaceName);
$this->client->waitUntil('BucketExists', array('Bucket' => $spaceName)); $success = $this->client->createBucket(array('Bucket' => $spaceName));
$this->setSpace($current_space); $this->client->waitUntil('BucketExists', array('Bucket' => $spaceName));
return $success; $this->setSpace($current_space);
return $this->ObjReturn($success->toArray());
} catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function listSpaces() { function listSpaces() {
$current_space = $this->space; $current_space = $this->space;
$this->setSpace(NULL); try {
$spaceList = $this->client->listBuckets(); $this->setSpace(NULL);
$this->setSpace($current_space); $spaceList = $this->client->listBuckets();
return $spaceList; $this->setSpace($current_space);
return $this->ObjReturn($spaceList->toArray());
} catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function ChangeSpace($spaceName, $region = "", $host = "") { function ChangeSpace($spaceName, $region = "", $host = "") {
setSpace($spaceName, $region, $host); return setSpace($spaceName, $region, $host);
} }
function setSpace($spaceName, $region = "", $host = "") { function setSpace($spaceName, $region = "", $host = "") {
@ -68,34 +80,50 @@ class SpacesConnect {
$endpoint = "https://".$region.".".$host; $endpoint = "https://".$region.".".$host;
$this->space = ""; $this->space = "";
} }
$this->client = Aws\S3\S3Client::factory(array( try {
'region' => $region, $this->client = Aws\S3\S3Client::factory(array(
'version' => 'latest', 'region' => $region,
'endpoint' => $endpoint, 'version' => 'latest',
'credentials' => array( 'endpoint' => $endpoint,
'key' => $this->access_key, 'credentials' => array(
'secret' => $this->secret_key, 'key' => $this->access_key,
), 'secret' => $this->secret_key,
'bucket_endpoint' => true ),
)); 'bucket_endpoint' => true
));
return $this->ObjReturn(true);
} catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function getSpaceName() { function getSpaceName() {
return $this->space; return $this->ObjReturn($this->space);
} }
function downloadSpaceToDirectory($pathToDirectory) { function downloadSpaceToDirectory($pathToDirectory) {
try {
$this->client->downloadBucket($pathToDirectory, $this->space); $this->client->downloadBucket($pathToDirectory, $this->space);
return $this->ObjReturn(true);
} catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function destroyThisSpace() { function destroyThisSpace() {
$objects = $this->listObjects(); try {
foreach ($objects as $value) { $objects = $this->listObjects();
$key = $value["Key"]; foreach ($objects as $value) {
$this->deleteObject($key); $key = $value["Key"];
} $this->deleteObject($key);
$this->client->deleteBucket(array('Bucket' => $this->space)); }
$this->client->waitUntil('BucketNotExists', array('Bucket' => $this->space)); $this->client->deleteBucket(array('Bucket' => $this->space));
$this->client->waitUntil('BucketNotExists', array('Bucket' => $this->space));
return $this->ObjReturn(true);
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
@ -103,63 +131,124 @@ class SpacesConnect {
function listObjects() { function listObjects() {
$objects = $this->client->getIterator('ListObjects', array( try {
'Bucket' => $this->space $objects = $this->client->getIterator('ListObjects', array(
)); 'Bucket' => $this->space
$objectArray = array(); ));
foreach ($objects as $object) { $objectArray = array();
$objectArray[] = $object; foreach ($objects as $object) {
} $objectArray[] = $object;
return $objectArray; }
return $this->ObjReturn($objectArray);
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function doesObjectExist($objectName) { function doesObjectExist($objectName) {
return $this->client->doesObjectExist($this->space, $objectName); try {
return $this->ObjReturn($this->client->doesObjectExist($this->space, $objectName));
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function getObject($file_name = "") { function getObject($file_name = "") {
$result = $this->client->getObject([ try {
'Bucket' => $this->space, $result = $this->client->getObject([
'Key' => $file_name, 'Bucket' => $this->space,
]); 'Key' => $file_name,
return $result; ]);
return $this->ObjReturn($result->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
}
function makePrivate($file_path = "") {
try {
return $this->PutObjectACL($file_path, ["ACL" => "private"]);
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
}
function makePublic($file_path = "") {
try {
return $this->PutObjectACL($file_path, ["ACL" => "public-read"]);
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function deleteObject($file_path = "") { function deleteObject($file_path = "") {
return $this->client->deleteObject([ try {
return $this->ObjReturn($this->client->deleteObject([
'Bucket' => $this->space, 'Bucket' => $this->space,
'Key' => $file_path, 'Key' => $file_path,
]); ])->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function uploadFile($pathToFile, $fileName = "") { function uploadFile($pathToFile, $access = "private", $fileName = "") {
if(empty($filename)) { if(empty($fileName)) {
$fileName = $pathToFile; $fileName = $pathToFile;
} }
$result = $this->client->putObject(array( if($access == "public") {
'Bucket' => $this->space, $access = "public-read";
'Key' => $fileName, }
'Body' => fopen($pathToFile, 'r+') try {
)); $result = $this->client->putObject(array(
'Bucket' => $this->space,
'Key' => $fileName,
'Body' => fopen($pathToFile, 'r+'),
"ACL" => $access
));
$this->client->waitUntil('ObjectExists', array( $this->client->waitUntil('ObjectExists', array(
'Bucket' => $this->space, 'Bucket' => $this->space,
'Key' => $fileName 'Key' => $fileName
)); ));
return $result; return $this->ObjReturn($result->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function downloadFile($fileName, $destinationPath) { function downloadFile($fileName, $destinationPath) {
try {
$result = $this->client->getObject(array( $result = $this->client->getObject(array(
'Bucket' => $this->space, 'Bucket' => $this->space,
'Key' => $fileName, 'Key' => $fileName,
'SaveAs' => $destinationPath 'SaveAs' => $destinationPath
)); ));
return $this->ObjReturn($result->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function uploadDirectory($directory, $keyPrefix = "") { function uploadDirectory($directory, $keyPrefix = "") {
try {
$this->client->uploadDirectory($directory, $this->space, $keyPrefix); $this->client->uploadDirectory($directory, $this->space, $keyPrefix);
return $this->ObjReturn($result->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
@ -168,10 +257,15 @@ class SpacesConnect {
function listCORS() { function listCORS() {
$cors = $this->client->getBucketCors([ try {
'Bucket' => $this->space, $cors = $this->client->getBucketCors([
]); 'Bucket' => $this->space,
return $cors; ]);
return $this->ObjReturn($cors->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function putCORS($cors_rules = "") { function putCORS($cors_rules = "") {
@ -182,37 +276,63 @@ class SpacesConnect {
'ExposeHeaders' => ['Access-Control-Allow-Origin'], 'ExposeHeaders' => ['Access-Control-Allow-Origin'],
]; ];
} }
$result = $this->client->putBucketCors([ try {
'Bucket' => $this->space, $result = $this->client->putBucketCors([
'CORSConfiguration' => ['CORSRules' => [$cors_rules]] 'Bucket' => $this->space,
]); 'CORSConfiguration' => ['CORSRules' => [$cors_rules]]
return $result; ]);
return $this->ObjReturn($result->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function listSpaceACL() { function listSpaceACL() {
$acl = $this->client->getBucketAcl([ try {
'Bucket' => $this->space, $acl = $this->client->getBucketAcl([
]); 'Bucket' => $this->space,
return $acl; ]);
return $this->ObjReturn($acl->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function PutSpaceACL($params) { function PutSpaceACL($params) {
$acl = $s3Client->putBucketAcl($params); try {
return $acl; $acl = $this->client->putBucketAcl($params);
return $this->ObjReturn($acl->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function listObjectACL($file) { function listObjectACL($file) {
$result = $client->getObjectAcl([ try {
$result = $this->client->getObjectAcl([
'Bucket' => $this->space, 'Bucket' => $this->space,
'Key' => $file, 'Key' => $file,
]); ]);
return $result; return $this->ObjReturn($result->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
function PutObjectACL($file, $acl) { function PutObjectACL($file, $acl) {
$acl = array_merge (array("Bucket" => $this->bucket, "Key" => $file), $acl); try {
$result = $client->putObjectAcl($acl); $acl = array_merge(array("Bucket" => $this->space, "Key" => $file), $acl);
$result = $this->client->putObjectAcl($acl);
return $this->ObjReturn($result->toArray());
}
catch (\Exception $e) {
$this->HandleAWSException($e);
}
} }
@ -239,6 +359,69 @@ class SpacesConnect {
'Signature' => $signature, 'Signature' => $signature,
)); ));
$url .= "?".$queries; $url .= "?".$queries;
return $url; return $this->ObjReturn($url);
}
function ObjReturn($return) {
$return = @json_decode(@json_encode($return), true);
$return = $this->AWSTime($return);
return $return;
}
function AWSTime($obj) {
$time_keys = ["LastModified", "CreationDate", "Expires", "last-modified", "date", "Expiration"];
if(is_array($obj)) {
foreach ($obj as $key => $value) {
if(is_array($obj[$key])) {
$obj[$key] = $this->AWSTime($obj[$key]);
}
else {
foreach ($time_keys as $time_key) {
if(array_key_exists($time_key, $obj) && !empty($obj[$time_key]) && !is_numeric($obj[$time_key])) {
$obj[$time_key] = strtotime($obj[$time_key]);
}
}
}
}
}
return $obj;
}
private function any_key_exists($keys, $arr) {
foreach ($keys as $key) {
if(array_key_exists($key, $arr)) {
return true;
}
}
return false;
}
function HandleAWSException($e) {
if(get_class($e) == "Aws\S3\Exception\S3Exception") {
$error["error"] = [
"message" => $e->getAwsErrorMessage(),
"code" => $e->getAwsErrorCode(),
"type" => $e->getAwsErrorType(),
"http_code" => $e->getStatusCode(),
];
}
else {
throw $e;
}
throw new SpacesAPIException(@json_encode($error));
}
function GetError($e) {
$error = @json_decode($e->getMessage(), true);
return $error["error"];
}
}
class SpacesAPIException extends \Exception {
public function __construct($message, $code = 0, Exception $previous = null) {
parent::__construct($message, $code, $previous);
} }
} }