OSS uses a flat structure to store objects, but simulates folders by treating objects whose names end with a forward slash (/) as folder markers. A folder is a naming convention — its existence is implied by the objects it contains.
Prerequisites
Before you begin, make sure you have:
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with your access credentials.An OSS bucket
The examples use the public endpoint for the China (Hangzhou) region (https://oss-cn-hangzhou.aliyuncs.com). To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.
To create an OSSClient instance using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
All examples share the same client initialization. Set up the OSSClient instance once and reuse it across operations:
<?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\CoreOssException;
// Load credentials from environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
$config = array(
"provider" => $provider,
"endpoint" => "yourEndpoint",
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);Create a folder
A folder is an object with an empty body whose name ends with /. Upload an empty object using putObject() with a key that ends with a forward slash.
// Specify the bucket name and folder name.
// The folder name must end with a forward slash (/).
$bucket = "examplebucket";
$object = "exampledir/";
$content = "";
// Optional: set access permissions and custom metadata on the folder object.
$options = array(
OssClient::OSS_HEADERS => array(
'x-oss-object-acl' => 'private',
'x-oss-meta-info' => 'your info'
),
);
try {
$ossClient->putObject($bucket, $object, $content, $options);
print("Folder created successfully." . PHP_EOL);
} catch (OssException $e) {
printf("Failed to create folder: %s" . PHP_EOL, $e->getMessage());
}For the underlying API reference, see PutObject.
Delete a folder
Deleting a folder permanently deletes all objects and subdirectories inside it. This action cannot be undone.
Because folders have no independent existence in OSS, deleting a folder means listing all objects with the folder prefix and deleting them in batches. The code below deletes the log/ folder and everything it contains from examplebucket.
$bucket = "examplebucket";
$option = array(
OssClient::OSS_MARKER => null,
// Specify the full path of the folder to delete (without the bucket name).
OssClient::OSS_PREFIX => "log/",
OssClient::OSS_DELIMITER => '',
);
try {
$hasMore = true;
while ($hasMore) {
$result = $ossClient->listObjects($bucket, $option);
$objects = array();
foreach ($result->getObjectList() as $info) {
printf("Found object: %s" . PHP_EOL, $info->getKey());
$objects[] = $info->getKey();
}
if (count($objects) > 0) {
$deleted = $ossClient->deleteObjects($bucket, $objects);
foreach ($deleted as $key) {
printf("Deleted: %s" . PHP_EOL, strval($key));
}
}
// Handle pagination: continue until all objects are listed.
if ($result->getIsTruncated() === 'true') {
$option[OssClient::OSS_MARKER] = $result->getNextMarker();
} else {
$hasMore = false;
}
}
print("Folder deleted successfully." . PHP_EOL);
} catch (OssException $e) {
printf("Failed to delete folder: %s" . PHP_EOL, $e->getMessage());
}For the complete sample code, see the GitHub example. For the underlying API reference, see DeleteObject.