Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: doesObjectExist and doesBucketExist v2 #2424

Merged
merged 10 commits into from
Apr 18, 2022
Merged

Conversation

stobrien89
Copy link
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
src/S3/S3ClientTrait.php Outdated Show resolved Hide resolved
src/S3/S3ClientTrait.php Outdated Show resolved Hide resolved
src/S3/S3ClientTrait.php Outdated Show resolved Hide resolved
src/S3/S3ClientTrait.php Outdated Show resolved Hide resolved
src/S3/S3ClientTrait.php Outdated Show resolved Hide resolved
$this->execute($command);
return true;
} catch (S3Exception $e) {
if (($accept403 && $e->getStatusCode() === 403)
Copy link
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

src/S3/S3ClientTrait.php Show resolved Hide resolved
src/S3/S3ClientTrait.php Outdated Show resolved Hide resolved
$response = $e->getResponse();

if ($includeDeleteMarkers
&& (!empty($response)
Copy link
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
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

src/S3/S3ClientTrait.php Outdated Show resolved Hide resolved
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