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

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

Jun 06, 2016 pm 07:55 PM
liphpbased onstorageaccomplishopenServeEnterAli

欢迎进入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实现阿里云开放存储服务

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.