Skip to content

Latest commit

 

History

History
146 lines (109 loc) · 4.35 KB

README-CN.md

File metadata and controls

146 lines (109 loc) · 4.35 KB

Alibaba Cloud OSS SDK for PHP v2

GitHub version

alibabacloud-oss-php-sdk-v2 是OSS在PHP编译语言下的第二版SDK

关于

  • 此PHP SDK基于阿里云对象存储服务官方API构建。
  • 阿里云对象存储(Object Storage Service,简称OSS),是阿里云对外提供的海量,安全,低成本,高可靠的云存储服务。
  • OSS适合存放任意文件类型,适合各种网站、开发企业及开发者使用。
  • 使用此SDK,用户可以方便地在任何应用、任何时间、任何地点上传,下载和管理数据。

运行环境

  • PHP 7.4及以上。

安装方法

通过 composer 安装

如果您通过composer管理您的项目依赖,可以在你的项目根目录运行:

$ composer require alibabacloud/oss-v2

或者在你的composer.json中声明对Alibaba Cloud OSS SDK for PHP v2的依赖:

"require": {
    "alibabacloud/oss-v2": "*"
}

然后通过composer install安装依赖

通过PHAR 文件安装

require_once '/path/to/alibabacloud-oss-php-sdk-v2-{version}.phar'

快速使用

获取存储空间列表(List Bucket)

<?php

use AlibabaCloud\Oss\V2 as Oss;

$region = 'cn-hangzhou';

// Loading credentials values from the environment variables
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Using the SDK's default configuration
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);

$client = new Oss\Client($cfg);

// Create the Paginator for the ListBuckets operation
$paginator = new Oss\Paginator\ListBucketsPaginator($client);
$iter = $paginator->iterPage(new Oss\Models\ListBucketsRequest());

// Iterate through the bucket pages
foreach ($iter as $page) {
    foreach ($page->buckets ?? [] as $bucket) {
        print("Bucket: $bucket->name, $bucket->location\n");
    }
}

获取文件列表(List Objects)

<?php

use AlibabaCloud\Oss\V2 as Oss;

$region = 'cn-hangzhou';
$bucket = 'your bucket name';

// Loading credentials values from the environment variables
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Using the SDK's default configuration
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);

$client = new Oss\Client($cfg);

# Create the Paginator for the ListBuckets operation
$paginator = new Oss\Paginator\ListObjectsV2Paginator($client);
$iter = $paginator->iterPage(new Oss\Models\ListObjectsV2Request($bucket));

// Iterate through the object pages
foreach ($iter as $page) {
    foreach ($page->contents ?? [] as $object) {
        print("Object: $object->key, $object->type, $object->size\n");
    }
}

上传文件(Put Object)

<?php

use AlibabaCloud\Oss\V2 as Oss;

$region = 'cn-hangzhou';
$bucket = 'your bucket name';
$key = 'your object name';

// Loading credentials values from the environment variables
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Using the SDK's default configuration
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);

$client = new Oss\Client($cfg);

$data = 'Hello OSS';

$request = new Oss\Models\PutObjectRequest($bucket, $key);
$request->body = Oss\Utils::streamFor($data);

$result = $client->putObject($request);

printf(
    'status code:'. $result->statusCode .PHP_EOL.
    'request id:'. $result->requestId .PHP_EOL.
    'etag:'. $result->etag. PHP_EOL
);

更多示例

请参看sample目录

运行示例

  • 执行composer install下载依赖的库
  • 进入示例程序目录 sample
  • 通过环境变量,配置访问凭证, export OSS_ACCESS_KEY_ID="your access key id", export OSS_ACCESS_KEY_SECRET="your access key secrect"
  • 以 ListBuckets.php 为例,执行 php ListBuckets.php --region cn-hangzhou

资源

开发者指南 - 参阅该指南,来帮助您安装、配置和使用该开发套件。

许可协议