Amazon.S3.IO.S3FileInfo()。Exists返回400个加密文件的错误请求

时间:2017-10-17 04:48:08

标签: c# amazon-web-services encryption amazon-s3

我使用C#和AWSSDK v3将文件上传到S3存储桶。该文件使用ServerSideEncryptionCustomerMethod加密。我可以上传文件,但如果我使用S3FileInfo()检查文件是否存在。存在,则会引发错误(400)错误请求。但是,如果我在上传例程中注释掉指定加密的行,则S3FileInfo()。Exists会在不抛出错误的情况下找到该文件。我做错了什么?或者有一种不同的方法来检查文件加密时是否存在?

这是我的上传例程:

       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;
    }

我的例程检查文件是否存在:

       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;
    }

1 个答案:

答案 0 :(得分:0)

好吧,我认为我使用的类/方法是不支持加密的高级API之一。我更改了代码以执行元数据查询,以查看是否有任何内容返回。如果它找不到该文件,它会抛出一个" NotFound"我检查的s3Exception中的ErrorCode。希望这有助于其他人。如果其他人建议采用更好的方法,我也很乐意学习它。

        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;
    }