GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Spaces-API/SpacesAPI/Spaces.php at master · SociallyDev/Spaces-API Expand file tree/ Copy pathSpaces.phpMore file actions 105 lines (94 loc) · 3.32 KB / Copy pathSpaces.phpFile metadata and controls105 lines (94 loc) · 3.32 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 <?php namespace SpacesAPI; use Aws\S3\Exception\S3Exception; use Aws\S3\S3Client; use SpacesAPI\Exceptions\AuthenticationException; use SpacesAPI\Exceptions\SpaceExistsException; /** * Represents the connection to Digital Ocean spaces. * The entry point for managing spaces. * * Instantiate your connection with `new \SpacesAPI\Spaces("access-key", "secret-key", "region")` * * Obtain your access and secret keys from the [DigitalOcean Applications & API dashboard](https://cloud.digitalocean.com/account/api/tokens) */ class Spaces { /** * @var \Aws\S3\S3Client */ private $s3; /** * Initialise the API * * @param string $accessKey Digital Ocean API access key * @param string $secretKey Digital Ocean API secret key * @param string $region Region, defaults to ams3 * @param string $host API endpoint, defaults to digitaloceanspaces.com * * @throws \SpacesAPI\Exceptions\AuthenticationException Authentication failed */ public function __construct(string $accessKey, string $secretKey, string $region = "ams3", string $host = "digitaloceanspaces.com") { $this->s3 = new S3Client([ "version" => "latest", "region" => $region, "endpoint" => "https://$region.$host", "credentials" => ["key" => $accessKey, "secret" => $secretKey], "ua_append" => "SociallyDev-Spaces-API/2", ]); try { $this->s3->headBucket(["Bucket" => 'auth-check']); } catch (S3Exception $e) { if ($e->getStatusCode() == 403) { throw new AuthenticationException("Authentication failed"); } } } /** * List all your spaces * * @return array An array of \SpacesAPI\Space instances */ public function list(): array { $spaces = []; foreach (Result::parse($this->s3->listBuckets()['Buckets']) as $bucket) { $spaces[$bucket['Name']] = new Space($this->s3, $bucket['Name'], false); } return $spaces; } /** * Create a new space * * @param string $name The name of the new space * @param bool $public Enable file listing. Default `false` * * @return \SpacesAPI\Space The newly created space * @throws \SpacesAPI\Exceptions\SpaceExistsException The named space already exists */ public function create(string $name, bool $public = false): Space { try { $this->s3->createBucket([ "ACL" => ($public) ? "public-read" : "private", "Bucket" => $name, ]); } catch (S3Exception $e) { throw new SpaceExistsException($e->getAwsErrorMessage()); } return new Space($this->s3, $name, false); } /** * Use an existing space * * @param string $name The name of the space * * @return \SpacesAPI\Space The loaded space * @throws \SpacesAPI\Exceptions\SpaceDoesntExistException The named space doesn't exist */ public function space(string $name): Space { return new Space($this->s3, $name); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |