anton-am/do-spaces-api

一个用于DigitalOcean Spaces对象存储的API包装器,易于使用。

1.0.5 2021-05-21 08:54 UTC

This package is auto-updated.

Last update: 2024-09-27 21:29:43 UTC


README

FOSSA Status

一个用于DigitalOcean Spaces对象存储的API包装器,易于使用。

安装

  • 使用Composer:
composer require sociallydev/spaces-api:dev-master

连接

//Either:
require_once("spaces.php");
//OR COMPOSER:
require_once("vendor/autoload.php"); //Install first by executing: composer require SociallyDev/Spaces-API in your project's directory.

$key = "EXAMPLE_KEY";
$secret = "EXAMPLE_SECRET";

$space_name = "my-space";
$region = "nyc3";

$space = new SpacesConnect($key, $secret, $space_name, $region);

所有可用选项

SpacesConnect(必需的密钥,必需的秘密,可选的Spaces名称,可选的区域,可选的主机);

 

上传/下载文件

// Don't start any path with a forward slash, or it will give "SignatureDoesNotMatch" exception
$path_to_file = "image.png";

$space->UploadFile($path_to_file, "public");



$download_file = "image.png";
$save_as = "folder/downloaded-image.png";

$space->DownloadFile($download_file, $save_as);

所有可用选项

UploadFile(必需的文件路径,可选的隐私(公共|私有)可选的保存文件名称);
DownloadFile(必需的下载文件,必需的保存位置);

 

删除文件/文件夹

$file_name = "image.png";

$space->DeleteObject($file_name);

所有可用选项

DeleteObject(必需的文件或文件夹删除,可选的递归(false|true));

 

更改隐私设置

$file = "image.png";

$space->MakePublic($file);
$space->MakePrivate($file);

所有可用选项

MakePublic(必需的文件路径);
MakePrivate(必需的文件路径);

 

创建临时链接

$file = "image.png";
$valid_for = "1 day";

$link = $space->CreateTemporaryURL($file, $valid_for);

所有可用选项

CreateTemporaryURL(必需的文件名,可选的链接有效时间);

   

其他文件API

//List all files and folders
$files = $space->ListObjects();


//Check if a file/folder by that name already exists. True/False.
$space->DoesObjectExist($file_name);


//Pull information about a single object.
$file_info = $space->GetObject($file_name);


//Upload a complete directory instead of a single file.
$space->UploadDirectory($path_to_directory, $key_prefix);


//Pull Access Control List information.
$acl = $space-ListObjectACL($file_name);


//Update Access Control List information.
$space->PutObjectACL($file_name, $acl_info_array);

         

创建Spaces

$new_space = "my-new-space";

$space->CreateSpace($new_space);

所有可用选项

CreateSpace(必需的Spaces名称,可选的Spaces区域);

 

切换Spaces

$new_space = "my-new-space";

$space->SetSpace($new_space);

所有可用选项

SetSpace(必需的Spaces名称,可选的Spaces区域,可选的主机);

   

其他Spaces API

//List all Spaces available in account.
$spaces = $space->ListSpaces();


//Delete a Space.
$space->DestroyThisSpace();


//Download whole Space to a folder.
$space->DownloadSpaceToDirectory($directory_to_download_to);


//Get the name of the current Space.
$space_name = $space->GetSpaceName();


//Pull the CORS policy of the Space.
$cors = $space->ListCORS();


//Update the CORS policy of the Space.
$space->PutCORS($new_policy);


//Pull the Access Control List information of the Space.
$acl = $space->ListSpaceACL();


//Update the Access Control List information of the Space.
$space->PutSpaceACL($new_acl);

处理错误

try {
   $space->CreateSpace("dev");
} catch (\SpacesAPIException $e) {
  $error = $e->GetError();

   //Error management code.
   echo "<pre>";
   print_r($error);
   /*
   EG:
   Array (
    [message] => Bucket already exists
    [code] => BucketAlreadyExists
    [type] => client
    [http_code] => 409
   )
   */
}

许可协议

FOSSA Status