全部产品
Search
文档中心

对象存储 OSS:PHP删除文件

更新时间:Nov 01, 2024

本文介绍如何在受版本控制的存储空间(Bucket)中删除单个或多个文件(Object))以及指定前缀的(Prefix)的文件。

注意事项

  • 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的Region与Endpoint的对应关系,请参见OSS访问域名、数据中心、开放端口

  • 本文以OSS域名新建OSSClient为例。如果您希望通过自定义域名、STS等方式新建OSSClient,请参见新建OssClient

  • 要删除文件,您必须有oss:DeleteObject权限。具体操作,请参见为RAM用户授权自定义的权限策略

版本控制下的删除行为

  • 未指定versionId(临时删除):

    如果在未指定versionId的情况下执行删除操作时,默认不会删除Object的当前版本,而是对当前版本插入删除标记(Delete Marker)。当执行GetObject操作时,OSS会检测到当前版本为删除标记,并返回404 Not Found。此外,响应中会返回header:x-oss-delete-marker = true以及新生成的删除标记的版本号x-oss-version-id

  • 指定versionId(永久删除):

    如果在指定versionId的情况下执行删除操作时,OSS会根据params中指定的versionId参数永久删除该版本。如果要删除ID为“null”的版本,请在params参数中添加params['versionId'] = “null”,OSS会将“null”字符串当成"null"的versionId,从而删除versionId为"null"的Object。

删除单个文件

以下提供了永久删除和临时删除单个Object的示例。

  • 永久删除

    以下代码用于指定versionId对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\CoreOssException;
    
    // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    $provider = new EnvironmentVariableCredentialsProvider();
    // Endpoint以杭州为例,其它Region请按实际情况填写。
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket= "<yourBucketName>";
    // 填写不包含Bucket名称在内的Object的完整路径,例如example/test.txt。
    $object = "<yourObjectName>";
    $versionId = "<yourObjectVersionId>";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    
    try{
        // 删除指定versionId的Object。
        $ossClient->deleteObject($bucket, $object, array(OssClient::OSS_VERSION_ID => $versionId));
    } catch(OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    
    print(__FUNCTION__ . ": OK" . "\n");        
  • 临时删除

    以下代码用于不指定versionId对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\CoreOssException;
    
    // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    $provider = new EnvironmentVariableCredentialsProvider();
    // Endpoint以杭州为例,其它Region请按实际情况填写。
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket= "<yourBucketName>";
    $object = "<yourObjectName>";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    
    try{
        // 不指定versionId对Object进行临时删除,此操作会为Object添加删除标记。
        $ret = $ossClient->deleteObject($bucket, $object);
        print("delete marker:" .$ret['x-oss-delete-marker'] ."\n");
        print("version id:" .$ret['x-oss-version-id']);
    } catch(OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    
    print(__FUNCTION__ . ": OK" . "\n");    

删除单个文件的详细信息请参见DeleteObject

删除多个文件

以下提供了永久删除以及临时删除多个Object的示例。

  • 永久删除

    以下代码用于指定versionId对多个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\CoreOssException;
    use OSS\Model\DeleteObjectInfo;
    
    // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    $provider = new EnvironmentVariableCredentialsProvider();
    // Endpoint以杭州为例,其它Region请按实际情况填写。
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket= "<yourBucketName>";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    
    try{
        // 删除多个指定versionId的Object。
        $objects = array(); 
        $objects[] = new DeleteObjectInfo("<yourObject1Name>", "<object1VersionId>");
        $objects[] = new DeleteObjectInfo("<yourObject2Name>", "<object2VersionId>");
        
        $deletedObjectList = $ossClient->deleteObjectVersions($bucket, $objects);
    
        // 查看删除结果。
        if (!empty($deletedObjectList)) {
              print("deletedObjectList:\n");
              foreach ($deletedObjectList as $deletedObjectInfo) {
                print($deletedObjectInfo->getKey() . ",");
                print($deletedObjectInfo->getVersionId() . ",");
                print($deletedObjectInfo->getDeleteMarker() . ",");
                print($deletedObjectInfo->getDeleteMarkerVersionId() . "\n");
              }
        }
    } catch(OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    
    print(__FUNCTION__ . ": OK" . "\n");
  • 临时删除

    以下代码用于不指定versionId对多个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\CoreOssException;
    use OSS\Model\DeleteObjectInfo;
    
    // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    $provider = new EnvironmentVariableCredentialsProvider();
    // Endpoint以杭州为例,其它Region请按实际情况填写。
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket= "<yourBucketName>";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    
    try{
        // 不指定versionId删除多个Object,此操作会为Object添加删除标记。
            $objects = array(); 
        $objects[] = new DeleteObjectInfo("<yourObject1Name>");
        $objects[] = new DeleteObjectInfo("<yourObject2Name>");
        
        $deletedObjectList = $ossClient->deleteObjectVersions($bucket, $objects);
    
        // 查看删除结果。
        if (!empty($deletedObjectList)) {
              print("deletedObjectList:\n");
              foreach ($deletedObjectList as $deletedObjectInfo) {
                print($deletedObjectInfo->getKey() . ",");
                print($deletedObjectInfo->getVersionId() . ",");
                print($deletedObjectInfo->getDeleteMarker() . ",");
                print($deletedObjectInfo->getDeleteMarkerVersionId() . "\n");
              }
        }
    } catch(OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    
    print(__FUNCTION__ . ": OK" . "\n");

删除多个文件的详细信息请参见DeleteMultipleObjects