webtechnick/cakephp-cloud-files-plugin

CakePHP 云存储插件

v1.4.0 2013-10-04 04:32 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:39:20 UTC


README

此插件用于与Rackspace CloudFiles服务接口。此插件利用rackspace提供的php-cloudfiles

要求

  • CakePHP 2.x, PHP 5.x
  • https://github.com/rackspace/php-opencloud定义的任何要求

变更日志

  • 1.4.0:升级到https://github.com/rackspace/php-opencloud,而不是已弃用的https://github.com/rackerlabs/php-cloudfiles。更新您的子模块。
  • 1.3.0:添加CloudFiles::exists和CloudFiles::upload现在会在上传之前检查文件是否存在(默认关闭)
  • 1.2.0:添加CloudFiles.cloud_files shell以实现文件到您的CDN的基本上传/删除
  • 1.1.0:添加CloudFiles::listContainer, CloudFiles::createContainer, CloudFiles::deleteContainer
  • 1.0.1:添加CloudFiles::download
  • 1.0.0:首次发布 -- 更多的抛光,添加CloudFilesHelper,实现所有基本云文件REST操作。
  • 0.0.2:添加CloudFiles库 -- 利用rackspace php-cloudfiles库并实现了上传函数CloudFiles::upload, CloudFiles::delete, CloudFiles::ls
  • 0.0.1:首次提交 -- 插件骨架

安装

有两种方式安装此插件,通过GIT与子模块或手动下载两个存储库

Git安装(推荐)

在克隆存储库后,您必须运行git submodule init和update以拉取所需的供应商

git clone git://github.com/webtechnick/CakePHP-CloudFiles-Plugin.git app/Plugin/CloudFiles
cd app/Plugin/CloudFiles
git submodule init
git submodule update

手动安装

  • 将此插件下载到app/Plugin/CloudFiles
  • 将https://github.com/rackspace/php-opencloud下载到app/Plugin/CloudFiles/Vendor/php-opencloud

设置和配置

确保在app/Config/bootstrap.php中加载了插件,通过调用CakePlugin::load('CloudFiles');

创建一个包含以下内容的文件app/Config/cloud_files.php

$config = array(
	'CloudFiles' => array(
		'server' => 'US', //UK
		'username' => 'your_username', //your username
		'api_key' => 'API_KEY', //your api key
		'region' => 'ORD', //ORD, DFW, LON
		'url_type' => 'publicURL',
		'tenant_name' => ''
	)
);

此配置文件的示例在app/Plugin/CloudFiles/Config/cloud_files.php.default

使用示例

以下是一些基本使用示例

上传文件到rackspace

将本地文件上传到rackspace指定的容器

App::uses('CloudFiles','CloudFiles.Lib');
$cdn_url = CloudFiles::upload('/path/to/image.jpg','container_name');
//Will not re-upload the same image if it's already in the CDN
CloudFiles::upload('/path/to/image.jpg', 'container_name', array('overwrite' => false));

提示:还有一个内置的shell可以帮助上传目录和文件

//Rerusively upload a directory to a container on rackspace
$ cake CloudFiles.cloud_files -r upload /path/to/directory container_name

//Upload a single file to a container on rackspace
$ cake CloudFiles.cloud_files upload_file /path/to/file.ext container_name

从rackspace下载文件

将特定容器中的远程文件下载到本地文件

App::uses('CloudFiles','CloudFiles.Lib');
CloudFiles::download('image.jpg', 'container_name', '/local/path/to/image.jpg');

从rackspace删除文件

从rackspace的特定容器中删除文件

App::uses('CloudFiles','CloudFiles.Lib');
CloudFiles::delete('image.jpg','container_name');

提示:还有一个内置的shell可以帮助在rackspace上删除文件

//Delete a single file in a container on rackspace
$ cake CloudFiles.cloud_files delete_file file.ext container_name

//Delete all files in a container as well as the container
$ cake CloudFiles.cloud_files delete_container container_name

在rackspace上列出文件

在rackspace的指定容器中列出文件

App::uses('CloudFiles','CloudFiles.Lib');
//Get all files in container
$files = CloudFiles::ls('container_name');

//Get files starting with a prefix
$files = CloudFiles::ls('container_name', array(
	'prefix' => 'cake'
));

//Limit the files returned
$files = CloudFiles::ls('container_name', array(
	'limit' => 10
));

//Limit the files returned, starting at marker
$files = CloudFiles::ls('container_name', array(
	'limit' => 10,
	'marker' => 30
));

文件在rackspace上的公共或流式URL

获取rackspace中对象的URL(流式或公共)

App::uses('CloudFiles','CloudFiles.Lib');
$url = CloudFiles::url('image.jpg','container_name');

$stream = CloudFiles::stream('movie.mov', 'container_name');

还有一个辅助类来协助图像和流式检索

//Some Controller
public $helpers = array('CloudFiles.CloudFiles');

//Some View
echo $this->CloudFiles->image('image.jpg','container_name');
echo $this->CloudFiles->stream('movie.mov', 'container_name');
echo $this->CloudFiles->url('some_file.txt', 'container_name');

在rackspace上列出容器

列出rackspace上的所有容器

App::uses('CloudFiles','CloudFiles.Lib');
//Get all containers
$containers = CloudFiles::listContainers();
//Limit the containers returned
$containers = CloudFiles::listContainers(array(
	'limit' => 2
));

在rackspace上创建容器

在rackspace上创建容器,默认为公共容器(CDN)

App::uses('CloudFiles','CloudFiles.Lib');
$Container = CloudFiles::createContainer('css');

提示:还有一个shell可以帮助创建容器。

$ cake CloudFiles.cloud_files create_container container_name

在rackspace上删除容器

在rackspace上删除容器,注意容器必须为空。

App::uses('CloudFiles', 'CloudFiles.Lib');
CloudFiles::deleteContainer('container_name');

提示:有一个外壳程序可以帮助删除容器。注意,这也会在删除容器之前删除所有文件

$ cake CloudFiles.cloud_files delete_container container_name