Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seeing if object exists in S3 using PHP

Tags:

php

amazon-s3

I am using PHP and I am using the S3 API to upload a file, but I wanted to make sure that this exact filename doesn't already exist in the bucket before upload.

I have found a few examples online that use "file_get_contents" but doesn't this mean that you would have to download the entire file first? Usually, these files are about 10 mb, so ideally, I wouldn't really want to do this.

Is there perhaps a way to use "file_get_contents" without downloading the file?

Or better yet, perhaps I could use an API request to see if the filename exists?

It's not important to me whether or not the content, or filesize, is the same, just the filename.

like image 487
SSH This Avatar asked Jun 01 '12 01:06

SSH This


People also ask

How do you check if S3 bucket exists or not?

To check whether a bucket already exists before attempting to create one with the same name, call the doesBucketExist method. It will return true if the bucket exists, and false otherwise. if (s3. doesBucketExistV2(bucket_name)) { System.

What objects are stored in S3?

All objects are stored in S3 buckets and can be organized with shared names called prefixes. You can also append up to 10 key-value pairs called S3 object tags to each object, which can be created, updated, and deleted throughout an object's lifecycle.

When you put objects in Amazon S3 What is the indication that an object?

When you put objects in Amazon S3, what is the indication that an object was successfully stored? Each S3 account has a special bucket named_s3_logs. Success codes are written to this bucket with a timestamp and checksum. A success code is inserted into the S3 object metadata.

What is checksum in S3?

Amazon S3 uses checksum values to verify the integrity of data that you upload to or download from Amazon S3. In addition, you can request that another checksum value be calculated for any object that you store in Amazon S3.


2 Answers

Gets whether or not the specified Amazon S3 object exists in the specified bucket.

AmazonS3 doesObjectExist

$s3 = new AmazonS3(); $bucket = 'my-bucket' . strtolower($s3->key);  $response = $s3->doesObjectExist($bucket, 'test1.txt');  // Success? (Boolean, not a CFResponse object) var_dump($response); 
like image 103
waqas Avatar answered Sep 21 '22 14:09

waqas


try to use code below:

$s3 = new S3();  $info = $s3->getObjectInfo($bucket, $filename); if ($info) {     echo 'File exists'; } else {     echo 'File does not exists'; } 

download the S3 SDK from amazon for php. There is a class called S3; create an object of S3. The object will allow to call the getObjectInfo() method. Pass your S3 bucket name and the file name (often the file name is referred as key). The getObjectInfo() method will return some information if the file exists, otherwise the method will return FALSE.

like image 42
Amir Md Amiruzzaman Avatar answered Sep 22 '22 14:09

Amir Md Amiruzzaman