c++amazon-web-servicesamazon-s3aws-sdkaws-sdk-cpp

How to check an AWS S3 key for existence with the AWS CPP SDK?


I use the S3 SDK CPP and have the following cenario:

  1. I get some information sent from a client to my server (client wants to download from S3)
  2. With the information sent I create a S3 key
  3. I want to check if the key exists (has a file) on the S3
  4. I create a presigned URL that allows the client to download a file from S3
  5. Send URL to client
  6. Client downloads the file

Before I execute step 4 I want to check if the key really exists on the S3. The client can't download a file that does not exist anyway.

I have an AWS::S3Client object. Do I really need to create a TransferManager for this or is there a simple way to handle this with the client object?

The client itself does not have a relation to S3 so I can't check it there. The server has to do all the work.


Solution

  • I found a working solution:

    auto client = Aws::MakeShared<Aws::S3::S3Client>("client", getCredentials(), getClientConfig());
    Aws::S3::Model::HeadObjectRequest request;
    request.WithBucket(<bucketname>).WithKey(<s3key>);
    const auto response = client->HeadObject(request);
    response.IsSuccess(); //Is key existing on s3?