Home > php教程 > php手册 > 基于PHP实现阿里云开放存储服务

基于PHP实现阿里云开放存储服务

WBOY
Release: 2016-06-06 19:55:54
Original
1323 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 开放存储服务(OpenStorageService,简称OSS),是阿里云对外提供的海量,安全,低成本,高可靠的云存储服务。用户可以通过简单的API(REST方式的接口),在任何时间、任何地点、任何互联网设备上进行

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  开放存储服务(OpenStorageService,简称OSS),是阿里云对外提供的海量,安全,低成本,高可靠的云存储服务。用户可以通过简单的API(REST方式的接口),在任何时间、任何地点、任何互联网设备上进行数据上传和下载。

  首先、登录阿里云首页免费开通服务。

  开通以后,进入"管理控制台"--进入"开发存储服务OSS",进入到控制台。

  其次、下载SDK

  解压后在samples文件夹内新建一个index.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),

  ));

  }

  }

[1] [2] 

基于PHP实现阿里云开放存储服务

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template