Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if an object exists in a S3 bucket based on wildcard

Tags:

c#

amazon-s3

Can someone please show me how to determine if a certain file/object exists in a S3 bucket and display a message if it exists or if it does not exist.

Basically I want it to:

1) Check a bucket on my S3 account such as testbucket

2) Inside of that bucket, look to see if there is a file with the prefix test_ (test_file.txt or test_data.txt).

3) If that file exists, then display a MessageBox (or Console message) that the file exists, or that the file does not exist.

Can someone please show me how to do this?

like image 262
fraXis Avatar asked Aug 19 '10 21:08

fraXis


People also ask

Is S3 searchable?

Our solution is built with Amazon S3 event notifications, AWS Lambda, AWS Glue Catalog, and Amazon Athena. These services allow you to search thousands of objects in an S3 bucket by filenames, object metadata, and object keys.

Is S3 getObject synchronous?

But as s3. getObject is an asynchronous call the current thread moves on & nothing gets added to the fileContentList while I am doing the merging.

How do I know if my S3 bucket is accessible?

Log into the console, click on S3, and look for the Public tag. AWS uses some advanced back end math to evaluate all the bucket policies to figure out if something is public, which catches most of the fringe cases, but it does not show if the bucket is private and objects in it are public.


1 Answers

Using the AWSSDK For .Net I Currently do something along the lines of:

public bool Exists(string fileKey, string bucketName) {         try         {             response = _s3Client.GetObjectMetadata(new GetObjectMetadataRequest()                .WithBucketName(bucketName)                .WithKey(key));              return true;         }          catch (Amazon.S3.AmazonS3Exception ex)         {             if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)                 return false;              //status wasn't not found, so throw the exception             throw;         } } 

It kinda sucks, but it works for now.

like image 62
Alex Avatar answered Sep 20 '22 14:09

Alex