I'm using AWS SDK for PHP (Laravel) and I'm trying to delete a S3 bucket with all the objects inside. Here is my code:
public function deleteBucket($syndcateid){
$s3client = AWS::createClient('s3');
$bucket = env('S3_BUCKET')."/syndcate-uploads/syndcate-".$syndcateid;
$listObjectsParams = ['Bucket' => $bucket];
$batchDelete = BatchDelete::fromListObjects($s3client, $listObjectsParams);
$batchDelete->delete();
$s3client->deleteBucket(array('Bucket' => $bucket));
$s3client->waitUntil('BucketNotExists', array('Bucket' => $bucket));
}
And when I run this code, I get the error:
Error executing \\"ListObjects\\" on \\" https://s3-us-west-2.amazonaws.com/syndcate-media%2Fsyndcate-uploads%2Fsyndcate-32?delimiter=%2F&encoding-type=url \\"; AWS HTTP error: Client error:
GET https://s3-us-west-2.amazonaws.com/syndcate-media%2Fsyndcate-uploads%2Fsyndcate-32?delimiter=%2F&encoding-type=url
resulted in a403 Forbidden
response:\\n\\nSignatureDoesNotMatch
The request signature we calcul (truncated...)\\n SignatureDoesNotMatch (client): The request signature we calculated does not match the signature you provided. Check your key and signing method. - \\nSignatureDoesNotMatch
The request signature we calculated does not match the signature you provided. Check your key and signing method.
Basically it is a SignatureDoesNotMatch
error, and the reason its happening is, the slashes in the URL are not being decoded, as the requested URL ( https://s3-us-west-2.amazonaws.com/syndcate-media%2Fsyndcate-uploads%2Fsyndcate-32
) appears to have %2F
instead of slashes.
I tried with the root bucket (no slashes), and it worked.
Any idea how to get the bucket name right ?
Got it!
What was I thinking, the bucket is only env('S3_BUCKET')
, that is sort of the root. But there are no folders in S3, there is a folder-like structure to make classification easier. All files are in one place, the filenames has paths as the prefix (ie. /path/to/folder/image.jpg
).
So I want wasn't deleting a bucket, but only deleting a folder. But since there are no folders, I only wanted to delete the files that stars with a prefix such as path/to/folder/
Anyways, here is the updated code:
public function deleteBucket($syndcateid){
$s3client = AWS::createClient('s3');
$startsWith = "syndcate-uploads/syndcate-".$syndcateid."/";
$listObjectsParams = ['Bucket' => env('S3_BUCKET'), 'Prefix'=>$startsWith, 'Delimiter'=>'/'];
$batchDelete = BatchDelete::fromListObjects($s3client, $listObjectsParams);
$batchDelete->delete();
}
雅S3是一个对象存储,所以没有实际的文件夹。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.