drgwsx8405 2018-09-19 06:34
浏览 32
已采纳

使用php Yii2在Amazon S3中删除对象(单个图像)不起作用?

Here I have the code of connection

 $s3 = S3Client::factory(array(
           'key'    => 'xxxxxxxxx',
           'secret' => '0xxxxxxxxxx',
           'version' => 'latest',
           'region' => 'us-west-2',
     ))

Here the bucket name $bucket = 'ariana-ios-storages'; and here the name of file $p = parse_url($images->name) which is the name of file in amazon to delete;

Here is the code to delete the file

$delete =  $s3->deleteObject([
               'Bucket' => $bucket,
               'Key'    => $p['path']
            ]);
        var_dump($delete);
        exit();

The file path is generated like this;

It is not deleting the single file from the bucket so can any one help what i am missing

  • 写回答

1条回答 默认 最新

  • dongyou6768 2018-09-24 07:05
    关注
      public function actionDeleteImages($id)
      {
        $exportImage=  \common\models\ExportImages::findOne($id);
        $base_url='base url to bucket';
        $s3 = S3Client::factory(array(
            'key'    => 'xxxxxxx',
            'secret' => '0pdfOx21xxxxxxxxxx',
                'version' => 'latest',
                'region' => 'us-west-2',
        ));
        $bucket = 'name of bucket';
        $key=urldecode(explode($base_url,$exportImage->name)[1]);// $images->name is  Path from Db, base url is path to s3 server
        $delete =  $s3->deleteObject([
            'Bucket' => 'ariana-ios-storages',
            'Key' => $key
        ]);
        $object_exist =  $s3->doesObjectExist($bucket,$key);
        if(!$object_exist){
            return "succesfully deleted";
        }else{
            return "there is some problem while procesing the deletion";
        }
      }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?