hoekma
hoekma

Reputation: 803

Amazon.S3.IO.S3FileInfo().Exists returns 400 bad request for encrypted files

I am using C# and AWSSDK v3 to upload files into an S3 bucket. The file is encrypted using ServerSideEncryptionCustomerMethod. I can upload the file, but if I check if the file exists using S3FileInfo().Exists, an error is thrown as a (400) Bad Request. However, if I comment out the lines that specify encryption in the upload routine, the S3FileInfo().Exists finds the file without throwing an error. What I am doing wrong? Or is there a different way to check if a file exists when it is encrypted?

Here is my upload routine:

       public static string wfUpload(Stream pFileStream, string pBucketName, string pKeyName, string pCryptoKey) {
        string retVal = "";
        try {
            using (var lS3Client = new AmazonS3Client()) {
                Aes aesEncryption = Aes.Create();
                aesEncryption.KeySize = 256;
                aesEncryption.GenerateKey();
                string lCryptoKey = Convert.ToBase64String(aesEncryption.Key);

                PutObjectRequest request = new PutObjectRequest {
                    BucketName = pBucketName,
                    Key = pKeyName,
                    ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey = lCryptoKey,
                };

                request.InputStream = pFileStream;
                PutObjectResponse response = lS3Client.PutObject(request);

                retVal = lCryptoKey;
            }
        }
        catch (AmazonS3Exception s3Exception) {
            Console.WriteLine(s3Exception.Message,
                              s3Exception.InnerException);

            throw (s3Exception);
        }
        catch (Exception e) {
            throw (e);
        }

        return retVal;
    }

And my routine to check if the file exists or not:

       public static bool wfFileExists(String pBucketName, String pKeyName) {
        bool retVal = false;
        try {
            using (var lS3Client = new AmazonS3Client()) {
                if (new Amazon.S3.IO.S3FileInfo(lS3Client, pBucketName, pKeyName).Exists) {
                    retVal = true;
                }
            }
        }
        catch (AmazonS3Exception s3Exception) {
            Console.WriteLine(s3Exception.Message,
                              s3Exception.InnerException);

            throw (s3Exception);
        }
        catch (Exception e) {
            throw (e);
        }

        return retVal;
    }

Upvotes: 0

Views: 1659

Answers (1)

hoekma
hoekma

Reputation: 803

Well, I think the class/method I was using is one of the high level APIs that doesn't support encryption. I changed my code to do a meta-data query to see if anything comes back. If it can't find the file it throws a "NotFound" ErrorCode in the s3Exception that I check for. Hopefully this helps someone else. If someone else suggests a better approach, I'd love to learn it too.

        public static bool wfFileExists(String pBucketName, String pKeyName, String pCryptoKey) {
        bool retVal = false;
        try {
            using (var lS3Client = new AmazonS3Client()) {
                GetObjectMetadataRequest request = new GetObjectMetadataRequest {
                    BucketName = pBucketName,
                    Key = pKeyName,
                    ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey = pCryptoKey,
                };

                GetObjectMetadataResponse lMetaData = lS3Client.GetObjectMetadata(request);

                // If an error is not thrown, we found the metadata.
                retVal = true;
            }
        }
        catch (AmazonS3Exception s3Exception) {
            Console.WriteLine(s3Exception.Message,
                              s3Exception.InnerException);

            if (s3Exception.ErrorCode != "NotFound") {
                throw (s3Exception);
            }
        }
        catch (Exception e) {
            throw (e);
        }

        return retVal;
    }

Upvotes: 0

Related Questions