aliyuncs/oss-sdk-php

Install OSS PHP SDK

  • If you use the composer to manage project dependencies, run the following command in your project's root directory:

      composer require aliyuncs/oss-sdk-php
    

    You can also declare the dependency on Alibaba Cloud OSS SDK for PHP in the composer.json file.

      "require": {
          "aliyuncs/oss-sdk-php": "~2.0"
      }
    

    Then run composer install to install the dependency. After the Composer Dependency Manager is installed, import the dependency in your PHP code:

      require_once __DIR__ . '/vendor/autoload.php';
    
  • You can also directly download the packaged PHAR File, andintroduce the file to your code:

      require_once '/path/to/oss-sdk-php.phar';
    
  • Download the SDK source code, and introduce the autoload.php file under the SDK directory to your code:

      require_once '/path/to/oss-sdk/autoload.php';
    

Quick use

Common classes

ClassExplanation
OSS\OSSClientOSS client class. An OSSClient instance can be used to call the interface.
OSS\Core\OSSExceptionOSS Exception class . You only need to pay attention to this exception when you use the OSSClient.

Initialize an OSSClient

The SDK's operations for the OSS are performed through the OSSClient class. The code below creates an OSSClient object:

<?php
$accessKeyId = "<AccessKeyID that you obtain from OSS>";
$accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
$endpoint = "<Domain that you select to access an OSS data center, such as "oss-cn-hangzhou.aliyuncs.com>";
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
} catch (OssException $e) {
    print $e->getMessage();
}

Operations on objects

Objects are the most basic data units on the OSS. You can simply consider objects as files. The following code uploads an object:

<?php
$bucket= "<Name of the bucket in use. Pay attention to naming conventions>";
$object = "<Name of the object in use. Pay attention to naming conventions>";
$content = "Hello, OSS!"; // Content of the uploaded file
try {
    $ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
    print $e->getMessage();
}

Operations on buckets

Buckets are the space that you use to manage the stored objects. It is an object management unit for users. Each object must belong to a bucket. You can create a bucket with the following code:

<?php
$bucket= "<Name of the bucket in use. Pay attention to naming conventions>";
try {
    $ossClient->createBucket($bucket);
} catch (OssException $e) {
    print $e->getMessage();
}

Handle returned results

The OSSClient provides the following two types of returned data from interfaces:

  • Put and Delete interfaces: The PUT and DELETE operations are deemed successful if null is returned by the interfaces without OSSException.

  • Get and List interfaces: The GET and LIST operations are deemed successful if the desired data is returned by the interfaces without OSSException. For example,

    <?php
    $bucketListInfo = $ossClient->listBuckets();
    $bucketList = $bucketListInfo->getBucketList();
    foreach($bucketList as $bucket) {
        print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
    }

In the above code, $bucketListInfo falls into the 'OSS\Model\BucketListInfo' data type.

Run a sample project

  • Modify samples/Config.php to complete the configuration information.
  • Run cd samples/ && php RunAll.php.

Run a unit test

  • Run composer install to download the dependent libraries.

  • Set the environment variable.

      export OSS_ACCESS_KEY_ID=access-key-id
      export OSS_ACCESS_KEY_SECRET=access-key-secret
      export OSS_ENDPOINT=endpoint
      export OSS_BUCKET=bucket-name
    
  • Run php vendor/bin/phpunit

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值