开放存储服务(openstorageservice,简称oss),是阿里云对外提供的海量,安全,低成本,高可靠的云存储服务。用户可以通过简单的api(rest方式的接口),在任何时间、任何地点、任何互联网设备上进行数据上传和下载。
首先、登录阿里云首页http://www.aliyun.com/ 免费开通服务。
开通以后,进入管理控制台--进入开发存储服务oss,进入到控制台。
其次、下载sdk http://bbs.aliyun.com/job.php?action=download&aid=41064
解压后在samples文件夹内新建一个index.php文件
$accesskeyid, //accesskeyid在“用户中心”--“我的服务”--“安全认证”
'accesskeysecret' => $accesskeysecret,
));
}
//获取所有bucket
//bucket是水桶的意思,可以理解为容器,里面装了各种各样的东西,也就是key=>value
function listbuckets(ossclient $client) {
$buckets = $client->listbuckets();
foreach ($buckets as $bucket) {
echo 'bucket: ' . $bucket->getname() . \n;
}
}
//新建bucket
function createbucket(ossclient $client, $bucket) {
$client->createbucket(array( 'bucket' => $bucket, ));
}
// sample of get bucket acl
function getbucketacl(ossclient $client, $bucket) {
$acl = $client->getbucketacl(array( 'bucket' => $bucket, ));
$grants = $acl->getgrants(); echo $grants[0];
}
//获取bucket的所有key
function getbucketkey(ossclient $client, $bucket){
$objectlisting = $client->listobjects(array( 'bucket' => $bucket, ));
foreach ($objectlisting->getobjectsummarys() as $objectsummary) {
$objectsummary->getkey();
}
}
//向某个bucket上传内容,可以字符串或者文件
function upload(ossclient $client, $bucket,$key,$content,$type){
isset($type)?$type:0;
if($type == 0){
$type = gettype($content);
//如果上传内容为数组或者对象,需要进行序列化
if($type == array || $type == object) {
$content = serialize($content);
}
$client->putobject(array( 'bucket' => $bucket, 'key' => $key, 'content' => $content, ));
}else{
$client->putobject(
array( 'bucket' => $bucket,
'key' => $key,
'content' => fopen($content, 'r'),
'contentlength' => filesize($content),
));
}
}
//下载文件
function download(ossclient $client, $bucket, $key){
$object = $client->getobject(
array( 'bucket' => $bucket,
'key' => $key,
));
echo (string)$object;
}
// 删除bucket
function deletebucket(ossclient $client, $bucket) {
$client->deletebucket(
array( 'bucket' => $bucket, ));
}
$keyid = '5wtgmhx6mqx6q***';
$keysecret = 'vdisrnuhey0thb9v1ryv08vj4lc***';
$client = createclient($keyid, $keysecret);
$bucket = 'xiaoqiangbucket';
$key = rand(1,1000);
$content = this is xiaoqiang's content;
//$content = array(1,2,3,4,5,6,7,8);
listbuckets($client); //显示所有bucket
createbucket($client, $bucket); //新建一个bucket
getbucketacl($client, $bucket);
download($client, $bucket, $key); //下载key为$key的content
upload($client, $bucket, $key, $content,0); //上传$key=>$content到bucket
var_dump(getbucketkey($client, $bucket));//显示bucket内的所有key
//deletebucket($client, $bucket); //删除bucket
至此在客户端实现了控制台的所有操。
http://www.bkjia.com/phpjc/621621.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/621621.htmltecharticle开放存储服务(openstorageservice,简称oss),是阿里云对外提供的海量,安全,低成本,高可靠的云存储服务。用户可以通过简单的api(rest方...