基於PHP實現阿里雲開放存儲服務

開放存儲服務(OpenStorageService,簡稱OSS),是阿里雲對外提供的海量,安全,低成本,高可靠的雲存儲服務。用戶能夠經過簡單的API(REST方式的接口),在任什麼時候間、任何地點、任何互聯網設備上進行數據上傳和下載。php

首先、登陸阿里雲首頁http://www.aliyun.com/ 免費開通服務。數組

開通之後,進入"管理控制檯"--進入"開發存儲服務OSS",進入到控制檯。安全

其次、下載SDK http://bbs.aliyun.com/job.php?action=download&aid=41064ui

解壓後在samples文件夾內新建一個index.php文件this

 

<?php

require_once dirname(__DIR__).'/aliyun.php';

use Aliyun\OSS\OSSClient;

// 建立一個鏈接阿里雲開放存儲的客戶端
function createClient($accessKeyId, $accessKeySecret) {
    return OSSClient::factory(array(
        'AccessKeyId' => $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

 至此在客戶端實現了控制檯的全部操。阿里雲

相關文章
相關標籤/搜索