Quick Start
This guide covers the basic steps to initialize the OSS client and perform simple file operations.
1. Initialize the Client
To communicate with OSS, you must initialize an OssClient. You need your AccessKey ID, AccessKey Secret, and the Endpoint for your region.
use OSS\OssClient;
use OSS\Core\OssException;
$accessKeyId = "<Your AccessKeyID>";
$accessKeySecret = "<Your AccessKeySecret>";
$endpoint = "oss-cn-hangzhou.aliyuncs.com"; // Example endpoint
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
2. Create a Bucket
A bucket is a namespace for your objects. Every object must reside in a bucket.
$bucket = "my-unique-bucket-name";
try {
$ossClient->createBucket($bucket);
} catch (OssException $e) {
print $e->getMessage();
}
3. Upload an Object
Objects are the fundamental data units in OSS. You can upload data directly from a string.
$object = "example-object.txt";
$content = "Hello, OSS!";
try {
$ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
print $e->getMessage();
}
4. Download an Object
Retrieve the content of an object back into memory.
try {
$content = $ossClient->getObject($bucket, $object);
echo "Object Content: " . $content;
} catch (OssException $e) {
print $e->getMessage();
}
5. Delete an Object
try {
$ossClient->deleteObject($bucket, $object);
} catch (OssException $e) {
print $e->getMessage();
}