PHP code example of alibabacloud / oss-v2

1. Go to this page and download the library: Download alibabacloud/oss-v2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alibabacloud / oss-v2 example snippets




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");
    }
}



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");
    }
}



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
);