山河云对象存储的 PHP SDK 已在 GitHub 开源,本文为简要使用文档。更多详细信息请参见 GitHub 项目

使用 SDK 之前请先在 管理控制台申请 Access key。

安装

使用 Composer 安装:

> composer require yunify/qingstor-sdk

初始化服务

发起请求前需要初始化服务。以下代码初始化了一个 QingStor Service。

use QingStor\SDK\Service\QingStor;
use QingStor\SDK\Config;

$config = new Config("ACCESS_KEY_ID", "SECRET_ACCESS_KEY");
$service = new QingStor($config);

代码示例

  1. 获取账户下的 Bucket 列表

    $response = $test_service->listBuckets();
    
    // Print the response statusCode.
    echo $response->statusCode;
    
    // Print the buckets.
    var_dump($response->buckets);
  2. 初始化并创建 Bucket, 需要指定 Bucket 名称和所在 Zone:

    $test_bucket = $test_service->Bucket("test-bucket", "jn1a")
    $response = $test_bucket.put()
  3. 获取 Bucket 中存储的 Object 列表

    $response = bucket.listObjects()
    
    // Print the response statusCode.
    echo $response->statusCode;
    
    // Print the objects keys.
    var_dump($response->keys);
  4. 创建一个 Object

    // Put object
    $response = $test_bucket->putObject(
        "test_file",
        array(
            'body' => file_get_contents('/tmp/test_file'),
        )
    )
    
    // Print the response statusCode.
    echo $response->statusCode;
  5. 删除一个 Object

    // Delete object
    $response = $test_bucket->deleteObject("test_object");
    
    // Print the response statusCode.
    echo $response->statusCode;
  6. 设置 Bucket ACL

    $response = $test_bucket->putACL(
        array(
            "acl => array(
                "grantee" => array(
                    "type" => "user",
                    "ID" => "usr-xxxxxxxx"
                ),
                "permission" => "FULL_CONTROL"
            )
        )
    );
    
    // Print the response statusCode.
    echo $response->statusCode;