Create buckets, upload objects, download objects, list objects, and delete objects by using the OSS PHP SDK.
Prerequisites
The OSS PHP SDK is installed. For more information, see Installation.
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables are configured.The endpoint for the region where your bucket is located. For more information, see Regions and endpoints.
The bucket naming conventions. For more information, see Basic concepts.
Create a bucket
A bucket is a container that stores objects in OSS. Bucket names must be globally unique across all OSS users.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region. Replace with your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "examplebucket";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Set the storage class to Infrequent Access (IA). The default storage class is Standard.
$options = array(
OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_IA
);
// Set the ACL to public-read. The default ACL is private.
$ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");The createBucket method accepts the bucket name, an Access Control List (ACL) constant, and an options array. In this example, the storage class is set to IA and the ACL is set to public-read. By default, the bucket uses the Standard storage class and the private ACL.
Upload an object
This example uploads a string to OSS through streaming upload, which sends content directly from memory rather than from a local file.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region. For example, https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "examplebucket";
// Specify the object key. Do not include the bucket name.
$object = "exampledir/exampleobject.txt";
// Specify the content to upload.
$content = "Hello OSS";
// Optional: set the object ACL to private and add custom metadata.
$options = array(
OssClient::OSS_HEADERS => array(
'x-oss-object-acl' => 'private',
'x-oss-meta-info' => 'yourinfo'
),
);
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->putObject($bucket, $object, $content, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");The putObject method uploads content to the specified object key. Pass headers through the OssClient::OSS_HEADERS option to set the object ACL or add custom metadata.
Download an object
This example downloads an object to a local file.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "examplebucket";
// Specify the object key. Do not include the bucket name.
$object = "testfolder/exampleobject.txt";
// Specify the local file path. If a file with the same name exists, it is overwritten.
// If omitted, the object is saved to the project directory.
$localfile = "D:\\localpath\\examplefile.txt";
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $localfile
);
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->getObject($bucket, $object, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK, please check localfile: 'examplefile.txt'" . "\n");Set the OssClient::OSS_FILE_DOWNLOAD option to specify the local file path. If you omit this option, the object is saved to the project directory.
List objects
This example lists objects in a bucket. By default, a maximum of 100 objects are listed.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
try {
// Obtain access credentials from environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "examplebucket";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$listObjectInfo = $ossClient->listObjects($bucket);
printf("Bucket Name: %s\n", $listObjectInfo->getBucketName());
printf("Prefix: %s\n", $listObjectInfo->getPrefix());
printf("Marker: %s\n", $listObjectInfo->getMarker());
printf("Next Marker: %s\n", $listObjectInfo->getNextMarker());
printf("Max Keys: %s\n", $listObjectInfo->getMaxKeys());
printf("Delimiter: %s\n", $listObjectInfo->getDelimiter());
printf("Is Truncated: %s\n", $listObjectInfo->getIsTruncated());
$objectList = $listObjectInfo->getObjectList();
$prefixList = $listObjectInfo->getPrefixList();
if (!empty($objectList)) {
print("objectList:\n");
foreach ($objectList as $objectInfo) {
printf("Object Name: %s\n", $objectInfo->getKey());
printf("Object Size: %s\n", $objectInfo->getSize());
printf("Object Type: %s\n", $objectInfo->getType());
printf("Object ETag: %s\n", $objectInfo->getETag());
printf("Object Last Modified: %s\n", $objectInfo->getLastModified());
printf("Object Storage Class: %s\n", $objectInfo->getStorageClass());
if ($objectInfo->getRestoreInfo()) {
printf("Restore Info: %s\n", $objectInfo->getRestoreInfo());
}
if ($objectInfo->getOwner()) {
printf("Owner Id: %s\n", $objectInfo->getOwner()->getId());
printf("Owner Name: %s\n", $objectInfo->getOwner()->getDisplayName());
}
}
}
if (!empty($prefixList)) {
print("prefixList:\n");
foreach ($prefixList as $prefixInfo) {
printf("Common Prefix: %s\n", $prefixInfo->getPrefix());
}
}
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}The listObjects method returns an ObjectListInfo object that contains:
Listing metadata: BucketName, Prefix, Marker, NextMarker, MaxKeys, Delimiter, IsTruncated
Object details: Key, Size, Type, ETag, LastModified, StorageClass, and optionally RestoreInfo and Owner (Id, DisplayName)
Common prefixes: groups objects by delimiter
Delete an object
This example deletes a specified object.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region. For example, https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket = "examplebucket";
// Specify the object key. Do not include the bucket name.
$object = "exampledir/exampleobject.txt";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->deleteObject($bucket, $object);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");References
For the complete sample code, see GitHub example.
PutBucket - Create a bucket
PutObject - Upload an object
GetObject - Download an object
GetBucket (ListObjects) - List objects
DeleteObject - Delete an object