Implementing Alibaba Cloud open storage service based on PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:18:47
Original
989 people have browsed it

Open Storage Service (OSS) is a massive, secure, low-cost, and highly reliable cloud storage service provided by Alibaba Cloud. Users can upload and download data at any time, anywhere, and on any Internet device through a simple API (REST interface).

First, log in to the Alibaba Cloud homepage http://www.aliyun.com/ to activate the service for free.
After activation, enter the "Management Console"--enter "Development Storage Service OSS" and enter the console.
Secondly, download the SDK http://bbs.aliyun.com/job.php?action=download&aid=41064
After unzipping, create a new index.php file in the samples folder
require_once dirname(__DIR__).'/aliyun.php';
use AliyunOSSOSSClient;
//Create a client to connect to Alibaba Cloud Open Storage
function createClient($accessKeyId, $accessKeySecret) {
return OSSClient::factory(array(
'AccessKeyId' => $accessKeyId, //AccessKeyId is in "User Center" - "My Services" - "Security Authentication"
'AccessKeySecret' => $accessKeySecret,
));
}
//Get all buckets
//Bucket means bucket, which can be understood as a container, which contains various things, that is, key=>value
function listBuckets(OSSClient $client) {
$buckets = $client->listBuckets();
foreach ($buckets as $bucket) {
echo 'Bucket: ' . $bucket->getName() . "n";
}
}
//New 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];
}
//Get all keys of the bucket
function getBucketKey(OSSClient $client, $bucket){
$objectListing = $client->listObjects(array( 'Bucket' => $bucket, ));
foreach ($objectListing->getObjectSummarys() as $objectSummary) {
$objectSummary->getKey();
}
}
//Upload content to a bucket, which can be a string or a file
function upload(OSSClient $client, $bucket,$key,$content,$type){
isset($type)?$type:0;
if($type == 0){
$type = gettype($content);
                  //If the uploaded content is an array or object, it needs to be serialized
         if($type == "array" || $type == "object") {
$content = serialize($content);
    }
$client->putObject(array( 'Bucket' => $bucket, 'Key' => $key, 'Content' => $content, ));
}else{
$client->putObject(
              array( 'Bucket' => $bucket,
                                                                                                                                                    >
                                                                                                                                                                                       
              ));
}
}
//Download file
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
 至此在客户端实现了控制台的所有操。
 
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/621621.htmlTechArticle开放存储服务(OpenStorageService,简称OSS),是阿里云对外提供的海量,安全,低成本,高可靠的云存储服务。用户可以通过简单的API(REST方...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template