What is the best way to check if an S3 object exists?

I am currently creating GetObjectMetaDataRequestif GetObjectMetaDataResponsethrowing an exception means that the object does not exist. Is there a better way to check if a file exists without downloading the file.

+5
source share
5 answers

you can use the S3FileInfo and Exists method of this class, it will help you check if the file exists without loading the file. In the example below, I used AWSSDK 3.1.6.net (3.5):

public static  bool ExistsFile()
{
    BasicAWSCredentials basicCredentials = new BasicAWSCredentials("my access key", "my secretkey");
                AmazonS3Config configurationClient = new AmazonS3Config();
                configurationClient.RegionEndpoint = RegionEndpoint.EUCentral1;

                try
                {
                    using (AmazonS3Client clientConnection = new AmazonS3Client(basicCredentials, configurationClient))
                    {

                        S3FileInfo file = new S3FileInfo(clientConnection, "mybucket", "FolderNameUniTest680/FileNameUnitTest680");
                        return file.Exists;//if the file exists return true, in other case false
                    }
                }
                catch(Exception ex)
                {
                    return false;
                }
    }
+5
source

Try this solution, it works for me.

AmazonS3Client client = new AmazonS3Client(accessKey, secretKey, regionEndpoint);       
S3FileInfo s3FileInfo = new S3FileInfo(client, bucketName, fileName);
if (s3FileInfo.Exists)
    return true;
else
    return false;
+2
source

ListObjectRequest, ListObjectsRequest, . , , . , , , . ( - , , ).

Instead, you can try requesting a parts list if you know the boot identifier.

Also, I have no idea. I would like to chat with the person who wrote the S3 api ...

+1
source

You will probably have to use the REST API yourself, as the proposed method internally just does the same (try ... catch the request)

0
source

Yes.

You can use ListObjectsRequest. Use the Marker property and get only 1 element.

-1
source

All Articles