Skip to content

feat: doesObjectExist and doesBucketExist v2#2424

Merged
stobrien89 merged 10 commits intoaws:masterfrom
stobrien89:doe-v2
Apr 18, 2022
Merged

feat: doesObjectExist and doesBucketExist v2#2424
stobrien89 merged 10 commits intoaws:masterfrom
stobrien89:doe-v2

Conversation

@stobrien89
Copy link
Copy Markdown
Member

@stobrien89 stobrien89 commented Apr 11, 2022

Issue #, if available:
Implements #1561, closes #2342

Description of changes:

New object/bucket existence helper methods with changed exception handling behavior. Previously, false was returned any time an exception was thrown by the underlying S3 APIs (headObject and headBucket) unless the exception's status code was >= 500 (then exception was thrown) or the exception's error code was AccessDenied (true was returned). This was problematic because false could be returned in the cases of an unresolvable endpoint, expired credentials, invalid credentials, or if bucket permissions were not granted to the calling entity and resources did in fact exist.

This implementation makes fewer assumptions in cases where status codes are ambiguous, namely 403 status codes. a 403 status code can signify bad credentials or incorrect bucket-level permissions— this causes issues with headObject/doesObjectExist calls in particular because a 403 will be returned whether or not an object exists. In this implementation, exceptions will be thrown in all cases when an exception is thrown by the underlying API, unless the exception has a 404 status code (returns false) or in the case of DoesBucketExist, a user can specify if they want to accept 403 status codes, as a 403 will be thrown if a bucket exists, but an entity doesn't have correct bucket-level permissions.

Other changes include accepting bucket redirects (incorrectly specified region) as a true case for DoesBucketExist, the $accept403 flag for doesBucketExist and the $includeDeleteMarkers flag for doesObjectExist, which counts a 404 response with the presence of a delete marker header as a true case.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@stobrien89 stobrien89 added the contribution/core This is a PR that came from AWS. label Apr 11, 2022
@stobrien89 stobrien89 requested a review from SamRemis April 11, 2022 19:26
Comment thread src/S3/S3ClientTrait.php Outdated
Comment thread src/S3/S3ClientTrait.php Outdated
Comment thread src/S3/S3ClientTrait.php Outdated
Comment thread src/S3/S3ClientTrait.php Outdated
Comment thread src/S3/S3ClientTrait.php Outdated
Comment thread src/S3/S3ClientInterface.php Outdated
Comment thread src/S3/S3ClientTrait.php Outdated
$this->execute($command);
return true;
} catch (S3Exception $e) {
if (($accept403 && $e->getStatusCode() === 403)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a newline after the first parenthesis

Comment thread src/S3/S3ClientTrait.php
Comment thread src/S3/S3ClientTrait.php Outdated
Comment thread src/S3/S3ClientTrait.php Outdated
$response = $e->getResponse();

if ($includeDeleteMarkers
&& (!empty($response)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract this into a hasDeleteMarker method that checks if a response exists and returns whether or not that header is there? Condense a a bit of code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally agree. just took a second glance and that looks pretty ugly

Comment thread src/S3/S3ClientTrait.php Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution/core This is a PR that came from AWS.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

doesObjectExist should not return true or false based on assumptions

2 participants