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

Check if an object exists on S3 #17

Closed
capsandiego opened this issue Feb 18, 2021 · 3 comments
Closed

Check if an object exists on S3 #17

capsandiego opened this issue Feb 18, 2021 · 3 comments

Comments

@capsandiego
Copy link

Hi, is there a way to check if an object (file) does exist in my bucket? I see Connector::getObject but that seems to download the file while I just need something like objectExists(string $path):bool
Thanks!

@nikosdion
Copy link
Member

We currently do not implement Amazon S3's HeadObject. This leaves you with two options.

One option is listing the bucket (getBucket) and see if the object exists.

The other option is trying to retrieve the first byte of an object, e.g.

try {
  $dummy = $s3->getObject($bucket, $objectPath, null, 0, 1)
  $fileExists = true;
} catch (Exception $e) {
  $fileExists = false;
}

This method isn't ideal and may outright fail if you have objects with certain storage types like Glacier.

I can definitely try to implement HeadObject.

@capsandiego
Copy link
Author

As I don't plan to have thousands of files in a bucket folder at the same time, the Connector::getBucket() should do the job :)

By the way, I see defined('AKEEBAENGINE') or die(); inside the classes. Do I need to define the constant upfront of there is a better way to use the classes?

THANK YOU!

@nikosdion
Copy link
Member

Yes you need to define the constant upfront. We had to that because the original land main use case of the S3 repo is its integration with our backup engine. Since it's typically installed in publicly accessible folders and the server may or may not support .htaccess / web.config access controls we have the die statement to prevent any accidental directory disclosure from error messages.

I'll update the README to note that. Thank you for the heads up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants